forked from lingui/js-lingui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateFormat.test.js
171 lines (156 loc) · 4.24 KB
/
DateFormat.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// @flow
import * as React from "react"
import { mount } from "enzyme"
import { DateFormat, NumberFormat } from "@lingui/react"
describe("DateFormat", function() {
const languageContext = (language, locales) => ({
context: {
linguiPublisher: {
i18n: {
language: language,
locales: locales
}
}
}
})
it("should render", function() {
const nowStr = "2017-06-17:14:00.000Z"
const now = new Date(nowStr)
const [day, month, year] = [
now.getDate(),
now.getMonth() + 1,
now.getFullYear()
]
const expectedDate = `${month}/${day}/${year}`
const dateObj = mount(
<DateFormat value={now} />,
languageContext("en")
).find("Render")
expect(dateObj.text()).toEqual(expectedDate)
const dateString = mount(
<DateFormat value={nowStr} />,
languageContext("en")
).find("Render")
expect(dateString.text()).toEqual(expectedDate)
})
it("should render using the given locales", function() {
const now = new Date("2017-06-17:14:00.000Z")
const node = mount(
<DateFormat value={now} />,
languageContext("en", "fr-FR")
).find("Render")
expect(node.text()).toEqual("17/06/2017")
})
it("should render using the first recognized locale", function() {
const now = new Date("2017-06-17:14:00.000Z")
const node = mount(
<DateFormat value={now} />,
languageContext("en", ["unknown-locale", "fr-FR"])
).find("Render")
expect(node.text()).toEqual("17/06/2017")
})
it("should render translation inside custom component", function() {
const now = new Date("2017-06-17:14:00.000Z")
const [day, month, year] = [
now.getDate(),
now.getMonth() + 1,
now.getFullYear()
]
const expectedDate = `${month}/${day}/${year}`
const html1 = mount(
<DateFormat value={now} render={<p className="lead" />} />,
languageContext("en")
)
.find("Render")
.html()
const html2 = mount(
<DateFormat
value={now}
render={({ translation }) => <p className="lead">{translation}</p>}
/>,
languageContext("en")
)
.find("Render")
.html()
expect(html1).toEqual(`<p class="lead">${expectedDate}</p>`)
expect(html2).toEqual(html1)
})
})
describe("NumberFormat", function() {
const languageContext = (language, locales) => ({
context: {
linguiPublisher: { i18n: { language: language, locales: locales } }
}
})
it("should render", function() {
const node = mount(
<NumberFormat
value={42}
format={{
style: "currency",
currency: "EUR",
minimumFractionDigits: 2
}}
/>,
languageContext("en")
).find("Render")
expect(node.text()).toEqual("€42.00")
})
it("should render using the given locales", function() {
const node = mount(
<NumberFormat
value={42000}
format={{
style: "currency",
currency: "EUR",
minimumFractionDigits: 2
}}
/>,
languageContext("en", "de-DE")
).find("Render")
expect(node.text()).toEqual("42.000,00 €")
})
it("should render using the first recognized locale", function() {
const node = mount(
<NumberFormat
value={42000}
format={{
style: "currency",
currency: "EUR",
minimumFractionDigits: 2
}}
/>,
languageContext("en", ["unknown-locale", "de-DE"])
).find("Render")
expect(node.text()).toEqual("42.000,00 €")
})
it("should render translation inside custom component", function() {
const format = {
style: "currency",
currency: "EUR",
minimumFractionDigits: 2
}
const html1 = mount(
<NumberFormat
value="42"
format={format}
render={<p className="lead" />}
/>,
languageContext("en")
)
.find("Render")
.html()
const html2 = mount(
<NumberFormat
value="42"
format={format}
render={({ translation }) => <p className="lead">{translation}</p>}
/>,
languageContext("en")
)
.find("Render")
.html()
expect(html1).toEqual('<p class="lead">€42.00</p>')
expect(html2).toEqual(html1)
})
})