-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathcreateServerInstance.spec.tsx
More file actions
60 lines (48 loc) · 2.25 KB
/
Copy pathcreateServerInstance.spec.tsx
File metadata and controls
60 lines (48 loc) · 2.25 KB
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
import { TolgeeInstance } from '@tolgee/web';
import { createServerInstance } from './createServerInstance';
jest.mock('react', () => ({
...jest.requireActual('react'),
cache: (callback: unknown) => callback,
}));
describe('createServerInstance', () => {
const createTolgeeMock = () => {
const run = jest.fn().mockResolvedValue(undefined);
const t = jest.fn();
const tolgee = { run, t } as unknown as TolgeeInstance;
const createTolgee = jest.fn().mockResolvedValue(tolgee);
return { createTolgee, run, t, tolgee };
};
it('uses the configured locale resolver by default', async () => {
const { createTolgee, run, tolgee } = createTolgeeMock();
const getLocale = jest.fn().mockResolvedValue('en');
const { getTolgee } = createServerInstance({ createTolgee, getLocale });
await expect(getTolgee()).resolves.toBe(tolgee);
expect(getLocale).toHaveBeenCalledTimes(1);
expect(createTolgee).toHaveBeenCalledWith('en');
expect(run).toHaveBeenCalledTimes(1);
});
it('supports an explicit locale parameter object', async () => {
const { createTolgee, tolgee } = createTolgeeMock();
const getLocale = jest.fn().mockResolvedValue('en');
const { getTolgee } = createServerInstance({ createTolgee, getLocale });
await expect(getTolgee({ locale: 'cs' })).resolves.toBe(tolgee);
expect(getLocale).not.toHaveBeenCalled();
expect(createTolgee).toHaveBeenCalledWith('cs');
});
it('passes an explicit locale parameter object to translations', async () => {
const { createTolgee, t } = createTolgeeMock();
const getLocale = jest.fn().mockResolvedValue('en');
const { getTranslate } = createServerInstance({ createTolgee, getLocale });
await expect(getTranslate({ locale: 'cs' })).resolves.toBe(t);
expect(getLocale).not.toHaveBeenCalled();
expect(createTolgee).toHaveBeenCalledWith('cs');
});
it('supports an explicit locale in the server T component', async () => {
const { createTolgee } = createTolgeeMock();
const getLocale = jest.fn().mockResolvedValue('en');
const { T } = createServerInstance({ createTolgee, getLocale });
await T({ keyName: 'hello', locale: 'cs' });
expect(getLocale).not.toHaveBeenCalled();
expect(createTolgee).toHaveBeenCalledWith('cs');
});
});