diff --git a/example/test-jest/src/UseTranslationWithInterpolation.js b/example/test-jest/src/UseTranslationWithInterpolation.js new file mode 100644 index 000000000..3eb9e2b9d --- /dev/null +++ b/example/test-jest/src/UseTranslationWithInterpolation.js @@ -0,0 +1,8 @@ +import React from 'react'; +import { useTranslation } from 'react-i18next'; + +export default function CustomComponent() { + const { t } = useTranslation(); + + return
{t('some.key', { some: 'variable' })}
; +} diff --git a/example/test-jest/src/UseTranslationWithInterpolation.test.js b/example/test-jest/src/UseTranslationWithInterpolation.test.js new file mode 100644 index 000000000..0c7a4c4be --- /dev/null +++ b/example/test-jest/src/UseTranslationWithInterpolation.test.js @@ -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(); + + // console.log(mounted.debug()); + expect(mounted.contains(
some.key
)).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' }); +});