Skip to content

Commit 11a41ce

Browse files
author
Kent C. Dodds
authored
feat: add flushEffects to prepare for hooks (#223)
1 parent 169434f commit 11a41ce

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ test('Fetch makes an API call and displays the greeting when load-greeting is cl
144144
- [Usage](#usage)
145145
- [`render`](#render)
146146
- [`cleanup`](#cleanup)
147+
- [`flushEffects` (experimental)](#flusheffects-experimental)
147148
- [`dom-testing-library` APIs](#dom-testing-library-apis)
148149
- [`fireEvent(node: HTMLElement, event: Event)`](#fireeventnode-htmlelement-event-event)
149150
- [`waitForElement`](#waitforelement)
@@ -283,9 +284,11 @@ module.exports = {
283284
],
284285
// ... other options ...
285286
}
286-
```
287+
```
287288

288-
If your project is based on top of Create React App, to make the file accessible without using relative imports, you just need to create a `.env` file in the root of your project with the following configuration:
289+
If your project is based on top of Create React App, to make the file accessible
290+
without using relative imports, you just need to create a `.env` file in the
291+
root of your project with the following configuration:
289292

290293
```
291294
// Create React App project structure
@@ -493,7 +496,8 @@ unmount()
493496
494497
#### `getByLabelText(text: TextMatch, options): HTMLElement`
495498
496-
> Options: `{selector = '*', exact = true, collapseWhitespace = true, trim = true}`
499+
> Options:
500+
> `{selector = '*', exact = true, collapseWhitespace = true, trim = true}`
497501
498502
This will search for the label that matches the given [`TextMatch`](#textmatch),
499503
then find the element associated with that label.
@@ -550,7 +554,8 @@ const inputNode = getByPlaceholderText('Username')
550554
551555
#### `getByText(text: TextMatch, options): HTMLElement`
552556
553-
> Options: `{selector = '*', exact = true, collapseWhitespace = true, trim = true, ignore = 'script, style'}`
557+
> Options:
558+
> `{selector = '*', exact = true, collapseWhitespace = true, trim = true, ignore = 'script, style'}`
554559
555560
This will search for all elements that have a text node with `textContent`
556561
matching the given [`TextMatch`](#textmatch).
@@ -733,6 +738,11 @@ that you configure your test framework to run a file before your tests which
733738
does this automatically. See the [setup](#setup) section for guidance on how to
734739
set up your framework.
735740
741+
### `flushEffects` (experimental)
742+
743+
This experimental API is intended to be used to force React's `useEffect` hook
744+
to run synchronously.
745+
736746
## `dom-testing-library` APIs
737747
738748
`react-testing-library` is built on top of
@@ -1369,6 +1379,7 @@ Thanks goes to these people ([emoji key][emojis]):
13691379
| [<img src="https://avatars3.githubusercontent.com/u/881986?v=4" width="100px;"/><br /><sub><b>dadamssg</b></sub>](https://github.com/dadamssg)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=dadamssg "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/8734097?v=4" width="100px;"/><br /><sub><b>Yazan Aabed</b></sub>](https://www.yaabed.com/)<br />[📝](#blog-YazanAabeed "Blogposts") | [<img src="https://avatars0.githubusercontent.com/u/556258?v=4" width="100px;"/><br /><sub><b>Tim</b></sub>](https://github.com/timbonicus)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Atimbonicus "Bug reports") [💻](https://github.com/kentcdodds/react-testing-library/commits?author=timbonicus "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=timbonicus "Documentation") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=timbonicus "Tests") | [<img src="https://avatars3.githubusercontent.com/u/6682655?v=4" width="100px;"/><br /><sub><b>Divyanshu Maithani</b></sub>](http://divyanshu.xyz)<br />[✅](#tutorial-divyanshu013 "Tutorials") [📹](#video-divyanshu013 "Videos") | [<img src="https://avatars2.githubusercontent.com/u/9116042?v=4" width="100px;"/><br /><sub><b>Deepak Grover</b></sub>](https://www.linkedin.com/in/metagrover)<br />[✅](#tutorial-metagrover "Tutorials") [📹](#video-metagrover "Videos") | [<img src="https://avatars0.githubusercontent.com/u/16276358?v=4" width="100px;"/><br /><sub><b>Eyal Cohen</b></sub>](https://github.com/eyalcohen4)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=eyalcohen4 "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/7452681?v=4" width="100px;"/><br /><sub><b>Peter Makowski</b></sub>](https://github.com/petermakowski)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=petermakowski "Documentation") |
13701380
| [<img src="https://avatars2.githubusercontent.com/u/20361668?v=4" width="100px;"/><br /><sub><b>Michiel Nuyts</b></sub>](https://github.com/Michielnuyts)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=Michielnuyts "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/1195863?v=4" width="100px;"/><br /><sub><b>Joe Ng'ethe</b></sub>](https://github.com/joeynimu)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=joeynimu "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=joeynimu "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/19998290?v=4" width="100px;"/><br /><sub><b>Kate</b></sub>](https://github.com/Enikol)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=Enikol "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/11980217?v=4" width="100px;"/><br /><sub><b>Sean</b></sub>](http://www.seanrparker.com)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=SeanRParker "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/17031?v=4" width="100px;"/><br /><sub><b>James Long</b></sub>](http://jlongster.com)<br />[🤔](#ideas-jlongster "Ideas, Planning, & Feedback") [📦](#platform-jlongster "Packaging/porting to new platform") | [<img src="https://avatars1.githubusercontent.com/u/10118777?v=4" width="100px;"/><br /><sub><b>Herb Hagely</b></sub>](https://github.com/hhagely)<br />[💡](#example-hhagely "Examples") | [<img src="https://avatars2.githubusercontent.com/u/5779538?v=4" width="100px;"/><br /><sub><b>Alex Wendte</b></sub>](http://www.wendtedesigns.com/)<br />[💡](#example-themostcolm "Examples") |
13711381
| [<img src="https://avatars0.githubusercontent.com/u/6998954?v=4" width="100px;"/><br /><sub><b>Monica Powell</b></sub>](http://www.aboutmonica.com)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=M0nica "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/2699953?v=4" width="100px;"/><br /><sub><b>Vitaly Sivkov</b></sub>](http://sivkoff.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=sivkoff "Code") | [<img src="https://avatars3.githubusercontent.com/u/7049?v=4" width="100px;"/><br /><sub><b>Weyert de Boer</b></sub>](https://github.com/weyert)<br />[🤔](#ideas-weyert "Ideas, Planning, & Feedback") [👀](#review-weyert "Reviewed Pull Requests") | [<img src="https://avatars3.githubusercontent.com/u/13613037?v=4" width="100px;"/><br /><sub><b>EstebanMarin</b></sub>](https://github.com/EstebanMarin)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=EstebanMarin "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/13953703?v=4" width="100px;"/><br /><sub><b>Victor Martins</b></sub>](https://github.com/vctormb)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=vctormb "Documentation") |
1382+
13721383
<!-- ALL-CONTRIBUTORS-LIST:END -->
13731384
13741385
This project follows the [all-contributors][all-contributors] specification.

src/__tests__/render.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'jest-dom/extend-expect'
22
import React from 'react'
33
import ReactDOM from 'react-dom'
4-
import {render, cleanup} from '../'
4+
import {render, cleanup, flushEffects} from '../'
55

66
afterEach(cleanup)
77

@@ -90,3 +90,10 @@ it('supports fragments', () => {
9090
cleanup()
9191
expect(document.body.innerHTML).toBe('')
9292
})
93+
94+
test('flushEffects can be called without causing issues', () => {
95+
render(<div />)
96+
const preHtml = document.documentElement.innerHTML
97+
flushEffects()
98+
expect(document.documentElement.innerHTML).toBe(preHtml)
99+
})

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ function cleanup() {
4848
mountedContainers.forEach(cleanupAtContainer)
4949
}
5050

51+
function flushEffects() {
52+
ReactDOM.render(null, document.createElement('div'))
53+
}
54+
5155
// maybe one day we'll expose this (perhaps even as a utility returned by render).
5256
// but let's wait until someone asks for it.
5357
function cleanupAtContainer(container) {
@@ -81,6 +85,6 @@ fireEvent.select = (node, init) => {
8185

8286
// just re-export everything from dom-testing-library
8387
export * from 'dom-testing-library'
84-
export {render, cleanup}
88+
export {render, cleanup, flushEffects}
8589

8690
/* eslint func-name-matching:0 */

0 commit comments

Comments
 (0)