react-i18next icon indicating copy to clipboard operation
react-i18next copied to clipboard

Best way to make components unit testing rework with i18next

Open be-next-hotdog opened this issue 9 years ago • 7 comments

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,

be-next-hotdog avatar Feb 27 '17 10:02 be-next-hotdog

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);

jamuhl avatar Feb 27 '17 10:02 jamuhl

keeping this open for awhile...might be other users have other options/opinions...

jamuhl avatar Feb 27 '17 10:02 jamuhl

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 ;)

be-next-hotdog avatar Feb 27 '17 10:02 be-next-hotdog

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 avatar May 04 '17 13:05 trevoreyre

@TrevorEyre you're very welcome to contribute that....

jamuhl avatar May 04 '17 14:05 jamuhl

@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).

Stansbridge avatar May 09 '17 10:05 Stansbridge

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.

jamuhl avatar May 30 '17 07:05 jamuhl