From 2d25b74ad471f46dcab095a8ce3a5ca1d0514a8b Mon Sep 17 00:00:00 2001 From: itsik-avidan Date: Wed, 5 May 2021 17:20:05 +0300 Subject: [PATCH] docs: add docs for intl componnets --- docs/example-react-intl.mdx | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/example-react-intl.mdx b/docs/example-react-intl.mdx index 79e6f3e36..0a56eb3d5 100644 --- a/docs/example-react-intl.mdx +++ b/docs/example-react-intl.mdx @@ -129,3 +129,16 @@ test('it should render FormattedDate and have a formatted pt date', () => { expect(screen.getByTestId('date-display')).toHaveTextContent('11/03/2019') }) ``` + +## Translated components testing stategy + +When testing a translated component there can be different approches for +achiving the wanted coverage, where the aimed goal should be to allow to test +the component in a way that will simulate the user behavior as much as posible. + +| Approach | Pros | Cons | +| ------------------------------------------ | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| Use strings from the default language | Test is easy to read, and asserts expected default output. If you have variables in your strings, you can test that they work properly with correct output. | 1. Strings hardcoded into tests mean you have to update both tests and code for any copy changes. 2. If multiple elements have the same string/substring text, find-and-replace may be hard to use reliably. | +| Mock the translation library | If your library is difficult to use in the test environment, you can mock it so it is easier. For example, you can add the message ID as a data-attribute to the text so you can query by that. | Test code deviates from what runs in production. Tests may assert about message IDs but not enough about content, so errors are possible. | +| Use translation library in tests | Decouples strings from tests, so you can update the message files in one place without worrying about breaking tests. Can run tests in another language or multiple languages. `const buttonText = getNodeText();` | Overhead - it takes more lines of code to write the test, and you need to know the variables and message IDs to create the right strings. It's not obvious what the text actually is when you read the test code, making maintaining it harder. | +| Use translation library + inline snapshots | Same as above, but by adding an inline snapshot of the string, you can read the test code and see what strings are in use, but easily update them with `jest -u` if the messages change. `expect(buttonText).toMatchInlineSnapshot("'My button text'")` | Tests are longer because of the extra lines. You can wrap up some of the translation-related code into a helper function to make it a little more inline-able and avoid repeating yourself, but you still need to know the message IDs and variables inside the test. |