Skip to content

Commit 61952ed

Browse files
author
Jack Pope
committed
Update structure based on feedback
1 parent 9f3ae08 commit 61952ed

File tree

1 file changed

+27
-6
lines changed
  • src/content/reference/react

1 file changed

+27
-6
lines changed

src/content/reference/react/act.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,46 @@ title: act
44

55
<Intro>
66

7-
To prepare a component for assertions, wrap the code rendering it and performing updates inside an act() call. This makes your test run closer to how React works in the browser.
7+
`act` lets you wrap rendering and updates in tests to ensure changes are applied to the DOM before making assertions.
8+
9+
```js
10+
act(updateFn)
11+
```
812

913
</Intro>
1014

15+
To prepare a component for assertions, wrap the code rendering it and performing updates inside an `act()` call. This makes your test run closer to how React works in the browser.
16+
17+
1118
<InlineToc />
1219

1320
---
1421

22+
## Reference {/*reference*/}
23+
24+
### `act(updateFn)` {/*actupdatefn*/}
25+
1526
When writing UI tests, tasks like rendering, user events, or data fetching can be considered as “units” of interaction with a user interface. React provides a helper called `act()` that makes sure all updates related to these “units” have been processed and applied to the DOM before you make any assertions.
1627

1728
The name `act` comes from the [Arrange-Act-Assert](https://wiki.c2.com/?ArrangeActAssert) pattern.
1829

19-
```js
20-
act(() => {
21-
// render components
30+
```js {2,4}
31+
it ('renders with button disabled', () => {
32+
act(() => {
33+
root.render(<TestComponent />)
34+
});
35+
expect(container.querySelector('button')).toBeDisabled();
2236
});
23-
// make assertions
2437
```
2538

39+
#### Parameters {/*parameters*/}
40+
41+
* `updateFn`: A function wrapping renders or interactions for components being tested. Any updates triggered within the `updateFn` are added to an internal act queue, which are then flushed together to process and apply any changes to the DOM.
42+
43+
#### Returns {/*returns*/}
44+
45+
`act` does not return anything.
46+
2647
## Usage {/*usage*/}
2748

2849
<Note>
@@ -55,7 +76,7 @@ function Counter() {
5576

5677
Here is how we can test it:
5778

58-
```js
79+
```js {19,21,28,30}
5980
import React from 'react';
6081
import ReactDOM from 'react-dom/client';
6182
import Counter from './Counter';

0 commit comments

Comments
 (0)