forked from i18next/react-i18next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseTranslation.loading.spec.js
55 lines (48 loc) · 1.67 KB
/
useTranslation.loading.spec.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
import { renderHook } from '@testing-library/react-hooks';
import i18n from './i18n';
import BackendMock from './backendMock';
import { useTranslation } from '../src/useTranslation';
jest.unmock('../src/useTranslation');
describe('useTranslation loading ns', () => {
let newI18n;
let backend;
beforeEach(() => {
newI18n = i18n.createInstance();
backend = new BackendMock();
newI18n.use(backend).init({
lng: 'en',
fallbackLng: 'en',
interpolation: {
escapeValue: false, // not needed for react!!
},
});
});
it('should wait for correct translation with suspense', async () => {
const { result, waitForNextUpdate } = renderHook(() =>
useTranslation('common', { i18n: newI18n, useSuspense: true }),
);
expect(result.all).toHaveLength(0);
backend.flush();
await waitForNextUpdate();
const { t } = result.current;
expect(t('key1')).toBe('test');
});
it('should wait for correct translation without suspense', async () => {
const { result, waitForNextUpdate } = renderHook(() =>
useTranslation('common', { i18n: newI18n, useSuspense: false }),
);
const { t } = result.current;
expect(t('key1')).toBe('key1');
backend.flush();
await waitForNextUpdate();
expect(t('key1')).toBe('test');
});
it('should return defaultValue if i18n instance not found', async () => {
const { result } = renderHook(() =>
useTranslation('common', { i18n: newI18n, useSuspense: false }),
);
const { t } = result.current;
expect(t('key1', 'my default value')).toBe('my default value');
expect(t('key1', { defaultValue: 'my default value' })).toBe('my default value');
});
});