|
3 | 3 | title: FAQ
|
4 | 4 | ---
|
5 | 5 |
|
| 6 | +See also the [main FAQ](/docs/faq) for questions not specific to React testing |
| 7 | + |
6 | 8 | <details>
|
7 | 9 |
|
8 | 10 | <summary>How do I test input onChange handlers?</summary>
|
@@ -62,62 +64,6 @@ as part of the `change` method call.
|
62 | 64 |
|
63 | 65 | <details>
|
64 | 66 |
|
65 |
| -<summary>Which get method should I use?</summary> |
66 |
| - |
67 |
| -Based on [the Guiding Principles](./guiding-principles), your test should |
68 |
| -resemble how your code (component, page, etc.) is used as much as possible. With |
69 |
| -this in mind, we recommend this order of priority: |
70 |
| - |
71 |
| -1. `getByLabelText`: Only really good for form fields, but this is the number 1 |
72 |
| - method a user finds those elements, so it should be your top preference. |
73 |
| -2. `getByPlaceholderText`: |
74 |
| - [A placeholder is not a substitute for a label](https://www.nngroup.com/articles/form-design-placeholders/). |
75 |
| - But if that's all you have, then it's better than alternatives. |
76 |
| -3. `getByText`: Not useful for forms, but this is the number 1 method a user |
77 |
| - finds other elements (like buttons to click), so it should be your top |
78 |
| - preference for non-form elements. |
79 |
| -4. `getByAltText`: If your element is one which supports `alt` text (`img`, |
80 |
| - `area`, and `input`), then you can use this to find that element. |
81 |
| -5. `getByTestId`: The user cannot see (or hear) these, so this is only |
82 |
| - recommended for cases where you can't match by text or it doesn't make sense |
83 |
| - (the text is dynamic). |
84 |
| - |
85 |
| -Other than that, you can also use the `container` to query the rendered |
86 |
| -component as well (using the regular |
87 |
| -[`querySelector` API](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector)). |
88 |
| - |
89 |
| -</details> |
90 |
| - |
91 |
| -<details> |
92 |
| - |
93 |
| -<summary>Can I write unit tests with this library?</summary> |
94 |
| - |
95 |
| -Definitely yes! You can write unit and integration tests with this library. See |
96 |
| -below for more on how to mock dependencies (because this library intentionally |
97 |
| -does NOT support shallow rendering) if you want to unit test a high level |
98 |
| -component. The tests in this project show several examples of unit testing with |
99 |
| -this library. |
100 |
| - |
101 |
| -As you write your tests, keep in mind: |
102 |
| - |
103 |
| -> The more your tests resemble the way your software is used, the more |
104 |
| -> confidence they can give you. - [17 Feb 2018][guiding-principle] |
105 |
| -
|
106 |
| -</details> |
107 |
| - |
108 |
| -<details> |
109 |
| - |
110 |
| -<summary>What if my app is localized and I don't have access to the text in test?</summary> |
111 |
| - |
112 |
| -This is fairly common. Our first bit of advice is to try to get the default text |
113 |
| -used in your tests. That will make everything much easier (more than just using |
114 |
| -this utility). If that's not possible, then you're probably best to just stick |
115 |
| -with `data-testid`s (which is not bad anyway). |
116 |
| - |
117 |
| -</details> |
118 |
| - |
119 |
| -<details> |
120 |
| - |
121 | 67 | <summary>If I can't use shallow rendering, how do I mock out components in tests?</summary>
|
122 | 68 |
|
123 | 69 | In general, you should avoid mocking out components (see
|
@@ -169,80 +115,6 @@ Learn more about how Jest mocks work from my blog post:
|
169 | 115 |
|
170 | 116 | <details>
|
171 | 117 |
|
172 |
| -<summary>What if I want to verify that an element does NOT exist?</summary> |
173 |
| - |
174 |
| -You typically will get access to rendered elements using the `getByTestId` |
175 |
| -utility. However, that function will throw an error if the element isn't found. |
176 |
| -If you want to specifically test for the absence of an element, then you should |
177 |
| -use the `queryByTestId` utility which will return the element if found or `null` |
178 |
| -if not. |
179 |
| - |
180 |
| -```javascript |
181 |
| -expect(queryByTestId('thing-that-does-not-exist')).toBeNull() |
182 |
| -``` |
183 |
| - |
184 |
| -</details> |
185 |
| - |
186 |
| -<details> |
187 |
| - |
188 |
| -<summary>I really don't like data-testids, but none of the other queries make sense. Do I have to use a data-testid?</summary> |
189 |
| - |
190 |
| -Definitely not. That said, a common reason people don't like the `data-testid` |
191 |
| -attribute is they're concerned about shipping that to production. I'd suggest |
192 |
| -that you probably want some simple E2E tests that run in production on occasion |
193 |
| -to make certain that things are working smoothly. In that case the `data-testid` |
194 |
| -attributes will be very useful. Even if you don't run these in production, you |
195 |
| -may want to run some E2E tests that run on the same code you're about to ship to |
196 |
| -production. In that case, the `data-testid` attributes will be valuable there as |
197 |
| -well. |
198 |
| - |
199 |
| -All that said, if you really don't want to ship `data-testid` attributes, then |
200 |
| -you can use |
201 |
| -[this simple babel plugin](https://www.npmjs.com/package/babel-plugin-react-remove-properties) |
202 |
| -to remove them. |
203 |
| - |
204 |
| -If you don't want to use them at all, then you can simply use regular DOM |
205 |
| -methods and properties to query elements off your container. |
206 |
| - |
207 |
| -```javascript |
208 |
| -const firstLiInDiv = container.querySelector('div li') |
209 |
| -const allLisInDiv = container.querySelectorAll('div li') |
210 |
| -const rootElement = container.firstChild |
211 |
| -``` |
212 |
| - |
213 |
| -</details> |
214 |
| - |
215 |
| -<details> |
216 |
| - |
217 |
| -<summary>What if I’m iterating over a list of items that I want to put the data-testid="item" attribute on. How do I distinguish them from each other?</summary> |
218 |
| - |
219 |
| -You can make your selector just choose the one you want by including :nth-child |
220 |
| -in the selector. |
221 |
| - |
222 |
| -```javascript |
223 |
| -const thirdLiInUl = container.querySelector('ul > li:nth-child(3)') |
224 |
| -``` |
225 |
| - |
226 |
| -Or you could include the index or an ID in your attribute: |
227 |
| - |
228 |
| -```javascript |
229 |
| -<li data-testid={`item-${item.id}`}>{item.text}</li> |
230 |
| -``` |
231 |
| - |
232 |
| -And then you could use the `getByTestId` utility: |
233 |
| - |
234 |
| -```javascript |
235 |
| -const items = [ |
236 |
| - /* your items */ |
237 |
| -] |
238 |
| -const { getByTestId } = render(/* your component with the items */) |
239 |
| -const thirdItem = getByTestId(`item-${items[2].id}`) |
240 |
| -``` |
241 |
| - |
242 |
| -</details> |
243 |
| - |
244 |
| -<details> |
245 |
| - |
246 | 118 | <summary>What about enzyme is "bloated with complexity and features" and "encourage
|
247 | 119 | poor testing practices"?</summary>
|
248 | 120 |
|
|
0 commit comments