-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(content-explorer): Migrated Date (#3800)
- Loading branch information
1 parent
f6f8444
commit 5106779
Showing
3 changed files
with
83 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as React from 'react'; | ||
import { FormattedMessage } from 'react-intl'; | ||
import Datefield from '../common/date'; | ||
import messages from '../common/messages'; | ||
import { FIELD_INTERACTED_AT } from '../../constants'; | ||
import type { BoxItem } from '../../common/types/core'; | ||
|
||
type Props = { | ||
dataKey: string, | ||
item: BoxItem | ||
}; | ||
|
||
const Date = ({ | ||
dataKey, | ||
item, | ||
}: Props) => { | ||
const { | ||
modified_at = '', | ||
interacted_at = '', | ||
modified_by, | ||
}: BoxItem = item; | ||
const modifiedBy: string = modified_by ? modified_by.name || '' : ''; | ||
const isRecents: boolean = dataKey === FIELD_INTERACTED_AT; | ||
const date: string = isRecents ? interacted_at || modified_at : modified_at; | ||
const DateValue = <Datefield capitalize date={date} omitCommas />; | ||
|
||
if (isRecents || !modifiedBy) { | ||
return DateValue; | ||
} | ||
|
||
return ( | ||
<FormattedMessage | ||
{...messages.nameDate} | ||
values={{ | ||
date: DateValue, | ||
name: modifiedBy, | ||
}} | ||
/> | ||
); | ||
}; | ||
export default Date; |
11 changes: 5 additions & 6 deletions
11
src/elements/content-explorer/Date.js → src/elements/content-explorer/Date.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import * as React from 'react'; | ||
import { render, screen } from '../../../test-utils/testing-library'; | ||
|
||
import Date, { DateProps } from '../Date'; | ||
import { FIELD_INTERACTED_AT } from '../../../constants'; | ||
|
||
const itemWithModifiedBy = { | ||
modified_at: '2023-10-01T12:00:00Z', | ||
interacted_at: '2023-10-02T12:00:00Z', | ||
modified_by: { name: 'John Doe' }, | ||
}; | ||
|
||
const itemWithoutModifiedBy = { | ||
modified_at: '2023-10-01T12:00:00Z', | ||
interacted_at: '2023-10-02T12:00:00Z', | ||
}; | ||
|
||
describe('elements/content-explorer/Date', () => { | ||
const renderComponent = (props: DateProps) => { | ||
return render(<Date {...props} />); | ||
}; | ||
|
||
test('renders date with modified_by name when dataKey is not FIELD_INTERACTED_AT', () => { | ||
renderComponent({ dataKey: 'someKey', item: itemWithModifiedBy }); | ||
expect(screen.getByText(/Sun Oct 1 2023\s+by John Doe/)).toBeInTheDocument(); | ||
}); | ||
|
||
test('renders date without modified_by name when dataKey is FIELD_INTERACTED_AT', () => { | ||
renderComponent({ dataKey: FIELD_INTERACTED_AT, item: itemWithoutModifiedBy }); | ||
expect(screen.getByText('Mon Oct 2 2023')).toBeInTheDocument(); | ||
}); | ||
|
||
test('renders date without modified_by name when modified_by is not provided', () => { | ||
renderComponent({ dataKey: 'someKey', item: itemWithoutModifiedBy }); | ||
expect(screen.getByText('Sun Oct 1 2023')).toBeInTheDocument(); | ||
}); | ||
}); |