react-localize-redux
react-localize-redux copied to clipboard
Unit test for the Translate Component in Demo
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
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();
});