You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(render): add new query capabilities for improved tests
**What**: Add the following methods
- queryByText
- getByText
- queryByPlaceholderText
- getByPlaceholderText
- queryByLabelText
- getByLabelText
**Why**: Closes#16
These will really improve the usability of this module. These also align much better with the guiding principles 👍
**How**:
- Created a `queries.js` file where we have all the logic for the queries and their associated getter functions
- Migrate tests where it makes sense
- Update docs considerably.
**Checklist**:
* [x] Documentation
* [x] Tests
* [x] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? -->
* [ ] Added myself to contributors table N/A
[Open the tests](https://github.com/kentcdodds/react-testing-library/blob/master/src/__tests__/number-display.js)
216
324
for a full example of this.
217
325
326
+
</details>
327
+
218
328
**If I can't use shallow rendering, how do I mock out components in tests?**
219
329
330
+
<details>
331
+
332
+
<summary>expand for the answer</summary>
333
+
220
334
In general, you should avoid mocking out components (see
221
335
[the Guiding Principles section](#guiding-principles)). However if you need to,
222
336
then it's pretty trivial using
@@ -265,15 +379,28 @@ something more
265
379
Learn more about how Jest mocks work from my blog post:
266
380
["But really, what is a JavaScript mock?"](https://blog.kentcdodds.com/but-really-what-is-a-javascript-mock-10d060966f7d)
267
381
382
+
</details>
383
+
268
384
**What if I want to verify that an element does NOT exist?**
269
385
386
+
<details>
387
+
388
+
<summary>expand for the answer</summary>
389
+
270
390
You typically will get access to rendered elements using the `getByTestId` utility. However, that function will throw an error if the element isn't found. If you want to specifically test for the absence of an element, then you should use the `queryByTestId` utility which will return the element if found or `null` if not.
> The less your tests resemble the way your software is used, the less confidence they can give you. - [17 Feb 2018](https://twitter.com/kentcdodds/status/965052178267176960)
477
+
> The more your tests resemble the way your software is used, the more confidence they can give you. - [17 Feb 2018][guiding-principle]
338
478
339
479
Because users can't directly interact with your app's component instances,
340
480
assert on their internal state or what components they render, or call their
@@ -345,8 +485,14 @@ That's not to say that there's never a use case for doing those things, so they
345
485
should be possible to accomplish, just not the default and natural way to test
346
486
react components.
347
487
488
+
</details>
489
+
348
490
**How does `flushPromises` work and why would I need it?**
349
491
492
+
<details>
493
+
494
+
<summary>expand for the answer</summary>
495
+
350
496
As mentioned [before](#flushpromises), `flushPromises` uses
351
497
[`setImmediate`][set-immediate] to schedule resolving a promise after any pending
352
498
tasks in
@@ -366,6 +512,8 @@ that this is only effective if you've mocked out your async requests to resolve
366
512
immediately (like the `axios` mock we have in the examples). It will not `await`
367
513
for promises that are not already resolved by the time you attempt to flush them.
368
514
515
+
</details>
516
+
369
517
## Other Solutions
370
518
371
519
In preparing this project,
@@ -378,7 +526,7 @@ this one instead.
378
526
379
527
## Guiding Principles
380
528
381
-
> [The less your tests resemble the way your software is used, the less confidence they can give you.](https://twitter.com/kentcdodds/status/965052178267176960)
529
+
> [The more your tests resemble the way your software is used, the more confidence they can give you.][guiding-principle]
382
530
383
531
We try to only expose methods and utilities that encourage you to write tests
384
532
that closely resemble how your react components are used.
exports[`getByTestId finds matching element 1`] =`
4
-
<span
5
-
data-testid="test-component"
6
-
/>
7
-
`;
8
-
9
-
exports[`getByTestId throws error when no matching element exists 1`] =`"Unable to find element by [data-testid=\\"unknown-data-testid\\"]"`;
10
-
11
-
exports[`queryByTestId finds matching element 1`] =`
12
-
<span
13
-
data-testid="test-component"
14
-
/>
15
-
`;
3
+
exports[`get throws a useful error message 1`] =`"Unable to find a label with the text of: LucyRicardo"`;
4
+
5
+
exports[`get throws a useful error message 2`] =`"Unable to find an element with the placeholder text of: LucyRicardo"`;
6
+
7
+
exports[`get throws a useful error message 3`] =`"Unable to find an element with the text: LucyRicardo. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible."`;
8
+
9
+
exports[`get throws a useful error message 4`] =`"Unable to find an element by: [data-testid=\\"LucyRicardo\\"]"`;
10
+
11
+
exports[`label with no form control 1`] =`"Found a label with the text of: alone, however no form control was found associated to that label. Make sure you're using the \\"for\\" attribute or \\"aria-labelledby\\" attribute correctly."`;
12
+
13
+
exports[`totally empty label 1`] =`"Found a label with the text of: , however no form control was found associated to that label. Make sure you're using the \\"for\\" attribute or \\"aria-labelledby\\" attribute correctly."`;
0 commit comments