Skip to content

Commit 46ad1c8

Browse files
committed
fix: fix state typo, fixes #1411
1 parent 105af00 commit 46ad1c8

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

src/components/provider.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,19 @@ export default class IntlProvider extends React.PureComponent<
7373
private cache: IntlCache = createIntlCache();
7474
state: State = {
7575
cache: this.cache,
76-
intl: createIntl(filterIntlConfig(this.props)),
76+
intl: createIntl(filterIntlConfig(this.props), this.cache),
7777
prevConfig: filterIntlConfig(this.props),
7878
};
7979

8080
static getDerivedStateFromProps(
8181
props: OptionalIntlConfig,
8282
{prevConfig, cache}: State
83-
) {
83+
): Partial<State> | null {
8484
const config = filterIntlConfig(props);
8585
if (!shallowEquals(prevConfig, config)) {
8686
return {
8787
intl: createIntl(config, cache),
88-
prevProps: config,
88+
prevConfig: config,
8989
};
9090
}
9191
return null;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`useIntl() hook should work when switching locale on provider 1`] = `
4+
<IntlProvider
5+
defaultFormats={Object {}}
6+
defaultLocale="en"
7+
formats={Object {}}
8+
locale="en"
9+
messages={Object {}}
10+
onError={[Function]}
11+
textComponent={Symbol(react.fragment)}
12+
>
13+
<FC>
14+
$10,000.00
15+
</FC>
16+
</IntlProvider>
17+
`;
18+
19+
exports[`useIntl() hook should work when switching locale on provider 2`] = `
20+
<IntlProvider
21+
defaultFormats={Object {}}
22+
defaultLocale="en"
23+
formats={Object {}}
24+
locale="es"
25+
messages={Object {}}
26+
onError={[Function]}
27+
textComponent={Symbol(react.fragment)}
28+
>
29+
<FC>
30+
10.000,00 US$
31+
</FC>
32+
</IntlProvider>
33+
`;
34+
35+
exports[`useIntl() hook should work when switching locale on provider 3`] = `
36+
<IntlProvider
37+
defaultFormats={Object {}}
38+
defaultLocale="en"
39+
formats={Object {}}
40+
locale="en"
41+
messages={Object {}}
42+
onError={[Function]}
43+
textComponent={Symbol(react.fragment)}
44+
>
45+
<FC>
46+
$10,000.00
47+
</FC>
48+
</IntlProvider>
49+
`;

test/unit/components/useIntl.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ const FunctionComponent = ({spy}) => {
99
return null;
1010
};
1111

12+
const FC = () => {
13+
const {formatNumber} = useIntl();
14+
return formatNumber(10000, {style: 'currency', currency: 'USD'}) as any;
15+
};
16+
1217
describe('useIntl() hook', () => {
1318
it('throws when <IntlProvider> is missing from ancestry', () => {
1419
const consoleError = jest
@@ -30,4 +35,21 @@ describe('useIntl() hook', () => {
3035
const intl = rendered.state('intl');
3136
expect(spy).toHaveBeenCalledWith(intl);
3237
});
38+
39+
it('should work when switching locale on provider', () => {
40+
const rendered = mount(
41+
<IntlProvider locale="en">
42+
<FC />
43+
</IntlProvider>
44+
);
45+
expect(rendered).toMatchSnapshot();
46+
rendered.setProps({
47+
locale: 'es',
48+
});
49+
expect(rendered).toMatchSnapshot();
50+
rendered.setProps({
51+
locale: 'en',
52+
});
53+
expect(rendered).toMatchSnapshot();
54+
});
3355
});

0 commit comments

Comments
 (0)