File tree Expand file tree Collapse file tree 3 files changed +74
-3
lines changed Expand file tree Collapse file tree 3 files changed +74
-3
lines changed Original file line number Diff line number Diff line change @@ -73,19 +73,19 @@ export default class IntlProvider extends React.PureComponent<
73
73
private cache : IntlCache = createIntlCache ( ) ;
74
74
state : State = {
75
75
cache : this . cache ,
76
- intl : createIntl ( filterIntlConfig ( this . props ) ) ,
76
+ intl : createIntl ( filterIntlConfig ( this . props ) , this . cache ) ,
77
77
prevConfig : filterIntlConfig ( this . props ) ,
78
78
} ;
79
79
80
80
static getDerivedStateFromProps (
81
81
props : OptionalIntlConfig ,
82
82
{ prevConfig, cache} : State
83
- ) {
83
+ ) : Partial < State > | null {
84
84
const config = filterIntlConfig ( props ) ;
85
85
if ( ! shallowEquals ( prevConfig , config ) ) {
86
86
return {
87
87
intl : createIntl ( config , cache ) ,
88
- prevProps : config ,
88
+ prevConfig : config ,
89
89
} ;
90
90
}
91
91
return null ;
Original file line number Diff line number Diff line change
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
+ ` ;
Original file line number Diff line number Diff line change @@ -9,6 +9,11 @@ const FunctionComponent = ({spy}) => {
9
9
return null ;
10
10
} ;
11
11
12
+ const FC = ( ) => {
13
+ const { formatNumber} = useIntl ( ) ;
14
+ return formatNumber ( 10000 , { style : 'currency' , currency : 'USD' } ) as any ;
15
+ } ;
16
+
12
17
describe ( 'useIntl() hook' , ( ) => {
13
18
it ( 'throws when <IntlProvider> is missing from ancestry' , ( ) => {
14
19
const consoleError = jest
@@ -30,4 +35,21 @@ describe('useIntl() hook', () => {
30
35
const intl = rendered . state ( 'intl' ) ;
31
36
expect ( spy ) . toHaveBeenCalledWith ( intl ) ;
32
37
} ) ;
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
+ } ) ;
33
55
} ) ;
You can’t perform that action at this time.
0 commit comments