-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
38 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import React from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
export default function CustomComponent() { | ||
const { t } = useTranslation(); | ||
|
||
return <div>{t('some.key', { some: 'variable' })}</div>; | ||
} |
30 changes: 30 additions & 0 deletions
30
example/test-jest/src/UseTranslationWithInterpolation.test.js
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,30 @@ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import UseTranslationWithInterpolation from './UseTranslationWithInterpolation'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
jest.mock('react-i18next', () => ({ | ||
useTranslation: jest.fn(), | ||
})); | ||
|
||
const tSpy = jest.fn((str) => str); | ||
const useTranslationSpy = useTranslation; | ||
|
||
useTranslationSpy.mockReturnValue({ | ||
t: tSpy, | ||
i18n: { | ||
changeLanguage: () => new Promise(() => {}), | ||
}, | ||
}); | ||
|
||
it('test render', () => { | ||
const mounted = mount(<UseTranslationWithInterpolation />); | ||
|
||
// console.log(mounted.debug()); | ||
expect(mounted.contains(<div>some.key</div>)).toBe(true); | ||
|
||
// If you want you can also check how the t function has been called, | ||
// but basically this is testing your mock and not the actual code. | ||
expect(tSpy).toHaveBeenCalledTimes(1); | ||
expect(tSpy).toHaveBeenLastCalledWith('some.key', { some: 'variable' }); | ||
}); |