Skip to content

How to test i18next #390

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

Closed
josoroma-zz opened this issue Jun 22, 2019 · 11 comments
Closed

How to test i18next #390

josoroma-zz opened this issue Jun 22, 2019 · 11 comments

Comments

@josoroma-zz
Copy link

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');
});
@alexkrolick
Copy link
Collaborator

alexkrolick commented Jun 22, 2019

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

@Gpx
Copy link
Member

Gpx commented Jun 22, 2019

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

@kentcdodds
Copy link
Member

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

@Gpx
Copy link
Member

Gpx commented Jun 22, 2019

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

@Gpx
Copy link
Member

Gpx commented Jun 25, 2019

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?

@alexkrolick
Copy link
Collaborator

alexkrolick commented Jun 25, 2019

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

@kentcdodds
Copy link
Member

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

@viniciusavieira
Copy link
Contributor

viniciusavieira commented Jun 25, 2019

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.

@viniciusavieira
Copy link
Contributor

viniciusavieira commented Jun 25, 2019

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?

@alexkrolick
Copy link
Collaborator

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

@viniciusavieira
Copy link
Contributor

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

lucbpz pushed a commit to lucbpz/react-testing-library that referenced this issue Jul 26, 2020
Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
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

5 participants