Best way to make components unit testing rework with i18next
Hello, guys,
As sometimes we export just one element with wrapper HOC i18next by default for each component, therefore, we have to export separately the aim component to test and the HOC translate(...) instead of making a default export in order to make unit testing rework ?
export default translate(...) => export real component and HOC translate(...)
Or a simpler way to do that ?
Thx a lot,
there might be other options like mocking that stuff....but that's what we do independent on what HOC's are applied...we test the pure component by passing different props to it
so yes:
export myComponent;
export default translate('ns')(myComponent);
keeping this open for awhile...might be other users have other options/opinions...
Anyway, this would create nothing to block the testing practices, we could also restructure the scripts in isolating the i18next HOC, such as merely writing export default translate(FooComponent) in index.js(x) and put the real FooComponent codes in FooComponent.jsx, which makes rework all the same.
I was just looking for a concise way to test with i18next HOC, for example maybe add directly a test mocking suite in i18next lib ;)
Testing the undecorated component is the way to go in most cases. However, in my project we have a lot of larger components composed of other decorated components. To test these kinds of components, a simple mock for the I18NextProvider would be a very welcome addition.
@TrevorEyre you're very welcome to contribute that....
@TrevorEyre @be-next-hotdog We've been using react-stubber (https://github.com/kadirahq/react-stubber) and testing our undecorated components where possible.
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>);
});
};
We execute the stubInterpolate function along with our other stubs where required (test setup, storybooks).
closing for now....there are some samples in the test folder. And topic of testing pure not extended components was discussed. Will add some summary of this to the readme.