Skip to content

Commit 6fba260

Browse files
Fix act() event example using button before declaration (#8593)
* Fix act() dispatch example using button before it is defined The event example called button.dispatchEvent before const button was declared, which would throw at runtime. Also drop it.only from the docs snippet. * Highlight the full act event-dispatch block --------- Co-authored-by: Aurora Scharff <aurora.sofie@gmail.com>
1 parent fb39485 commit 6fba260

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

  • src/content/reference/react

‎src/content/reference/react/act.md‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,26 +117,27 @@ Using `act` ensures that all updates have been applied before we make assertions
117117

118118
To test events, wrap the event dispatch inside `act()`:
119119

120-
```js {14,16}
120+
```js {17-19}
121121
import {act} from 'react';
122122
import ReactDOMClient from 'react-dom/client';
123123
import Counter from './Counter';
124124

125-
it.only('can render and update a counter', async () => {
125+
it('can render and update a counter', async () => {
126126
const container = document.createElement('div');
127127
document.body.appendChild(container);
128128

129-
await act( async () => {
129+
await act(async () => {
130130
ReactDOMClient.createRoot(container).render(<Counter />);
131131
});
132132

133+
const button = container.querySelector('button');
134+
const label = container.querySelector('p');
135+
133136
// ✅ Dispatch the event inside act().
134137
await act(async () => {
135138
button.dispatchEvent(new MouseEvent('click', { bubbles: true }));
136139
});
137140

138-
const button = container.querySelector('button');
139-
const label = container.querySelector('p');
140141
expect(label.textContent).toBe('You clicked 1 times');
141142
expect(document.title).toBe('You clicked 1 times');
142143
});

0 commit comments

Comments
 (0)