-
-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Add typeof window check to createConfig (#976)
- Loading branch information
1 parent
0cea3c1
commit d75f874
Showing
5 changed files
with
81 additions
and
9 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
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,67 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
|
||
import React from 'react' | ||
import fs from 'fs' | ||
import { I18nextProvider } from 'react-i18next' | ||
import { renderToString } from 'react-dom/server' | ||
|
||
import { appWithTranslation } from './appWithTranslation' | ||
|
||
jest.mock('fs', () => ({ | ||
existsSync: jest.fn(), | ||
readdirSync: jest.fn(), | ||
})) | ||
|
||
const DummyI18nextProvider = ({ children }) => ( | ||
<>{children}</> | ||
) | ||
|
||
jest.mock('react-i18next', () => ({ | ||
I18nextProvider: jest.fn(), | ||
__esmodule: true, | ||
})) | ||
|
||
|
||
const DummyApp = appWithTranslation(() => ( | ||
<div>Hello world</div> | ||
)) | ||
|
||
const renderComponent = () => | ||
renderToString( | ||
<DummyApp | ||
pageProps={{ | ||
_nextI18Next: { | ||
initialLocale: 'en', | ||
}, | ||
}} | ||
/>, | ||
) | ||
|
||
describe('appWithTranslation', () => { | ||
beforeEach(() => { | ||
(fs.existsSync as jest.Mock).mockReturnValue(true); | ||
(fs.readdirSync as jest.Mock).mockReturnValue([]); | ||
(I18nextProvider as jest.Mock).mockImplementation(DummyI18nextProvider) | ||
}) | ||
afterEach(jest.resetAllMocks) | ||
|
||
|
||
it('returns an I18nextProvider', () => { | ||
renderComponent() | ||
expect(I18nextProvider).toHaveBeenCalledTimes(1) | ||
|
||
const [args] = (I18nextProvider as jest.Mock).mock.calls | ||
|
||
expect(I18nextProvider).toHaveBeenCalledTimes(1) | ||
expect(args).toHaveLength(3) | ||
expect(args[0].children).toBeTruthy() | ||
expect(args[0].i18n.addResource).toBeTruthy() | ||
expect(args[0].i18n.language).toEqual('en') | ||
expect(args[0].i18n.isInitialized).toEqual(true) | ||
|
||
expect(fs.existsSync).toHaveBeenCalledTimes(1) | ||
expect(fs.readdirSync).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
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
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