ryandrewjohnson / react-localize-redux

Dead simple localization for your React components
https://ryandrewjohnson.github.io/react-localize-redux-docs/
MIT License
374 stars 88 forks source link

Unit test for the Translate Component in Demo #200

Open nikitapandya opened 4 years ago

nikitapandya commented 4 years ago

Do you want to request a feature or report a bug? (Request an Example)

Question regarding how you can unit test the translate component when its being called for example in the demo files Books.js or Movies.js.

Current Behavior:

I am looking at your unit test for the actual Translate component in Translate.test.js, but am unable to use these tests when I am testing the Translate component in my file similar to Books.js.

Would you be able to show an example of a unit test (for testing Translate) for the demo (Book.js/Movies.js)

For example: I am using Jest and React Testing Library and calling the following <Translate data-testid="test-id1" id={movie${item}.title} /> However, Jest is "Unable to find an element by: [data-testid="test-id1"]" How do you recommend unit testing our Translations within the components?

Which versions of react and react-localize-redux are you using? React 16.9.23 react-localize-redux 3.5.3

nikitapandya commented 4 years ago
This is how I wrote unit tests for testing the <Translate /> component. Using redux and typescript: 

//component file 

class MyModal extends React.Component<MyModal> {
    constructor(props: MyModal) {
        super(props);
    }

    public render() {
       return (
          <p><Translate id=Modal.title.text/></p>
       );
    }

//Unit test file 

export default function renderWithRedux(
    ui: any,
    { store = createStore(reducers, initialState) } = {}
) {
    let component: any = (
        <Provider store={store}>
            <LocalizeProvider store={store}>
                {ui}
            </LocalizeProvider>
        </Provider>);
    return {
        ...render(component),
        store,
    }
}

    test('Testing English Translations', () => {
        //this is a store that can never be changed
        const store = createStore(() => ({
            ...initialState,
            localize: {
                languages: [
                    { name: 'English', code: 'en', active: true },
                    { name: 'Français', code: 'fr', active: false },
                ],
                options: { renderToStaticMarkup, defaultLanguage: 'en', onMissingTranslation() { return "MISSING TRANSLATION" } },
                translations: {
                    "Modal.title.text": ['example Modal title', "example Modal title FR"],
                }
            }
        }))
        const { getByText } = renderWithRedux(<MyModal  {...defaultProps} />, {
            store,
        })

        expect(getByText('example Modal title')).toBeInTheDocument();
    });