-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathuseTranslation.loadingLng.spec.js
95 lines (79 loc) · 3.19 KB
/
useTranslation.loadingLng.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
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
import { describe, it, vitest, beforeEach, afterEach, expect } from 'vitest';
import { renderHook, cleanup, waitFor } from '@testing-library/react';
import i18n from './i18n';
import { BackendLngAwareMock } from './backendLngAwareMock';
import { useTranslation } from '../src/useTranslation';
vitest.unmock('../src/useTranslation');
describe('useTranslation loading ns with lng via props', () => {
let newI18n;
/** @type {BackendLngAwareMock} */
let backend;
beforeEach(() => {
newI18n = i18n.createInstance();
backend = new BackendLngAwareMock();
newI18n.use(backend).init({
lng: 'en',
fallbackLng: 'en',
});
});
afterEach(cleanup);
it('should wait for correct translation with suspense', async () => {
const all = [];
const { result } = renderHook(() => {
const value = useTranslation('common', { i18n: newI18n, useSuspense: true, lng: 'de' });
all.push(value);
return value;
});
expect(all).toHaveLength(0);
backend.flush();
await waitFor(() => expect(result.current.t('key1')).toBe('de/common for key1'));
});
it('should wait for correct translation without suspense', async () => {
const { result } = renderHook(() =>
useTranslation('common', { i18n: newI18n, useSuspense: false, lng: 'it' }),
);
const { t } = result.current;
expect(t('key1')).toBe('key1');
backend.flush();
expect(t('key1')).toBe('it/common for key1');
});
it('should return defaultValue if resources not yet loaded', async () => {
const { result } = renderHook(() =>
useTranslation('common', { i18n: newI18n, useSuspense: false, lng: 'fr' }),
);
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');
backend.flush({ language: 'en' });
expect(t('key1', 'my default value')).toBe('en/common for key1');
expect(t('key1', { defaultValue: 'my default value' })).toBe('en/common for key1');
backend.flush({ language: 'fr' });
expect(t('key1', 'my default value')).toBe('fr/common for key1');
expect(t('key1', { defaultValue: 'my default value' })).toBe('fr/common for key1');
});
it('should correctly return and render correct translations in multiple useTranslation usages', async () => {
{
const { result } = renderHook(() =>
useTranslation('newns', { i18n: newI18n, useSuspense: true, lng: 'pt' }),
);
backend.flush();
await waitFor(() => expect(result.current.t('key1')).toBe('pt/newns for key1'));
}
{
const { result } = renderHook(() =>
useTranslation('newns', { i18n: newI18n, useSuspense: true, lng: 'de' }),
);
backend.flush({ language: 'de' });
await waitFor(() => expect(result.current.t('key1')).toBe('de/newns for key1'));
}
{
const { result } = renderHook(() =>
useTranslation('newns', { i18n: newI18n, useSuspense: true, lng: 'pt' }),
);
// backend.flush({ language: 'pt' }); // already loaded
// await retPT.waitForNextUpdate(); // already loaded
const { t } = result.current;
expect(t('key1')).toBe('pt/newns for key1');
}
});
});