Skip to content

Commit 11aa94c

Browse files
authored
docs: add docs for intl componnets (#834)
1 parent adc3832 commit 11aa94c

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

docs/example-react-intl.mdx

+13
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,16 @@ test('it should render FormattedDate and have a formatted pt date', () => {
129129
expect(screen.getByTestId('date-display')).toHaveTextContent('11/03/2019')
130130
})
131131
```
132+
133+
## Translated components testing stategy
134+
135+
When testing a translated component there can be different approches for
136+
achiving the wanted coverage, where the aimed goal should be to allow to test
137+
the component in a way that will simulate the user behavior as much as posible.
138+
139+
| Approach | Pros | Cons |
140+
| ------------------------------------------ | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
141+
| 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. |
142+
| 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. |
143+
| 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(<FormattedMessage id="buttonText" defaultMessage="Hello Button" />);` | 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. |
144+
| 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. |

0 commit comments

Comments
 (0)