import * as React from 'react'; import expect from 'expect'; import { render, fireEvent, waitFor, act, screen, } from '@testing-library/react'; import polyglotI18nProvider from 'ra-i18n-polyglot'; import { StoreContextProvider, memoryStore } from '../store'; import { useTranslate } from './useTranslate'; import { useSetLocale } from './useSetLocale'; import { I18nContextProvider } from './I18nContextProvider'; describe('useSetLocale', () => { const Component = () => { const translate = useTranslate(); const setLocale = useSetLocale(); return (
{translate('hello')}
); }; it('should not fail when used outside of a translation provider', () => { render(); expect(screen.queryAllByText('hello')).toHaveLength(1); }); it('should use the dataProvider.changeLocale function', async () => { const changeLocale = jest.fn().mockResolvedValue(); render( '', changeLocale, getLocale: () => 'de', }} > ); fireEvent.click(screen.getByText('Français')); await waitFor(() => { expect(changeLocale).toHaveBeenCalledTimes(1); }); }); it('should render the I18NcontextProvider children with the new locale', async () => { const i18nProvider = polyglotI18nProvider(locale => { if (locale === 'en') return { hello: 'hello' }; if (locale === 'fr') return { hello: 'bonjour' }; }); render( ); expect(screen.queryAllByText('hello')).toHaveLength(1); expect(screen.queryAllByText('bonjour')).toHaveLength(0); act(() => { fireEvent.click(screen.getByText('Français')); }); await waitFor(() => { expect(screen.queryAllByText('hello')).toHaveLength(0); expect(screen.queryAllByText('bonjour')).toHaveLength(1); }); }); });