import React from 'react'; import { render, screen } from '@testing-library/react'; import { useTranslate } from '../../hooks/useTranslate'; import { GrapesProvider } from './'; import { useLocale } from './hooks/useLocale'; import { useMapboxAccessToken } from './hooks/useMapboxAccessToken'; import { LOCALES } from './exampleLocales'; const exampleLocale = LOCALES.locales.en; describe('GrapesProvider', () => { it('exposes the value of the locale', () => { const ChildrenComponent = () => { const locale = useLocale(); return Locale: {locale}; }; render( , ); expect(screen.getByText('Locale: fr-FR')).toBeVisible(); }); it('exposes the value of the Mapbox token', () => { const ChildrenComponent = () => { const token = useMapboxAccessToken(); return Token: {token}; }; render( , ); expect(screen.getByText('Token: Mapbox')).toBeVisible(); }); it('accept locales to overwrite translation', () => { const ChildrenComponent = () => { const t = useTranslate(); return {t('cancel')}; }; render( , ); expect(screen.getByText('Wonderful cancel button')).toBeVisible(); }); it('accept function in locales key', () => { const ChildrenComponent = () => { const t = useTranslate(); return {t('cancel', { test: 'lala' })}; }; render( `${args?.test} cancel ${args?.test}`, }, }, }} > , ); expect(screen.getByText('lala cancel lala')).toBeVisible(); }); it('does a fallback to the default translation', () => { const ChildrenComponent = () => { const t = useTranslate(); return {t('cancel')}; }; render( , ); expect(screen.getByText('Cancel')).toBeVisible(); }); it('does not throw if translation cannot be found', () => { const ChildrenComponent = () => { const t = useTranslate(); return {t('cancel')}; }; render( , ); expect(screen.queryByText('Cancel default button')).not.toBeInTheDocument(); }); it('select the current locale ', () => { const ChildrenComponent = () => { const t = useTranslate(); return {t('cancel')}; }; render( , ); expect(screen.getByText('Cancel current button')).toBeVisible(); }); });