Skip to content

Commit

Permalink
additional jest usage
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Aug 25, 2022
1 parent ad80f5c commit db9ba28
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions example/test-jest/src/UseTranslationWithInterpolation.js
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 example/test-jest/src/UseTranslationWithInterpolation.test.js
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' });
});

0 comments on commit db9ba28

Please sign in to comment.