-
Notifications
You must be signed in to change notification settings - Fork 380
/
Trans.test.tsx
205 lines (177 loc) · 6.14 KB
/
Trans.test.tsx
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import * as React from "react"
import { render } from "@testing-library/react"
import { Trans, I18nProvider } from "@lingui/react"
import { setupI18n } from "@lingui/core"
describe("Trans component", function () {
/*
* Setup context, define helpers
*/
const i18n = setupI18n({
locale: "cs",
messages: {
cs: {
"All human beings are born free and equal in dignity and rights.":
"Všichni lidé rodí se svobodní a sobě rovní co do důstojnosti a práv.",
"My name is {name}": "Jmenuji se {name}",
Original: "Původní",
Updated: "Aktualizovaný",
"msg.currency": "{value, number, currency}",
ID: "Translation",
},
},
})
const renderWithI18n = (node) =>
render(<I18nProvider i18n={i18n}>{node}</I18nProvider>)
const text = (node) => renderWithI18n(node).container.textContent
const html = (node) => renderWithI18n(node).container.innerHTML
/*
* Tests
*/
it("should throw error without i18n context", function () {
const originalConsole = console.error
console.error = jest.fn()
expect(() => render(<Trans id="unknown" />)).toThrow()
console.error = originalConsole
})
it("should throw a console.error about deprecated usage of string built-in", function () {
const originalConsole = console.error
console.error = jest.fn()
renderWithI18n(<Trans render="span" id="Just a text" />)
expect(console.error).toHaveBeenCalled()
renderWithI18n(<Trans render="span" id="Just a text" />)
expect(console.error).toHaveBeenCalledTimes(2)
console.error = originalConsole
})
it("should throw a console.error if using twice props", function () {
const originalConsole = console.error
console.error = jest.fn()
renderWithI18n(<Trans render="div" component="span" id="Just a text" />)
expect(console.error).toHaveBeenCalled()
console.error = originalConsole
})
it("should render default string", function () {
expect(text(<Trans id="unknown" />)).toEqual("unknown")
expect(text(<Trans id="unknown" message="Not translated yet" />)).toEqual(
"Not translated yet"
)
expect(
text(
<Trans
id="unknown"
message="Not translated yet, {name}"
values={{ name: "Dave" }}
/>
)
).toEqual("Not translated yet, Dave")
})
it("should render translation", function () {
const translation = text(
<Trans id="All human beings are born free and equal in dignity and rights." />
)
expect(translation).toEqual(
"Všichni lidé rodí se svobodní a sobě rovní co do důstojnosti a práv."
)
})
it("should render translation from variable", function () {
const msg =
"All human beings are born free and equal in dignity and rights."
const translation = text(<Trans id={msg} />)
expect(translation).toEqual(
"Všichni lidé rodí se svobodní a sobě rovní co do důstojnosti a práv."
)
})
it("should render component in variables", function () {
const translation = html(
<Trans id="Hello {name}" values={{ name: <strong>John</strong> }} />
)
expect(translation).toEqual("Hello <strong>John</strong>")
})
it("should render translation inside custom component", function () {
const Component = (props) => <p className="lead">{props.children}</p>
const html1 = html(<Trans component={Component} id="Original" />)
const html2 = html(
<Trans
render={({ translation }) => <p className="lead">{translation}</p>}
id="Original"
/>
)
expect(html1).toEqual('<p class="lead">Původní</p>')
expect(html2).toEqual('<p class="lead">Původní</p>')
})
it("should render custom format", function () {
const translation = text(
<Trans
id="msg.currency"
values={{ value: 1 }}
formats={{
currency: {
style: "currency",
currency: "EUR",
minimumFractionDigits: 2,
},
}}
/>
)
expect(translation).toEqual("1,00 €")
})
describe("rendering", function () {
it("should render just a text without wrapping element", function () {
const txt = html(<Trans id="Just a text" />)
expect(txt).toEqual("Just a text")
})
it("should render custom element", function () {
const element = html(<Trans render={({ id, translation }) => <h1 id={id}>{translation}</h1>} id="Headline" />)
expect(element).toEqual(`<h1 id="Headline">Headline</h1>`)
})
it("should render function", function () {
const spy = jest.fn()
text(
<Trans
id="ID"
message="Default"
render={(props) => {
spy(props)
return null
}}
/>
)
expect(spy).toHaveBeenCalledWith({
id: "ID",
message: "Default",
translation: "Translation",
})
})
it("should take defaultComponent prop with a custom component", function () {
const ComponentFC: React.FunctionComponent = (props: { children?: React.ReactNode }) => {
return (<div>{props.children}</div>)
}
const span = render(
<I18nProvider i18n={i18n} defaultComponent={ComponentFC}>
<Trans id="Just a text" />
</I18nProvider>
).container.innerHTML
expect(span).toEqual(`<div>Just a text</div>`)
})
})
describe("component prop rendering", function() {
it("should render class component as simple prop", function () {
class ClassComponent extends React.Component {
render() {
return (
<div>Headline</div>
)
}
}
const element = html(<Trans component={ClassComponent} id="Headline" />)
expect(element).toEqual("<div>Headline</div>")
})
it("should render functional component as simple prop", function () {
const ComponentFC: React.FunctionComponent = (props: { id: any, children?: React.ReactNode }) => {
const [state] = React.useState("value")
return <div id={props.id}>{state}</div>
}
const element = html(<Trans component={ComponentFC} id="Headline" />)
expect(element).toEqual(`<div>value</div>`)
})
})
})