|
16 | 16 | [![downloads][downloads-badge]][npmtrends]
|
17 | 17 | [![MIT License][license-badge]][license]
|
18 | 18 |
|
19 |
| -[](#contributors) |
| 19 | +[](#contributors) |
20 | 20 | [![PRs Welcome][prs-badge]][prs]
|
21 | 21 | [![Code of Conduct][coc-badge]][coc]
|
22 | 22 |
|
@@ -78,8 +78,11 @@ facilitate testing implementation details). Read more about this in
|
78 | 78 | * [Usage](#usage)
|
79 | 79 | * [`Simulate`](#simulate)
|
80 | 80 | * [`flushPromises`](#flushpromises)
|
| 81 | + * [`waitForExpect`](#waitforexpect) |
81 | 82 | * [`render`](#render)
|
82 | 83 | * [Custom Jest Matchers](#custom-jest-matchers)
|
| 84 | + * [`toBeInTheDOM`](#tobeinthedom) |
| 85 | + * [`toHaveTextContent`](#tohavetextcontent) |
83 | 86 | * [`TextMatch`](#textmatch)
|
84 | 87 | * [`query` APIs](#query-apis)
|
85 | 88 | * [Examples](#examples)
|
@@ -151,6 +154,35 @@ you make your test function an `async` function and use
|
151 | 154 |
|
152 | 155 | See an example in the section about `render` below.
|
153 | 156 |
|
| 157 | +### `waitForExpect` |
| 158 | + |
| 159 | +Defined as: |
| 160 | + |
| 161 | +```javascript |
| 162 | +waitForExpect(expectation: () => void, timeout?: number, interval?: number) => Promise<{}>; |
| 163 | +``` |
| 164 | + |
| 165 | +When in need to wait for non-deterministic periods of time you can use waitForExpect, |
| 166 | +to wait for your expectations to pass. Take a look at [`Is there a different way to wait for things to happen?`](#waitForExpect) part of the FAQ, |
| 167 | +or the function documentation here: [`wait-for-expect`](https://github.com/TheBrainFamily/wait-for-expect) |
| 168 | +or just take a look at this simple example: |
| 169 | + |
| 170 | +```javascript |
| 171 | +... |
| 172 | +await waitForExpect(() => expect(queryByLabelText('username')).not.toBeNull()) |
| 173 | +getByLabelText('username').value = 'chucknorris' |
| 174 | +... |
| 175 | +``` |
| 176 | + |
| 177 | +Another advantage of waitForExpect in comparison to flushPromises, is that |
| 178 | +flushPromises will not flush promises that have not been queued up already, |
| 179 | +for example, if they will queue up as a result of the initial promises. |
| 180 | +In consequence of that, you might have to call flushPromises multiple times to get your components |
| 181 | +to your desired state. |
| 182 | + |
| 183 | +This can happen for example, when you integration test your apollo-connected react components |
| 184 | +that go a couple level deep, with queries fired up in consequent components. |
| 185 | + |
154 | 186 | ### `render`
|
155 | 187 |
|
156 | 188 | In the example above, the `render` method returns an object that has a few
|
@@ -591,6 +623,36 @@ that this is only effective if you've mocked out your async requests to resolve
|
591 | 623 | immediately (like the `axios` mock we have in the examples). It will not `await`
|
592 | 624 | for promises that are not already resolved by the time you attempt to flush them.
|
593 | 625 |
|
| 626 | +In case this doesn't work for you the way you would expect, take a look at the |
| 627 | +waitForExpect function that should be much more intuitive to use. |
| 628 | + |
| 629 | +</details> |
| 630 | + |
| 631 | +<details> |
| 632 | + |
| 633 | +<summary><a name="waitForExpectFAQ"></a>Is there a different way to wait for things to happen? For example for end to end or contract tests?</summary> |
| 634 | +Definitely! There is an abstraction called `waitForExpect` that will keep |
| 635 | +calling your expectations until a timeout or the expectation passes - whatever happens first. |
| 636 | + |
| 637 | +Please take a look at this example (taken from [`here`](https://github.com/kentcdodds/react-testing-library/blob/master/src/__tests__/end-to-end.js)): |
| 638 | + |
| 639 | +```javascript |
| 640 | +import {render, waitForExpect} from 'react-testing-library' |
| 641 | +test('it waits for the data to be loaded', async () => { |
| 642 | + const {queryByText, queryByTestId} = render(<ComponentWithLoader />) |
| 643 | + |
| 644 | + // Initially the loader shows |
| 645 | + expect(queryByText('Loading...')).toBeTruthy() |
| 646 | + |
| 647 | + // This will pass when the state of the component changes once the data is available |
| 648 | + // the loader will disappear, and the data will be shown |
| 649 | + await waitForExpect(() => expect(queryByText('Loading...')).toBeNull()) |
| 650 | + expect(queryByTestId('message').textContent).toMatch(/Hello World/) |
| 651 | +}) |
| 652 | +``` |
| 653 | + |
| 654 | +For consistency and making your tests easier to understand, you can use it instead of flushPromises. |
| 655 | + |
594 | 656 | </details>
|
595 | 657 |
|
596 | 658 | ## Other Solutions
|
@@ -634,7 +696,7 @@ Thanks goes to these people ([emoji key][emojis]):
|
634 | 696 | <!-- prettier-ignore -->
|
635 | 697 | | [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=kentcdodds "Tests") | [<img src="https://avatars1.githubusercontent.com/u/2430381?v=4" width="100px;"/><br /><sub><b>Ryan Castner</b></sub>](http://audiolion.github.io)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=audiolion "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/8008023?v=4" width="100px;"/><br /><sub><b>Daniel Sandiego</b></sub>](https://www.dnlsandiego.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=dnlsandiego "Code") | [<img src="https://avatars2.githubusercontent.com/u/12592677?v=4" width="100px;"/><br /><sub><b>Paweł Mikołajczyk</b></sub>](https://github.com/Miklet)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=Miklet "Code") | [<img src="https://avatars3.githubusercontent.com/u/464978?v=4" width="100px;"/><br /><sub><b>Alejandro Ñáñez Ortiz</b></sub>](http://co.linkedin.com/in/alejandronanez/)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=alejandronanez "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/1402095?v=4" width="100px;"/><br /><sub><b>Matt Parrish</b></sub>](https://github.com/pbomb)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Apbomb "Bug reports") [💻](https://github.com/kentcdodds/react-testing-library/commits?author=pbomb "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=pbomb "Documentation") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=pbomb "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1288694?v=4" width="100px;"/><br /><sub><b>Justin Hall</b></sub>](https://github.com/wKovacs64)<br />[📦](#platform-wKovacs64 "Packaging/porting to new platform") |
|
636 | 698 | | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
|
637 |
| -| [<img src="https://avatars1.githubusercontent.com/u/1241511?s=460&v=4" width="100px;"/><br /><sub><b>Anto Aravinth</b></sub>](https://github.com/antoaravinth)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Code") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Tests") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/3462296?v=4" width="100px;"/><br /><sub><b>Jonah Moses</b></sub>](https://github.com/JonahMoses)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=JonahMoses "Documentation") | |
| 699 | +| [<img src="https://avatars1.githubusercontent.com/u/1241511?s=460&v=4" width="100px;"/><br /><sub><b>Anto Aravinth</b></sub>](https://github.com/antoaravinth)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Code") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Tests") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/3462296?v=4" width="100px;"/><br /><sub><b>Jonah Moses</b></sub>](https://github.com/JonahMoses)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=JonahMoses "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/4002543?v=4" width="100px;"/><br /><sub><b>Łukasz Gandecki</b></sub>](http://team.thebrain.pro)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=lgandecki "Code") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=lgandecki "Tests") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=lgandecki "Documentation") | |
638 | 700 |
|
639 | 701 | <!-- ALL-CONTRIBUTORS-LIST:END -->
|
640 | 702 |
|
|
0 commit comments