-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
Trans.test.js
305 lines (272 loc) · 9.87 KB
/
Trans.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import React from 'react'
import { render, cleanup } from '@testing-library/react'
import I18nProvider from '../src/I18nProvider'
import Trans from '../src/Trans'
const TestEnglish = ({ namespaces, logger, ...props }) => {
return (
<I18nProvider lang="en" namespaces={namespaces} config={{ logger }}>
<Trans {...props} />
</I18nProvider>
)
}
describe('Trans', () => {
afterEach(cleanup)
describe('without components', () => {
test('should work the same way than useTranslate', () => {
const i18nKey = 'ns:number'
const expected = 'The number is 42'
const withSingular = {
number: 'The number is {{ num }}',
}
const { container } = render(
<TestEnglish
namespaces={{ ns: withSingular }}
i18nKey={i18nKey}
values={{ num: 42 }}
/>
)
expect(container.textContent).toContain(expected)
})
test('should work with nested keys', () => {
const i18nKey = 'ns:parent.child'
const expected = 'The number is 42'
const withSingular = {
parent: {
child: 'The number is {{ num }}',
},
}
const { container } = render(
<TestEnglish
namespaces={{ ns: withSingular }}
i18nKey={i18nKey}
values={{ num: 42 }}
/>
)
expect(container.textContent).toContain(expected)
})
})
describe('with components', () => {
test('should work with HTML5 Elements', () => {
const i18nKey = 'ns:number'
const expectedText = 'The number is 42'
const expectedHTML = '<h1 id="u1">The number is <b id="u2">42</b></h1>'
const withSingular = {
number: '<0>The number is <1>{{ num }}</1></0>',
}
const { container } = render(
<TestEnglish
namespaces={{ ns: withSingular }}
i18nKey={i18nKey}
values={{ num: 42 }}
components={[<h1 id="u1" />, <b id="u2" />]}
/>
)
expect(container.textContent).toContain(expectedText)
expect(container.innerHTML).toContain(expectedHTML)
})
test('should work with React Components', () => {
const i18nKey = 'ns:number'
const expectedText = 'The number is 42'
const expectedHTML = '<h1>The number is <b>42</b></h1>'
const withSingular = {
number: '<0>The number is <1>{{ num }}</1></0>',
}
const H1 = (p) => <h1 {...p} />
const B = (p) => <b {...p} />
const { container } = render(
<TestEnglish
namespaces={{ ns: withSingular }}
i18nKey={i18nKey}
values={{ num: 42 }}
components={[<H1 />, <B />]}
/>
)
expect(container.textContent).toContain(expectedText)
expect(container.innerHTML).toContain(expectedHTML)
})
test('should work with very nested components', () => {
const i18nKey = 'ns:number'
const expectedText = 'Is the number 42?'
const expectedHTML = '<div><p>Is</p> <b>the <i>number</i></b> 42?</div>'
const withSingular = {
number: '<0><1>Is</1> <2>the <3>number</3></2> {{num}}?</0>',
}
const { container } = render(
<TestEnglish
namespaces={{ ns: withSingular }}
i18nKey={i18nKey}
values={{ num: 42 }}
components={[<div />, <p />, <b />, <i />]}
/>
)
expect(container.textContent).toContain(expectedText)
expect(container.innerHTML).toContain(expectedHTML)
})
test('should work without replacing the HTMLElement if the index is incorrectly', () => {
const i18nKey = 'common:test-html'
const expectedHTML = 'test with bad index.'
const common = {
'test-html': 'test <10>with bad index</10>.',
}
const { container } = render(
<TestEnglish
namespaces={{ common }}
i18nKey={i18nKey}
components={[<b />]}
/>
)
expect(container.innerHTML).toContain(expectedHTML)
})
})
describe('components prop as a object', () => {
test('should work with component as a object', () => {
const i18nKey = 'common:test-html'
const expectedHTML = 'test <b>components as a object</b>.'
const common = {
'test-html': 'test <example>components as a object</example>.',
}
const { container } = render(
<TestEnglish
namespaces={{ common }}
i18nKey={i18nKey}
components={{ example: <b /> }}
/>
)
expect(container.innerHTML).toContain(expectedHTML)
})
test('should work with component as a object of React Components', () => {
const i18nKey = 'common:test-html'
const expectedHTML = 'test <b class="test">components as a object</b>.'
const common = {
'test-html': 'test <example>components as a object</example>.',
}
const Component = ({ children }) => <b className="test">{children}</b>
const { container } = render(
<TestEnglish
namespaces={{ common }}
i18nKey={i18nKey}
components={{ example: <Component /> }}
/>
)
expect(container.innerHTML).toContain(expectedHTML)
})
test('should work with component as a object without replacing the HTMLElement if the key is incorrectly', () => {
const i18nKey = 'common:test-html'
const expectedHTML =
'test <b class="test">components as <u>a</u> object</b>.'
const common = {
'test-html':
'test <example>components <thisIsIncorrect>as <u>a</u> object</thisIsIncorrect></example>.',
}
const Component = ({ children }) => <b className="test">{children}</b>
const { container } = render(
<TestEnglish
namespaces={{ common }}
i18nKey={i18nKey}
components={{ example: <Component />, u: <u /> }}
/>
)
expect(container.innerHTML).toContain(expectedHTML)
})
})
describe('logger', () => {
test('should log a warn key if a key does not exist in the namespace', () => {
console.warn = jest.fn()
const i18nKey = 'ns:number'
const expected =
'[next-translate] "ns:number" is missing in current namespace configuration. Try adding "number" to the namespace "ns".'
const withSingular = {}
render(
<TestEnglish namespaces={{ ns: withSingular }} i18nKey={i18nKey} />
)
expect(console.warn).toBeCalledWith(expected)
})
test('should log a warn key if it has a fallback', () => {
console.warn = jest.fn()
const i18nKey = 'ns:number'
const expected =
'[next-translate] "ns:number" is missing in current namespace configuration. Try adding "number" to the namespace "ns".'
const withSingular = { fllbck: 'Im a fallback' }
const { container } = render(
<TestEnglish
namespaces={{ ns: withSingular }}
i18nKey={i18nKey}
fallback={['ns:fllbck']}
/>
)
expect(console.warn).toBeCalledWith(expected)
expect(container.innerHTML).toContain('Im a fallback')
})
test('should log a warn key multiple times if all fallbacks are also missing', () => {
console.warn = jest.fn()
const i18nKey = 'ns:number'
const expected =
'[next-translate] "ns:number" is missing in current namespace configuration. Try adding "number" to the namespace "ns".'
const withSingular = { fallback4: 'Im a fallback number 4' }
const { container } = render(
<TestEnglish
namespaces={{ ns: withSingular }}
i18nKey={i18nKey}
fallback={[
'ns:fallback1',
'ns:fallback2',
'ns:fallback3',
'ns:fallback4',
]}
/>
)
expect(console.warn).toBeCalledWith(expected)
expect(console.warn.mock.calls.length).toBe(4)
expect(container.innerHTML).toContain('Im a fallback number 4')
})
test('should log correctly if the value includes a ":", for example an URL', () => {
console.warn = jest.fn()
const i18nKey = 'ns:https://linkinsomelanguage.com'
const expected =
'[next-translate] "ns:https://linkinsomelanguage.com" is missing in current namespace configuration. Try adding "https://linkinsomelanguage.com" to the namespace "ns".'
const withSingular = {}
render(
<TestEnglish namespaces={{ ns: withSingular }} i18nKey={i18nKey} />
)
expect(console.warn).toBeCalledWith(expected)
})
test('should not log when the translation have ":" inside', () => {
console.warn = jest.fn()
const i18nKey = 'Some text without namespace'
const expected =
'[next-translate] The text "Some text without namespace" has no namespace in front of it.'
const withSingular = {}
render(
<TestEnglish namespaces={{ ns: withSingular }} i18nKey={i18nKey} />
)
expect(console.warn).toBeCalledWith(expected)
})
test('should log a warn key if a nested key does not exist in the namespace', () => {
console.warn = jest.fn()
const i18nKey = 'ns:parent.child'
const expected =
'[next-translate] "ns:parent.child" is missing in current namespace configuration. Try adding "parent.child" to the namespace "ns".'
const withSingular = {}
render(
<TestEnglish namespaces={{ ns: withSingular }} i18nKey={i18nKey} />
)
expect(console.warn).toBeCalledWith(expected)
})
test('should pass the key and the namespace to the logger function if the key does not exist in the namespace', () => {
console.log = jest.fn()
const i18nKey = 'ns:number'
const logger = ({ i18nKey, namespace }) =>
console.log(`Logger: ${i18nKey} ${namespace}`)
const expected = 'Logger: number ns'
const withSingular = {}
render(
<TestEnglish
logger={logger}
namespaces={{ ns: withSingular }}
i18nKey={i18nKey}
/>
)
expect(console.log).toBeCalledWith(expected)
})
})
})