-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
useSetLocale.spec.js
70 lines (64 loc) · 2.45 KB
/
useSetLocale.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
import React from 'react';
import expect from 'expect';
import { fireEvent, cleanup, wait, act } from '@testing-library/react';
import polyglotI18nProvider from 'ra-i18n-polyglot';
import useTranslate from './useTranslate';
import useSetLocale from './useSetLocale';
import { TranslationContext, TranslationProvider } from './';
import { renderWithRedux } from '../util';
describe('useSetLocale', () => {
afterEach(cleanup);
const Component = () => {
const translate = useTranslate();
const setLocale = useSetLocale();
return (
<div>
{translate('hello')}
<button onClick={() => setLocale('fr')}>Français</button>
</div>
);
};
it('should not fail when used outside of a translation provider', () => {
const { queryAllByText } = renderWithRedux(<Component />);
expect(queryAllByText('hello')).toHaveLength(1);
});
it('should use the setLocale function set in the translation context', async () => {
const setLocale = jest.fn();
const { getByText } = renderWithRedux(
<TranslationContext.Provider
value={{
i18nProvider: {
translate: () => '',
changeLocale: () => Promise.resolve(),
},
locale: 'de',
setLocale,
}}
>
<Component />
</TranslationContext.Provider>
);
fireEvent.click(getByText('Français'));
await wait();
expect(setLocale).toHaveBeenCalledTimes(1);
});
it('should use the i18n provider when using TranslationProvider', async () => {
const i18nProvider = polyglotI18nProvider(locale => {
if (locale === 'en') return { hello: 'hello' };
if (locale === 'fr') return { hello: 'bonjour' };
});
const { getByText, queryAllByText } = renderWithRedux(
<TranslationProvider locale="en" i18nProvider={i18nProvider}>
<Component />
</TranslationProvider>
);
expect(queryAllByText('hello')).toHaveLength(1);
expect(queryAllByText('bonjour')).toHaveLength(0);
act(() => {
fireEvent.click(getByText('Français'));
});
await wait();
expect(queryAllByText('hello')).toHaveLength(0);
expect(queryAllByText('bonjour')).toHaveLength(1);
});
});