|
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 |
|
@@ -79,6 +79,7 @@ facilitate testing implementation details). Read more about this in
|
79 | 79 | * [`Simulate`](#simulate)
|
80 | 80 | * [`flushPromises`](#flushpromises)
|
81 | 81 | * [`render`](#render)
|
| 82 | +* [Custom Jest Matchers](#custom-jest-matchers) |
82 | 83 | * [`TextMatch`](#textmatch)
|
83 | 84 | * [`query` APIs](#query-apis)
|
84 | 85 | * [Examples](#examples)
|
@@ -250,6 +251,44 @@ const usernameInputElement = getByTestId('username-input')
|
250 | 251 | > Learn more about `data-testid`s from the blog post
|
251 | 252 | > ["Making your UI tests resilient to change"][data-testid-blog-post]
|
252 | 253 |
|
| 254 | +## Custom Jest Matchers |
| 255 | + |
| 256 | +There are two simple API which extend the `expect` API of jest for making assertions easier. |
| 257 | + |
| 258 | +### `toBeInTheDOM` |
| 259 | + |
| 260 | +This allows you to assert whether an element present in the DOM or not. |
| 261 | + |
| 262 | +```javascript |
| 263 | +// add the custom expect matchers |
| 264 | +import 'react-testing-library/extend-expect' |
| 265 | + |
| 266 | +// ... |
| 267 | +const {queryByTestId} = render(<span data-testid="count-value">2</span>) |
| 268 | +expect(queryByTestId('count-value')).toBeInTheDOM() |
| 269 | +expect(queryByTestId('count-value1')).not.toBeInTheDOM() |
| 270 | +// ... |
| 271 | +``` |
| 272 | + |
| 273 | +> Note: when using `toBeInTheDOM`, make sure you use a query function |
| 274 | +> (like `queryByTestId`) rather than a get function (like `getByTestId`). |
| 275 | +> Otherwise the `get*` function could throw an error before your assertion. |
| 276 | +
|
| 277 | +### `toHaveTextContent` |
| 278 | + |
| 279 | +This API allows you to check whether the given element has a text content or not. |
| 280 | + |
| 281 | +```javascript |
| 282 | +// add the custom expect matchers |
| 283 | +import 'react-testing-library/extend-expect' |
| 284 | + |
| 285 | +// ... |
| 286 | +const {getByTestId} = render(<span data-testid="count-value">2</span>) |
| 287 | +expect(getByTestId('count-value')).toHaveTextContent('2') |
| 288 | +expect(getByTestId('count-value')).not.toHaveTextContent('21') |
| 289 | +// ... |
| 290 | +``` |
| 291 | + |
253 | 292 | ## `TextMatch`
|
254 | 293 |
|
255 | 294 | Several APIs accept a `TextMatch` which can be a `string`, `regex` or a
|
@@ -595,7 +634,7 @@ Thanks goes to these people ([emoji key][emojis]):
|
595 | 634 | <!-- prettier-ignore -->
|
596 | 635 | | [<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") |
|
597 | 636 | | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
|
598 |
| -| [<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") | |
| 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") | |
599 | 638 |
|
600 | 639 | <!-- ALL-CONTRIBUTORS-LIST:END -->
|
601 | 640 |
|
|
0 commit comments