Testing
export MyComponent;
export default withTranslation('ns')(MyComponent);import { MyComponent } from './myComponent';
<MyComponent t={key => key} />const tDefault = (key) => key;
const StubbableInterpolate = mayBeStubbed(Interpolate);
const stubInterpolate = function () {
stub(StubbableInterpolate, (props, context) => {
const t = (context && context.t) || tDefault;
return (<span>{t(props.i18nKey)}</span>);
});
};const tDefault = (key) => key;
const StubbableInterpolate = mayBeStubbed(Interpolate);
const stubInterpolate = function () {
stub(StubbableInterpolate, (props, context) => {
const t = (context && context.t) || tDefault;
return (<span>{t($ => $[props.i18nKey])}</span>);
});
};jest.mock('react-i18next', () => ({
// this mock makes sure any components using the translate HoC receive the t function as a prop
withTranslation: () => Component => {
Component.defaultProps = { ...Component.defaultProps, t: (i18nKey) => i18nKey };
// or with TypeScript:
//Component.defaultProps = { ...Component.defaultProps, t: (i18nKey: string) => i18nKey };
return Component;
},
}));Testing without stubbing
Example configuration for testing
Example test using this configuration
Last updated