forked from QuiiBz/next-international
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add I18nProvider tests (QuiiBz#16)
* test: add I18nProvider tests * feat: add coverage * test: add test for useI18n not inside I18nProvider * test: enable getLocaleStaticProps test file * test: add test for not defined locale within getLocaleStaticProps
- Loading branch information
Showing
9 changed files
with
395 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ node_modules/ | |
.next/ | ||
out/ | ||
dist/ | ||
coverage/ | ||
|
||
.DS_Store | ||
*.log* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
packages/next-international/__tests__/get-locale-static-props.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { describe, expect, it, vi } from 'vitest'; | ||
import { createI18n } from '../src'; | ||
import en from './utils/en'; | ||
|
||
describe('getLocaleStaticProps', () => { | ||
it('should error if locale is not defined', async () => { | ||
const spy = vi.spyOn(console, 'error').mockImplementation(() => null); | ||
const { getLocaleStaticProps } = createI18n<typeof import('./utils/en').default>({ | ||
en: () => import('./utils/en'), | ||
fr: () => import('./utils/fr'), | ||
}); | ||
|
||
const props = await getLocaleStaticProps()({ | ||
locales: ['en', 'fr'], | ||
}); | ||
|
||
expect(props).toEqual({ | ||
props: {}, | ||
}); | ||
expect(console.error).toHaveBeenCalledWith( | ||
"[next-international] 'i18n.defaultLocale' not defined in 'next.config.js'", | ||
); | ||
spy.mockReset(); | ||
}); | ||
|
||
it('should return default locale', async () => { | ||
const { getLocaleStaticProps } = createI18n<typeof import('./utils/en').default>({ | ||
en: () => import('./utils/en'), | ||
fr: () => import('./utils/fr'), | ||
}); | ||
|
||
const props = await getLocaleStaticProps()({ | ||
locale: 'en', | ||
defaultLocale: 'en', | ||
locales: ['en', 'fr'], | ||
}); | ||
|
||
expect(props).toEqual({ | ||
props: { | ||
locale: en, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should return default locale with existing getStaticProps', async () => { | ||
const { getLocaleStaticProps } = createI18n<typeof import('./utils/en').default>({ | ||
en: () => import('./utils/en'), | ||
fr: () => import('./utils/fr'), | ||
}); | ||
|
||
const props = await getLocaleStaticProps(() => ({ | ||
props: { | ||
hello: 'world', | ||
}, | ||
}))({ | ||
locale: 'en', | ||
defaultLocale: 'en', | ||
locales: ['en', 'fr'], | ||
}); | ||
|
||
expect(props).toEqual({ | ||
props: { | ||
hello: 'world', | ||
locale: en, | ||
}, | ||
}); | ||
}); | ||
}); |
52 changes: 0 additions & 52 deletions
52
packages/next-international/__tests__/get-locale-static-props.ts
This file was deleted.
Oops, something went wrong.
129 changes: 129 additions & 0 deletions
129
packages/next-international/__tests__/i18n-provider.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import React from 'react'; | ||
import { describe, vi } from 'vitest'; | ||
import { createI18n } from '../src'; | ||
import { render, screen, waitFor } from './utils'; | ||
import en from './utils/en'; | ||
|
||
beforeEach(() => { | ||
vi.mock('next/router', () => ({ | ||
useRouter: vi.fn().mockImplementation(() => ({ | ||
locale: 'en', | ||
defaultLocale: 'en', | ||
locales: ['en', 'fr'], | ||
})), | ||
})); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe('I18nProvider', () => { | ||
describe('fallback', () => { | ||
it('should return the fallback while loading the locale', async () => { | ||
const { useI18n, I18nProvider } = createI18n<typeof import('./utils/en').default>({ | ||
en: () => import('./utils/en'), | ||
fr: () => import('./utils/fr'), | ||
}); | ||
|
||
function App() { | ||
const { t } = useI18n(); | ||
|
||
return <p>{t('hello')}</p>; | ||
} | ||
|
||
render( | ||
// @ts-expect-error we don't provide `locale` to test the fallback | ||
<I18nProvider fallback={<p>Loading...</p>}> | ||
<App /> | ||
</I18nProvider>, | ||
); | ||
|
||
expect(screen.getByText('Loading...')).toBeInTheDocument(); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('Hello')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('should return null if no fallback provided', async () => { | ||
const { useI18n, I18nProvider } = createI18n<typeof import('./utils/en').default>({ | ||
en: () => import('./utils/en'), | ||
fr: () => import('./utils/fr'), | ||
}); | ||
|
||
function App() { | ||
const { t } = useI18n(); | ||
|
||
return <p>{t('hello')}</p>; | ||
} | ||
|
||
render( | ||
// @ts-expect-error we don't provide `locale` to test the fallback | ||
<I18nProvider> | ||
<App /> | ||
</I18nProvider>, | ||
); | ||
|
||
expect(screen.queryByText('Hello')).not.toBeInTheDocument(); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('Hello')).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('config', () => { | ||
it('should warn about mismatching locales in createI18n', () => { | ||
const spy = vi.spyOn(console, 'warn').mockImplementation(() => null); | ||
|
||
const { useI18n, I18nProvider } = createI18n<typeof import('./utils/en').default>({ | ||
en: () => import('./utils/en'), | ||
fr: () => import('./utils/fr'), | ||
es: () => import('./utils/en'), | ||
}); | ||
|
||
function App() { | ||
const { t } = useI18n(); | ||
|
||
return <p>{t('hello')}</p>; | ||
} | ||
|
||
render( | ||
<I18nProvider locale={en}> | ||
<App /> | ||
</I18nProvider>, | ||
); | ||
|
||
expect(console.warn).toHaveBeenCalledWith( | ||
"[next-international] The following locales are defined in 'createI18n' but not in 'next.config.js': es", | ||
); | ||
spy.mockClear(); | ||
}); | ||
|
||
it('should warn about mismatching locales in next.config.js', () => { | ||
const spy = vi.spyOn(console, 'warn').mockImplementation(() => null); | ||
|
||
const { useI18n, I18nProvider } = createI18n<typeof import('./utils/en').default>({ | ||
en: () => import('./utils/en'), | ||
}); | ||
|
||
function App() { | ||
const { t } = useI18n(); | ||
|
||
return <p>{t('hello')}</p>; | ||
} | ||
|
||
render( | ||
<I18nProvider locale={en}> | ||
<App /> | ||
</I18nProvider>, | ||
); | ||
|
||
expect(console.warn).toHaveBeenCalledWith( | ||
"[next-international] The following locales are defined in 'next.config.js' but not in 'createI18n': fr", | ||
); | ||
spy.mockClear(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { createI18n } from './i18n/create-i18n'; | ||
/* c8 ignore next */ | ||
export { BaseLocale, LocaleValue } from './types'; |
Oops, something went wrong.