You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/reference/react/act.md
+27-6Lines changed: 27 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -4,25 +4,46 @@ title: act
4
4
5
5
<Intro>
6
6
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
+
```
8
12
9
13
</Intro>
10
14
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
+
11
18
<InlineToc />
12
19
13
20
---
14
21
22
+
## Reference {/*reference*/}
23
+
24
+
### `act(updateFn)` {/*actupdatefn*/}
25
+
15
26
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.
16
27
17
28
The name `act` comes from the [Arrange-Act-Assert](https://wiki.c2.com/?ArrangeActAssert) pattern.
*`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.
0 commit comments