|
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 |
|
@@ -81,8 +81,11 @@ facilitate testing implementation details). Read more about this in
|
81 | 81 | * [Installation](#installation)
|
82 | 82 | * [Usage](#usage)
|
83 | 83 | * [`render`](#render)
|
| 84 | + * [`renderIntoDocument`](#renderintodocument) |
| 85 | + * [`cleanup`](#cleanup) |
84 | 86 | * [`Simulate`](#simulate)
|
85 | 87 | * [`wait`](#wait)
|
| 88 | + * [`fireEvent(node: HTMLElement, event: Event)`](#fireeventnode-htmlelement-event-event) |
86 | 89 | * [`TextMatch`](#textmatch)
|
87 | 90 | * [`query` APIs](#query-apis)
|
88 | 91 | * [Examples](#examples)
|
@@ -264,6 +267,31 @@ const usernameInputElement = getByTestId('username-input')
|
264 | 267 | > Learn more about `data-testid`s from the blog post
|
265 | 268 | > ["Making your UI tests resilient to change"][data-testid-blog-post]
|
266 | 269 |
|
| 270 | +### `renderIntoDocument` |
| 271 | + |
| 272 | +Render into `document.body`. Should be used with [cleanup](#cleanup) |
| 273 | + |
| 274 | +```javascript |
| 275 | +renderIntoDocument(<div>) |
| 276 | +``` |
| 277 | + |
| 278 | +### `cleanup` |
| 279 | + |
| 280 | +Unmounts React trees that were mounted with [renderIntoDocument](#renderintodocument). |
| 281 | + |
| 282 | +```javascript |
| 283 | +afterEach(cleanup) |
| 284 | + |
| 285 | +test('renders into document', () => { |
| 286 | + renderIntoDocument(<div>) |
| 287 | + // ... |
| 288 | +}) |
| 289 | +``` |
| 290 | + |
| 291 | +Failing to call `cleanup` when you've called `renderIntoDocument` could |
| 292 | +result in a memory leak and tests which are not `idempotent` (which can |
| 293 | +lead to difficult to debug errors in your tests). |
| 294 | + |
267 | 295 | ### `Simulate`
|
268 | 296 |
|
269 | 297 | This is simply a re-export from the `Simulate` utility from
|
@@ -313,6 +341,62 @@ The default `interval` is `50ms`. However it will run your callback immediately
|
313 | 341 | on the next tick of the event loop (in a `setTimeout`) before starting the
|
314 | 342 | intervals.
|
315 | 343 |
|
| 344 | +### `fireEvent(node: HTMLElement, event: Event)` |
| 345 | + |
| 346 | +Fire DOM events. |
| 347 | + |
| 348 | +React attaches an event handler on the `document` and handles some DOM events |
| 349 | +via event delegation (events bubbling up from a `target` to an ancestor). Because |
| 350 | +of this, your `node` must be in the `document.body` for `fireEvent` to work with |
| 351 | +React. You can render into the document using the |
| 352 | +[renderIntoDocument](#renderintodocument) utility. This is an alternative to |
| 353 | +simulating Synthetic React Events via [Simulate](#simulate). The benefit of |
| 354 | +using `fireEvent` over `Simulate` is that you are testing real DOM events |
| 355 | +instead of Synthetic Events. This aligns better with |
| 356 | +[the Guiding Principles](#guiding-principles). |
| 357 | + |
| 358 | +> NOTE: If you don't like having to render into the document to get `fireEvent` |
| 359 | +> working, then feel free to try to chip into making it possible for React |
| 360 | +> to attach event handlers to the rendered node rather than the `document`. |
| 361 | +> Learn more here: |
| 362 | +> [facebook/react#2043](https://github.com/facebook/react/issues/2043) |
| 363 | + |
| 364 | +```javascript |
| 365 | +import { renderIntoDocument, cleanup, render, fireEvent } |
| 366 | +
|
| 367 | +// don't forget to clean up the document.body |
| 368 | +afterEach(cleanup) |
| 369 | +
|
| 370 | +test('clicks submit button', () => { |
| 371 | + const spy = jest.fn(); |
| 372 | + const { unmount, getByText } = renderIntoDocument(<button onClick={spy}>Submit</button>) |
| 373 | +
|
| 374 | + fireEvent( |
| 375 | + getByText('Submit'), |
| 376 | + new MouseEvent('click', { |
| 377 | + bubbles: true, // click events must bubble for React to see it |
| 378 | + cancelable: true, |
| 379 | + }) |
| 380 | + ) |
| 381 | +
|
| 382 | + expect(spy).toHaveBeenCalledTimes(1) |
| 383 | +}) |
| 384 | +``` |
| 385 | + |
| 386 | +#### `fireEvent[eventName](node: HTMLElement, eventProperties: Object)` |
| 387 | + |
| 388 | +Convenience methods for firing DOM events. Check out |
| 389 | +[dom-testing-library/src/events.js](https://github.com/kentcdodds/dom-testing-library/blob/master/src/events.js) |
| 390 | +for a full list as well as default `eventProperties`. |
| 391 | + |
| 392 | +```javascript |
| 393 | +// similar to the above example |
| 394 | +// click will bubble for React to see it |
| 395 | +const rightClick = {button: 2} |
| 396 | +fireEvent.click(getElementByText('Submit'), rightClick) |
| 397 | +// default `button` property for click events is set to `0` which is a left click. |
| 398 | +``` |
| 399 | + |
316 | 400 | ## `TextMatch`
|
317 | 401 |
|
318 | 402 | Several APIs accept a `TextMatch` which can be a `string`, `regex` or a
|
@@ -635,7 +719,7 @@ Thanks goes to these people ([emoji key][emojis]):
|
635 | 719 | <!-- prettier-ignore -->
|
636 | 720 | | [<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") |
|
637 | 721 | | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
|
638 |
| -| [<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") | [<img src="https://avatars2.githubusercontent.com/u/498274?v=4" width="100px;"/><br /><sub><b>Ivan Babak</b></sub>](https://sompylasar.github.io)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Asompylasar "Bug reports") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") | [<img src="https://avatars3.githubusercontent.com/u/4439618?v=4" width="100px;"/><br /><sub><b>Jesse Day</b></sub>](https://github.com/jday3)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=jday3 "Code") | [<img src="https://avatars0.githubusercontent.com/u/15199?v=4" width="100px;"/><br /><sub><b>Ernesto García</b></sub>](http://gnapse.github.io)<br />[💬](#question-gnapse "Answering Questions") [💻](https://github.com/kentcdodds/react-testing-library/commits?author=gnapse "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=gnapse "Documentation") | |
| 722 | +| [<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") | [<img src="https://avatars2.githubusercontent.com/u/498274?v=4" width="100px;"/><br /><sub><b>Ivan Babak</b></sub>](https://sompylasar.github.io)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Asompylasar "Bug reports") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") | [<img src="https://avatars3.githubusercontent.com/u/4439618?v=4" width="100px;"/><br /><sub><b>Jesse Day</b></sub>](https://github.com/jday3)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=jday3 "Code") | [<img src="https://avatars0.githubusercontent.com/u/15199?v=4" width="100px;"/><br /><sub><b>Ernesto García</b></sub>](http://gnapse.github.io)<br />[💬](#question-gnapse "Answering Questions") [💻](https://github.com/kentcdodds/react-testing-library/commits?author=gnapse "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=gnapse "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/2747424?v=4" width="100px;"/><br /><sub><b>Josef Maxx Blake</b></sub>](http://jomaxx.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=jomaxx "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=jomaxx "Documentation") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=jomaxx "Tests") | |
639 | 723 |
|
640 | 724 | <!-- ALL-CONTRIBUTORS-LIST:END -->
|
641 | 725 |
|
|
0 commit comments