-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathuseTranslation.spec.jsx
220 lines (184 loc) · 6.8 KB
/
useTranslation.spec.jsx
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
import { describe, it, vitest, expect, beforeAll, afterAll, afterEach } from 'vitest';
import React from 'react';
import { renderHook, cleanup } from '@testing-library/react';
import i18nInstance from './i18n';
import { useTranslation } from '../src/useTranslation';
import { setI18n } from '../src/context';
import { I18nextProvider } from '../src/I18nextProvider';
vitest.unmock('../src/useTranslation');
vitest.unmock('../src/I18nextProvider');
describe('useTranslation', () => {
afterEach(() => {
cleanup();
});
describe('object', () => {
it('should render correct content', () => {
const { result } = renderHook(() => useTranslation('translation', { i18n: i18nInstance }));
const { t, i18n } = result.current;
expect(t('key1')).toBe('test');
expect(i18nInstance).toBe(i18n);
});
});
describe('proper object-equality of returned t function', () => {
it('should refresh t upon i18n.changeLanguage', () => {
const { result, rerender } = renderHook(() =>
useTranslation('translation', { i18n: i18nInstance }),
);
const { i18n, t } = result.current;
expect(i18n.language).toBe('en');
i18n.changeLanguage('fr');
try {
rerender();
const { t: refreshedT } = result.current;
expect(refreshedT).not.toBe(t);
} finally {
i18n.changeLanguage('en');
}
});
});
describe('array', () => {
it('should render correct content', () => {
const { result } = renderHook(() => useTranslation('translation', { i18n: i18nInstance }));
const [t, i18n] = result.current;
expect(t('key1')).toBe('test');
expect(i18n).toBe(i18nInstance);
});
});
describe('without i18next instance', () => {
beforeAll(() => {
setI18n(undefined);
});
afterAll(() => {
setI18n(i18nInstance);
});
describe('handling gracefully', () => {
it('should render content fallback', () => {
console.warn = vitest.fn();
const { result } = renderHook(() => useTranslation('translation', { i18n: undefined }));
const { t, i18n } = result.current;
expect(t('key1')).toBe('key1');
expect(t(['doh', 'Human friendly fallback'])).toBe('Human friendly fallback');
expect(i18n).toEqual({});
expect(console.warn).toHaveBeenCalled();
});
});
});
describe('few namespaces', () => {
it('hook destructured values are expected types', () => {
const { result } = renderHook(() =>
useTranslation(['other', 'translation'], { i18n: i18nInstance }),
);
const { t, i18n } = result.current;
expect(typeof t).toBe('function');
expect(i18n).toEqual(i18nInstance);
expect(t('key1')).toEqual('key1');
});
describe('fallback mode', () => {
beforeAll(() => {
i18nInstance.options.react.nsMode = 'fallback';
});
afterAll(() => {
delete i18nInstance.options.react.nsMode;
});
it('should render correct content', () => {
const { result } = renderHook(() =>
useTranslation(['other', 'translation'], { i18n: i18nInstance }),
);
const { t } = result.current;
expect(t('key1')).toBe('test');
});
});
it('should render content fallback', () => {
const { result } = renderHook(() =>
useTranslation(['other', 'translation'], { i18n: i18nInstance }),
);
const { t } = result.current;
expect(t('key1')).toBe('key1');
});
});
describe('default namespace from context', () => {
afterEach(() => {
i18nInstance.reportNamespaces.usedNamespaces = {};
});
const namespace = 'sampleNS';
const wrapper = ({ children }) => (
<I18nextProvider defaultNS={namespace} i18={i18nInstance}>
{children}
</I18nextProvider>
);
it('should render content fallback', () => {
const { result } = renderHook(() => useTranslation(), { wrapper });
const { t } = result.current;
expect(t('key1')).toBe('key1');
expect(i18nInstance.reportNamespaces.getUsedNamespaces()).toContain(namespace);
});
});
describe('key prefix', () => {
i18nInstance.addResource('en', 'translation', 'deeply.nested_a.key', 'here_a!');
i18nInstance.addResource('en', 'translation', 'deeply.nested_b.key', 'here_b!');
it('should apply keyPrefix and reset it once changed', () => {
let keyPrefix = 'deeply.nested_a';
const { result, rerender } = renderHook(() =>
useTranslation('translation', { i18n: i18nInstance, keyPrefix }),
);
const { t: t1 } = result.current;
expect(t1('key')).toBe('here_a!');
expect(t1.keyPrefix).toBe('deeply.nested_a');
keyPrefix = 'deeply.nested_b';
rerender();
const { t: t2 } = result.current;
expect(t2('key')).toBe('here_b!');
expect(t2.keyPrefix).toBe('deeply.nested_b');
});
});
describe('replacing i18n instance in provider', () => {
i18nInstance.addResource('fr', 'translation', 'key1', 'test2');
const i18nInstanceClone = i18nInstance.cloneInstance({ lng: 'fr' });
const wrapper = ({ children }) => (
<I18nextProvider i18n={children?.props?.renderCallbackProps.i18n}>{children}</I18nextProvider>
);
it('should render correct content', async () => {
const { result, rerender } = renderHook(() => useTranslation(), {
wrapper,
initialProps: { i18n: i18nInstance },
});
expect(result.current.t('key1')).toBe('test');
rerender({ i18n: i18nInstanceClone });
expect(result.current.t('key1')).toBe('test2');
});
});
describe('with lng prop', () => {
i18nInstance.addResource('en', 'translation', 'myKey', 'second test');
i18nInstance.addResource('fr', 'translation', 'myKey', 'deuxième essai');
i18nInstance.addResource('it', 'translation', 'myKey', 'secondo test');
const wrapper = ({ children, i18n }) => (
<I18nextProvider i18n={i18n}>{children}</I18nextProvider>
);
it('should render correct content', () => {
const { result: resultNoLng } = renderHook(() => useTranslation('translation'), {
wrapper,
initialProps: {
i18n: i18nInstance,
},
});
const { t: t1 } = resultNoLng.current;
expect(t1('myKey')).toBe('second test');
const { result: resultIt } = renderHook(() => useTranslation('translation', { lng: 'it' }), {
wrapper,
initialProps: {
i18n: i18nInstance,
},
});
const { t: t2 } = resultIt.current;
expect(t2('myKey')).toBe('secondo test');
const { result: resultFr } = renderHook(() => useTranslation('translation', { lng: 'fr' }), {
wrapper,
initialProps: {
i18n: i18nInstance,
},
});
const { t: t3 } = resultFr.current;
expect(t3('myKey')).toBe('deuxième essai');
});
});
});