@@ -8,51 +8,105 @@ title: React Intl
8
8
> If you want to combine setupTests with another setup you should check
9
9
> [ ` setup ` ] ( react-testing-library/setup.md )
10
10
11
- ## Configuring React-Intl Polyfills
11
+ ## Configuring React-Intl Polyfills / Locales
12
12
13
- If you're using React-Intl in your project, and you need to load a locale, you
14
- must load the Polyfills according to that language.
13
+ If you're using React-Intl in your project, and you ** need** to load a locale,
14
+ You have two options:
15
15
16
- In order to do so, you may use this small setup and/or combine it with other
17
- setups.
16
+ 1 . When using Node 13 and higher, Intl support is now out of the box. The
17
+ default ICU (International Components for Unicode) option for Node is
18
+ ` full-icu ` meaning all ICU's.
19
+ All you need to do is embed the set of ICU data you need:
18
20
19
- ```
20
- // src/setupTests.js
21
- import IntlPolyfill from 'intl'
22
- import 'intl/locale-data/jsonp/pt'
23
-
24
- const setupTests = () => {
25
- // https://formatjs.io/guides/runtime-environments/#server
26
- if (global.Intl) {
27
- Intl.NumberFormat = IntlPolyfill.NumberFormat
28
- Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat
29
- } else {
30
- global.Intl = IntlPolyfill
31
- }
21
+ ``` js
22
+ // test-utils.js
23
+
24
+ const hasFullICU = () => {
25
+ // That's the recommended way to test for ICU support according to Node.js docs
26
+ try {
27
+ const january = new Date (9e8 )
28
+ const pt = new Intl.DateTimeFormat (' pt' , { month: ' long' })
29
+ return pt .format (january) === ' janeiro'
30
+ } catch (err) {
31
+ return false
32
+ }
33
+ }
34
+
35
+ export const setupTests = () => {
36
+ if (hasFullICU ()) {
37
+ Intl .NumberFormat .format = new Intl.NumberFormat (' pt' ).format
38
+ Intl .DateTimeFormat .format = new Intl.DateTimeFormat (' pt' ).format
39
+ } else {
40
+ global .Intl = IntlPolyfill
41
+ }
42
+ }
43
+ ```
44
+
45
+ 2. When using Node with prior versions, the ICU default option is ` small-icu`
46
+ meaning it includes a subset of ICU data (typically only the English
47
+ locale).
48
+ If you do need to load a locale you have two options:
49
+
50
+ 1. Load the Polyfills according to that language:
51
+
52
+ ` ` ` js
53
+ // test-utils.js
54
+ import IntlPolyfill from 'intl'
55
+ import 'intl/locale-data/jsonp/pt'
56
+
57
+ export const setupTests = () => {
58
+ // https://formatjs.io/guides/runtime-environments/#server
59
+ if (global.Intl) {
60
+ Intl.NumberFormat = IntlPolyfill.NumberFormat
61
+ Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat
62
+ } else {
63
+ global.Intl = IntlPolyfill
64
+ }
65
+ }
66
+ ` ` `
67
+
68
+ 2. Load the ICU ' s at runtime:
69
+ Install the package
70
+ [full-icu](https://github.com/unicode-org/full-icu-npm) and inject it to
71
+ your test environment, you can do that by setting `NODE_ICU_DATA` before
72
+ calling jest: `NODE_ICU_DATA=node_modules/full-icu jest`. Doing that you
73
+ will give you full-icu support as shown in option 1.
74
+
75
+ ## Creating a custom render function
76
+
77
+ To test our translated component we can create a custom `render` function using
78
+ the `wrapper` option as explained in the
79
+ [setup](./react-testing-library/setup.md) page.
80
+ Our custom `render` function can look like this:
81
+
82
+ ```jsx
83
+ // test-utils.js
84
+ import React from ' react'
85
+ import { render as rtlRender } from ' @testing- library/ react'
86
+ import { IntlProvider } from ' react- intl'
87
+
88
+ function render(ui, { locale = ' pt' , ...renderOptions } = {}) {
89
+ function Wrapper({ children }) {
90
+ return <IntlProvider locale={locale}>{children}</IntlProvider>
91
+ }
92
+ return rtlRender(ui, { wrapper: Wrapper, ...renderOptions })
32
93
}
33
94
34
- setupTests();
95
+ // re-export everything
96
+ export * from ' @testing- library/ react'
97
+
98
+ // override render method
99
+ export { render }
35
100
```
36
101
37
- A complete example:
102
+ ## A complete example
38
103
39
104
```jsx
40
105
import React from ' react'
41
106
import ' @testing- library/ jest- dom/ extend- expect'
42
- import { render , getByTestId } from ' @testing-library/react'
43
- import { IntlProvider , FormattedDate } from ' react-intl'
44
- import IntlPolyfill from ' intl'
45
- import ' intl/locale-data/jsonp/pt'
46
-
47
- const setupTests = () => {
48
- // https://formatjs.io/guides/runtime-environments/#server
49
- if (global .Intl ) {
50
- Intl .NumberFormat = IntlPolyfill .NumberFormat
51
- Intl .DateTimeFormat = IntlPolyfill .DateTimeFormat
52
- } else {
53
- global .Intl = IntlPolyfill
54
- }
55
- }
107
+ // We' re importing from our own created test- utils and not RTL ' s
108
+ import { render, screen, setupTests } from ' ../ test- utils .js '
109
+ import { FormattedDate } from ' react- intl'
56
110
57
111
const FormatDateView = () => {
58
112
return (
@@ -68,14 +122,10 @@ const FormatDateView = () => {
68
122
)
69
123
}
70
124
71
- const renderWithReactIntl = component => {
72
- return render (< IntlProvider locale= " pt" > {component}< / IntlProvider> )
73
- }
74
-
75
125
setupTests()
76
126
77
127
test(' it should render FormattedDate and have a formatted pt date' , () => {
78
- const { container } = renderWithReactIntl (< FormatDateView / > )
79
- expect (getByTestId (container, ' date-display' )).toHaveTextContent (' 11/03/2019' )
128
+ render (<FormatDateView />)
129
+ expect(screen. getByTestId(' date- display' )).toHaveTextContent(' 11 / 03 / 2019 ' )
80
130
})
81
131
```
0 commit comments