-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-intl): introduce FormattedDateTimeRange, a stage-3 API
- Loading branch information
Showing
9 changed files
with
195 additions
and
4 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
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,31 @@ | ||
import * as React from 'react'; | ||
import {Context} from './injectIntl'; | ||
import {FormatDateOptions} from '@formatjs/intl'; | ||
import {invariantIntlContext} from '../utils'; | ||
import {DateTimeFormat} from '@formatjs/ecma402-abstract'; | ||
|
||
interface Props extends FormatDateOptions { | ||
from: Parameters<DateTimeFormat['formatRange']>[0]; | ||
to: Parameters<DateTimeFormat['formatRange']>[1]; | ||
children?(value: React.ReactNode): React.ReactElement | null; | ||
} | ||
|
||
const FormattedDateTimeRange: React.FC<Props> = props => ( | ||
<Context.Consumer> | ||
{(intl): React.ReactElement | null => { | ||
invariantIntlContext(intl); | ||
const {from, to, children, ...formatProps} = props; | ||
// TODO: fix TS type definition for localeMatcher upstream | ||
const formattedValue = intl.formatDateTimeRange(from, to, formatProps); | ||
|
||
if (typeof children === 'function') { | ||
return children(formattedValue as any); | ||
} | ||
const Text = intl.textComponent || React.Fragment; | ||
return <Text>{formattedValue}</Text>; | ||
}} | ||
</Context.Consumer> | ||
); | ||
|
||
FormattedDateTimeRange.displayName = 'FormattedDateTimeRange'; | ||
export default FormattedDateTimeRange; |
3 changes: 3 additions & 0 deletions
3
packages/react-intl/tests/unit/components/__snapshots__/dateTimeRange.tsx.snap
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,3 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`<FormattedDateTimeRange> falls back and warns on invalid Intl.DateTimeFormat options 1`] = `"FORMAT_ERROR"`; |
94 changes: 94 additions & 0 deletions
94
packages/react-intl/tests/unit/components/dateTimeRange.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import * as React from 'react'; | ||
import {mount} from 'enzyme'; | ||
import {FormattedDateTimeRange} from '../../../'; | ||
import {mountFormattedComponentWithProvider} from '../testUtils'; | ||
import {createIntl} from '../../../src/components/provider'; | ||
import {IntlShape} from '../../../'; | ||
|
||
const mountWithProvider = mountFormattedComponentWithProvider( | ||
FormattedDateTimeRange | ||
); | ||
|
||
describe('<FormattedDateTimeRange>', () => { | ||
let intl: IntlShape; | ||
beforeEach(() => { | ||
intl = createIntl({ | ||
locale: 'en', | ||
}); | ||
}); | ||
|
||
it('has a `displayName`', () => { | ||
expect(typeof FormattedDateTimeRange.displayName).toBe('string'); | ||
}); | ||
|
||
it('throws when <IntlProvider> is missing from ancestry', () => { | ||
expect(() => | ||
mount(<FormattedDateTimeRange from={Date.now()} to={Date.now()} />) | ||
).toThrow(Error); | ||
}); | ||
|
||
it('renders a formatted date in a <>', () => { | ||
const from = new Date('2020-01-01'); | ||
const to = new Date('2020-01-15'); | ||
|
||
const rendered = mountWithProvider({from, to}, intl); | ||
|
||
expect(rendered.text()).toBe(intl.formatDateTimeRange(from, to)); | ||
}); | ||
it('renders a formatted date w/o textComponent', () => { | ||
const from = new Date('2020-01-01'); | ||
const to = new Date('2020-01-15'); | ||
const rendered = mountWithProvider( | ||
{from, to}, | ||
{...intl, textComponent: '' as any} | ||
); | ||
|
||
expect(rendered.text()).toBe(intl.formatDateTimeRange(from, to)); | ||
}); | ||
|
||
it('accepts valid Intl.DateTimeFormat options as props', () => { | ||
const from = new Date('2020-01-01'); | ||
const to = new Date('2020-01-15'); | ||
const options = {year: 'numeric'}; | ||
|
||
const rendered = mountWithProvider({from, to, ...options}, intl); | ||
|
||
expect(rendered.text()).toBe(intl.formatDateTimeRange(from, to, options)); | ||
}); | ||
|
||
it('falls back and warns on invalid Intl.DateTimeFormat options', () => { | ||
const from = new Date(); | ||
const onError = jest.fn(); | ||
const rendered = mountWithProvider( | ||
// @ts-ignore | ||
{from, to: undefined, year: 'invalid'}, | ||
{...intl, onError} | ||
); | ||
|
||
expect(rendered.text()).toBe(String(from)); | ||
expect(onError).toHaveBeenCalled(); | ||
expect(onError.mock.calls[0][0].code).toMatchSnapshot(); | ||
}); | ||
|
||
it('supports function-as-child pattern', () => { | ||
const from = new Date('2020-01-01'); | ||
const to = new Date('2020-01-15'); | ||
const spyChildren = jest.fn().mockImplementation(() => <b>Jest</b>); | ||
const rendered = mountWithProvider( | ||
{ | ||
from, | ||
to, | ||
children: spyChildren, | ||
}, | ||
intl | ||
).find('b'); | ||
|
||
expect(spyChildren).toHaveBeenCalledTimes(1); | ||
expect(spyChildren.mock.calls[0]).toEqual([ | ||
intl.formatDateTimeRange(from, to), | ||
]); | ||
|
||
expect(rendered.type()).toBe('b'); | ||
expect(rendered.text()).toBe('Jest'); | ||
}); | ||
}); |
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
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