Content-Length: 520820 | pFad | http://github.com/stdavis/testing-library-docs/commit/17e7335c03f2af26946d549e68bdcaeaa5a2dd49

B3 Docs: Update React-Intl recipe (#439) · stdavis/testing-library-docs@17e7335 · GitHub
Skip to content

Commit 17e7335

Browse files
Docs: Update React-Intl recipe (testing-library#439)
Co-authored-by: Matan Borenkraout <matan.borenkraout@nielsen.com>
1 parent 69b70f5 commit 17e7335

File tree

1 file changed

+90
-40
lines changed

1 file changed

+90
-40
lines changed

docs/example-react-intl.md

Lines changed: 90 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,105 @@ title: React Intl
88
> If you want to combine setupTests with another setup you should check
99
> [`setup`](react-testing-library/setup.md)
1010
11-
## Configuring React-Intl Polyfills
11+
## Configuring React-Intl Polyfills / Locales
1212

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:
1515

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:
1820

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 })
3293
}
3394
34-
setupTests();
95+
// re-export everything
96+
export * from '@testing-library/react'
97+
98+
// override render method
99+
export { render }
35100
```
36101
37-
A complete example:
102+
## A complete example
38103
39104
```jsx
40105
import React from 'react'
41106
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'
56110
57111
const FormatDateView = () => {
58112
return (
@@ -68,14 +122,10 @@ const FormatDateView = () => {
68122
)
69123
}
70124
71-
const renderWithReactIntl = component => {
72-
return render(<IntlProvider locale="pt">{component}</IntlProvider>)
73-
}
74-
75125
setupTests()
76126
77127
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')
80130
})
81131
```

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/stdavis/testing-library-docs/commit/17e7335c03f2af26946d549e68bdcaeaa5a2dd49

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy