|
| 1 | +import React from 'react'; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { createI18n } from '../src'; |
| 4 | +import { render, screen } from './utils'; |
| 5 | +import en from './utils/en'; |
| 6 | +import fr from './utils/fr'; |
| 7 | + |
| 8 | +beforeEach(() => { |
| 9 | + vi.mock('next/router', () => ({ |
| 10 | + useRouter: vi.fn().mockImplementation(() => ({ |
| 11 | + locale: 'fr', |
| 12 | + defaultLocale: 'fr', |
| 13 | + locales: ['en', 'fr'], |
| 14 | + })), |
| 15 | + })); |
| 16 | +}); |
| 17 | + |
| 18 | +afterEach(() => { |
| 19 | + vi.clearAllMocks(); |
| 20 | +}); |
| 21 | + |
| 22 | +describe('fallbackLocale', () => { |
| 23 | + it('should output the key when no fallback locale is configured', async () => { |
| 24 | + const { useI18n, I18nProvider } = createI18n<typeof import('./utils/en').default>({ |
| 25 | + en: () => import('./utils/en'), |
| 26 | + fr: () => import('./utils/fr'), |
| 27 | + }); |
| 28 | + |
| 29 | + function App() { |
| 30 | + const { t } = useI18n(); |
| 31 | + |
| 32 | + return <p>{t('only.exists.in.en')}</p>; |
| 33 | + } |
| 34 | + |
| 35 | + render( |
| 36 | + // @ts-expect-error missing key |
| 37 | + <I18nProvider locale={fr}> |
| 38 | + <App /> |
| 39 | + </I18nProvider>, |
| 40 | + ); |
| 41 | + |
| 42 | + expect(screen.getByText('only.exists.in.en')).toBeInTheDocument(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should output the key when no fallback locale is configured', async () => { |
| 46 | + const { useI18n, I18nProvider } = createI18n<typeof import('./utils/en').default>({ |
| 47 | + en: () => import('./utils/en'), |
| 48 | + fr: () => import('./utils/fr'), |
| 49 | + }); |
| 50 | + |
| 51 | + function App() { |
| 52 | + const { t } = useI18n(); |
| 53 | + |
| 54 | + return <p>{t('only.exists.in.en')}</p>; |
| 55 | + } |
| 56 | + |
| 57 | + render( |
| 58 | + // @ts-expect-error missing key |
| 59 | + <I18nProvider locale={fr} fallbackLocale={en}> |
| 60 | + <App /> |
| 61 | + </I18nProvider>, |
| 62 | + ); |
| 63 | + |
| 64 | + expect(screen.getByText('EN locale')).toBeInTheDocument(); |
| 65 | + }); |
| 66 | +}); |
0 commit comments