React-testing-library: How to test i18next

Created on 22 Jun 2019  ยท  11Comments  ยท  Source: testing-library/react-testing-library

I was wondering how to test the NEXT text here:

test('LayoutFooter text content', () => {
  const props = {
    handleOnClick: () => {},
  };

  const { container } = render(<LayoutFooter {...props} />);
  const fc = container.firstChild;
  expect(fc.textContent).toBe('NEXT');
});

Most helpful comment

We should probably have an article about this on the website.

All 11 comments

Approaches to testing translated components vary, but these are popular:

  • Use default strings Most internationalization frameworks will fall back to the default string if the locale data is not provided. You can use the default text as if it was hardcoded into the page, with methods like getByText, getByLabelText, etc.
  • Include locale providers In your tests, you can also include the full setup code necessary to actually do the translation with locale data. For example, you might set up a wrapper in a custom render method that includes a Context provider for the locale messages. Then you can query by the expected translated result.
  • Mock components You can also mock translation components (e.g. <FormattedMessage /> in react-intl) to do something like add a data-i18n attribute to a span with the default message, so you can query by both message content and a fallback attribute. This can be a good approach if your translations change a lot or you have many instances of similar strings with different roles (kind of like adding a testid).

Hey @josoroma, please join one of the support channels so we can better help. Issues should be used for bug reporting and feature requests.

โ“ Support Forums

We should probably have an article about this on the website.

I can take a stab at it next week. It should probably go in the examples section

I actually realized we already have an example for Rect Intl. https://testing-library.com/docs/example-react-intl

Do you think it's enough?

I've never actually had to do what is described in that example, not sure what environment it is referring to. It doesn't describe any of the the three methods above. EDIT: it's like the second bullet but we should include the other approaches

Yeah, I vaguely remember someone contributing that. I've never had to do that either though.

I've pushed this example, because I had to test the use of it in a production project.
I can provide an example with LinguiJS also, since I have another production project that uses it, but the logic is pretty much the same, you need to provide a wrapper around your component passing the i18n you use.

I've noticed something, and I think the docs should be updated.

Looks like React-intl and intl are different today than what they where when the docs where written. I managed to figure out the difference and is related to the locale-data.

If someone tries to run the setup from the docs, it will get an error like this codesandbox: https://codesandbox.io/s/yq2jz85jx1~~

This https://codesandbox.io/s/yq1434443z has the updated example.

Note the difference from the docs is that now we need to import the locales from another path, and addLocales, it was not required before to tests, only to browser application.
EDIT: Seems like this issue is related to codesandbox.

I can make a PR with this updates, and maybe add a linguiJs example.
I've never used I18next , but I can try to setup a codesandbox and if it works, add it too in the PR.

What do you think?

@viniciusavieira go ahead and get started with a PR if you have an idea. I think we should have pretty comprehensive docs for all these ideas since how to test localized content is one of the most-asked questions.

This codesandbox has a functional example of i18next test, not sure if it follow all convetions of this lib, since this was my first contact, I've checked they're docs and adapted as necessary:
https://react.i18next.com/misc/testing

https://codesandbox.io/s/testing-library-i18next-vot1o

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jaredmeakin picture jaredmeakin  ยท  3Comments

mezei picture mezei  ยท  3Comments

julienw picture julienw  ยท  4Comments

bdauria picture bdauria  ยท  4Comments

chasen-bettinger picture chasen-bettinger  ยท  3Comments