mercredi 13 janvier 2021

React Testing Lib: Best practice on querying translated strings?

I am having a discussion with my team about what is the best practice when it comes to querying translated texts.

As those texts can be modified by non-developers (translators) we don't want our tests to be broken on each translation change. But a part of my teammates thinks we should keep on using plain strings, so our tests resemble user behavior... In our current code we are doing something like this to find a button:

const buttonCHF = await screen.findByText('My button textl');

We are currently using react-intl, so I proposed a solution that would go like this to find the same button:

const buttonText = getNodeText(<FormatteMessage id="buttonText" />);
const buttonCHF = await screen.findByText(buttonText);

I think this would make the test more resilient, without breaking the rule of making tests the most similar as possible to user behavior, after all, we would be querying the same text as the user while avoiding unmaintainable strings. What do you think?

Aucun commentaire:

Enregistrer un commentaire