Skip to content

Commit 4692b90

Browse files
Example of react-intl added.
1 parent 6de78e0 commit 4692b90

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

docs/example-react-intl.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
id: example-react-intl
3+
title: React Intl
4+
---
5+
6+
```jsx
7+
import React from 'react'
8+
import 'jest-dom/extend-expect'
9+
import { render, cleanup, getByTestId } from 'react-testing-library'
10+
import { IntlProvider, FormattedDate } from 'react-intl'
11+
import IntlPolyfill from 'intl'
12+
import 'intl/locale-data/jsonp/pt'
13+
14+
const setupTests = () => {
15+
// https://formatjs.io/guides/runtime-environments/#server
16+
if (global.Intl) {
17+
Intl.NumberFormat = IntlPolyfill.NumberFormat
18+
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat
19+
} else {
20+
global.Intl = require('intl')
21+
}
22+
}
23+
24+
const FormatDateView = () => {
25+
return (
26+
<div data-testid="date-display">
27+
<FormattedDate
28+
value="2019-03-11"
29+
timeZone="utc"
30+
day="2-digit"
31+
month="2-digit"
32+
year="numeric"
33+
/>
34+
</div>
35+
)
36+
}
37+
38+
const renderWithReactIntl = component => {
39+
return {
40+
...render(<IntlProvider locale="pt">{component}</IntlProvider>),
41+
}
42+
}
43+
44+
setupTests()
45+
afterEach(cleanup)
46+
47+
test('it should render FormattedDate and have a formated pt date', () => {
48+
const { container } = renderWithReactIntl(<FormatDateView />)
49+
expect(getByTestId(container, 'date-display')).toHaveTextContent('11/03/2019')
50+
})
51+
```

website/sidebars.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"example-input-event",
1313
"example-update-props",
1414
"example-react-context",
15+
"example-react-intl",
1516
"example-react-redux",
1617
"example-react-router",
1718
"example-reach-router",

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+

0 commit comments

Comments
 (0)