Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test for the Translate Component in Demo #200

Open
nikitapandya opened this issue Apr 1, 2020 · 1 comment
Open

Unit test for the Translate Component in Demo #200

nikitapandya opened this issue Apr 1, 2020 · 1 comment

Comments

@nikitapandya
Copy link

nikitapandya commented Apr 1, 2020

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
Copy link
Author

nikitapandya commented Jun 8, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant