From 21b0cab3d207879258ec8217cf83930fd739ac2d Mon Sep 17 00:00:00 2001 From: Tom Lienard Date: Fri, 29 Jul 2022 18:57:03 +0200 Subject: [PATCH] test: useI18n 100% coverage (#17) --- .../__tests__/use-i18n.test.tsx | 69 +++++++++++++++++-- .../src/i18n/create-i18n-provider.tsx | 4 +- 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/packages/next-international/__tests__/use-i18n.test.tsx b/packages/next-international/__tests__/use-i18n.test.tsx index b129813..7d9cdfc 100644 --- a/packages/next-international/__tests__/use-i18n.test.tsx +++ b/packages/next-international/__tests__/use-i18n.test.tsx @@ -6,11 +6,20 @@ import en from './utils/en'; beforeEach(() => { vi.mock('next/router', () => ({ - useRouter: vi.fn().mockImplementation(() => ({ - locale: 'en', - defaultLocale: 'en', - locales: ['en', 'fr'], - })), + useRouter: vi + .fn() + .mockImplementationOnce(() => ({ + locales: ['en', 'fr'], + })) + .mockImplementationOnce(() => ({ + locale: 'en', + defaultLocale: 'en', + })) + .mockImplementation(() => ({ + locale: 'en', + defaultLocale: 'en', + locales: ['en', 'fr'], + })), })); }); @@ -19,7 +28,55 @@ afterEach(() => { }); describe('useI18n', () => { - it('should thrown if not used inside I18nProvider', () => { + it('should log error if locale not set in next.config.js', () => { + const spy = vi.spyOn(console, 'error').mockImplementation(() => null); + const { useI18n, I18nProvider } = createI18n({ + en: () => import('./utils/en'), + fr: () => import('./utils/fr'), + }); + + function App() { + const { t } = useI18n(); + + return

{t('hello')}

; + } + + render( + + + , + ); + expect(console.error).toHaveBeenCalledWith( + "[next-international] 'i18n.defaultLocale' not defined in 'next.config.js'", + ); + + spy.mockReset(); + }); + + it('should log error if locales not set in next.config.js', () => { + const spy = vi.spyOn(console, 'error').mockImplementation(() => null); + const { useI18n, I18nProvider } = createI18n({ + en: () => import('./utils/en'), + fr: () => import('./utils/fr'), + }); + + function App() { + const { t } = useI18n(); + + return

{t('hello')}

; + } + + render( + + + , + ); + expect(console.error).toHaveBeenCalledWith("[next-international] 'i18n.locales' not defined in 'next.config.js'"); + + spy.mockReset(); + }); + + it('should throw if not used inside I18nProvider', () => { const { useI18n } = createI18n({ en: () => import('./utils/en'), fr: () => import('./utils/fr'), diff --git a/packages/next-international/src/i18n/create-i18n-provider.tsx b/packages/next-international/src/i18n/create-i18n-provider.tsx index ebce46d..01836bb 100644 --- a/packages/next-international/src/i18n/create-i18n-provider.tsx +++ b/packages/next-international/src/i18n/create-i18n-provider.tsx @@ -46,9 +46,7 @@ export function createI18nProvider( return; } - const load = locales[locale] || locales[defaultLocale]; - - load().then(content => { + locales[locale]().then(content => { setClientLocale(content.default as Locale); }); }, [locale, defaultLocale]);