@@ -83,92 +83,106 @@ describe('ChangeEventPlugin', () => { |
83 | 83 | // keep track of the "current" value and only fire events when it changes. |
84 | 84 | // See https://github.com/facebook/react/pull/5746. |
85 | 85 |
|
86 | | - it('should consider initial text value to be current', () => { |
| 86 | + it('should consider initial text value to be current', async () => { |
87 | 87 | let called = 0; |
88 | 88 |
|
89 | 89 | function cb(e) { |
90 | 90 | called++; |
91 | 91 | expect(e.type).toBe('change'); |
92 | 92 | } |
93 | 93 |
|
94 | | - const node = ReactDOM.render( |
95 | | - <input type="text" onChange={cb} defaultValue="foo" />, |
96 | | - container, |
97 | | - ); |
| 94 | + const root = ReactDOMClient.createRoot(container); |
| 95 | + await act(() => { |
| 96 | + root.render(<input type="text" onChange={cb} defaultValue="foo" />); |
| 97 | + }); |
| 98 | + |
| 99 | + const node = container.firstChild; |
98 | 100 | node.dispatchEvent(new Event('input', {bubbles: true, cancelable: true})); |
99 | 101 | node.dispatchEvent(new Event('change', {bubbles: true, cancelable: true})); |
100 | 102 |
|
101 | 103 | // There should be no React change events because the value stayed the same. |
102 | 104 | expect(called).toBe(0); |
103 | 105 | }); |
104 | 106 |
|
105 | | - it('should consider initial text value to be current (capture)', () => { |
| 107 | + it('should consider initial text value to be current (capture)', async () => { |
106 | 108 | let called = 0; |
107 | 109 |
|
108 | 110 | function cb(e) { |
109 | 111 | called++; |
110 | 112 | expect(e.type).toBe('change'); |
111 | 113 | } |
112 | 114 |
|
113 | | - const node = ReactDOM.render( |
114 | | - <input type="text" onChangeCapture={cb} defaultValue="foo" />, |
115 | | - container, |
116 | | - ); |
| 115 | + const root = ReactDOMClient.createRoot(container); |
| 116 | + await act(() => { |
| 117 | + root.render( |
| 118 | + <input type="text" onChangeCapture={cb} defaultValue="foo" />, |
| 119 | + ); |
| 120 | + }); |
| 121 | + |
| 122 | + const node = container.firstChild; |
117 | 123 | node.dispatchEvent(new Event('input', {bubbles: true, cancelable: true})); |
118 | 124 | node.dispatchEvent(new Event('change', {bubbles: true, cancelable: true})); |
119 | 125 |
|
120 | 126 | // There should be no React change events because the value stayed the same. |
121 | 127 | expect(called).toBe(0); |
122 | 128 | }); |
123 | 129 |
|
124 | | - it('should not invoke a change event for textarea same value', () => { |
| 130 | + it('should not invoke a change event for textarea same value', async () => { |
125 | 131 | let called = 0; |
126 | 132 |
|
127 | 133 | function cb(e) { |
128 | 134 | called++; |
129 | 135 | expect(e.type).toBe('change'); |
130 | 136 | } |
131 | 137 |
|
132 | | - const node = ReactDOM.render( |
133 | | - <textarea onChange={cb} defaultValue="initial" />, |
134 | | - container, |
135 | | - ); |
| 138 | + const root = ReactDOMClient.createRoot(container); |
| 139 | + await act(() => { |
| 140 | + root.render(<textarea onChange={cb} defaultValue="initial" />); |
| 141 | + }); |
| 142 | + |
| 143 | + const node = container.firstChild; |
136 | 144 | node.dispatchEvent(new Event('input', {bubbles: true, cancelable: true})); |
137 | 145 | node.dispatchEvent(new Event('change', {bubbles: true, cancelable: true})); |
138 | 146 | // There should be no React change events because the value stayed the same. |
139 | 147 | expect(called).toBe(0); |
140 | 148 | }); |
141 | 149 |
|
142 | | - it('should not invoke a change event for textarea same value (capture)', () => { |
| 150 | + it('should not invoke a change event for textarea same value (capture)', async () => { |
143 | 151 | let called = 0; |
144 | 152 |
|
145 | 153 | function cb(e) { |
146 | 154 | called++; |
147 | 155 | expect(e.type).toBe('change'); |
148 | 156 | } |
149 | 157 |
|
150 | | - const node = ReactDOM.render( |
151 | | - <textarea onChangeCapture={cb} defaultValue="initial" />, |
152 | | - container, |
153 | | - ); |
| 158 | + const root = ReactDOMClient.createRoot(container); |
| 159 | + await act(() => { |
| 160 | + root.render(<textarea onChangeCapture={cb} defaultValue="initial" />); |
| 161 | + }); |
| 162 | + |
| 163 | + const node = container.firstChild; |
154 | 164 | node.dispatchEvent(new Event('input', {bubbles: true, cancelable: true})); |
155 | 165 | node.dispatchEvent(new Event('change', {bubbles: true, cancelable: true})); |
156 | 166 | // There should be no React change events because the value stayed the same. |
157 | 167 | expect(called).toBe(0); |
158 | 168 | }); |
159 | 169 |
|
160 | | - it('should consider initial checkbox checked=true to be current', () => { |
| 170 | + it('should consider initial checkbox checked=true to be current', async () => { |
161 | 171 | let called = 0; |
162 | 172 |
|
163 | 173 | function cb(e) { |
164 | 174 | called++; |
165 | 175 | expect(e.type).toBe('change'); |
166 | 176 | } |
167 | 177 |
|
168 | | - const node = ReactDOM.render( |
169 | | - <input type="checkbox" onChange={cb} defaultChecked={true} />, |
170 | | - container, |
171 | | - ); |
| 178 | + const root = ReactDOMClient.createRoot(container); |
| 179 | + await act(() => { |
| 180 | + root.render( |
| 181 | + <input type="checkbox" onChange={cb} defaultChecked={true} />, |
| 182 | + ); |
| 183 | + }); |
| 184 | + |
| 185 | + const node = container.firstChild; |
172 | 186 |
|
173 | 187 | // Secretly, set `checked` to false, so that dispatching the `click` will |
174 | 188 | // make it `true` again. Thus, at the time of the event, React should not |
@@ -181,18 +195,22 @@ describe('ChangeEventPlugin', () => { |
181 | 195 | expect(called).toBe(0); |
182 | 196 | }); |
183 | 197 |
|
184 | | - it('should consider initial checkbox checked=false to be current', () => { |
| 198 | + it('should consider initial checkbox checked=false to be current', async () => { |
185 | 199 | let called = 0; |
186 | 200 |
|
187 | 201 | function cb(e) { |
188 | 202 | called++; |
189 | 203 | expect(e.type).toBe('change'); |
190 | 204 | } |
191 | 205 |
|
192 | | - const node = ReactDOM.render( |
193 | | - <input type="checkbox" onChange={cb} defaultChecked={false} />, |
194 | | - container, |
195 | | - ); |
| 206 | + const root = ReactDOMClient.createRoot(container); |
| 207 | + await act(() => { |
| 208 | + root.render( |
| 209 | + <input type="checkbox" onChange={cb} defaultChecked={false} />, |
| 210 | + ); |
| 211 | + }); |
| 212 | + |
| 213 | + const node = container.firstChild; |
196 | 214 |
|
197 | 215 | // Secretly, set `checked` to true, so that dispatching the `click` will |
198 | 216 | // make it `false` again. Thus, at the time of the event, React should not |
@@ -205,18 +223,20 @@ describe('ChangeEventPlugin', () => { |
205 | 223 | expect(called).toBe(0); |
206 | 224 | }); |
207 | 225 |
|
208 | | - it('should fire change for checkbox input', () => { |
| 226 | + it('should fire change for checkbox input', async () => { |
209 | 227 | let called = 0; |
210 | 228 |
|
211 | 229 | function cb(e) { |
212 | 230 | called++; |
213 | 231 | expect(e.type).toBe('change'); |
214 | 232 | } |
215 | 233 |
|
216 | | - const node = ReactDOM.render( |
217 | | - <input type="checkbox" onChange={cb} />, |
218 | | - container, |
219 | | - ); |
| 234 | + const root = ReactDOMClient.createRoot(container); |
| 235 | + await act(() => { |
| 236 | + root.render(<input type="checkbox" onChange={cb} />); |
| 237 | + }); |
| 238 | + |
| 239 | + const node = container.firstChild; |
220 | 240 |
|
221 | 241 | expect(node.checked).toBe(false); |
222 | 242 | node.dispatchEvent( |
@@ -235,18 +255,20 @@ describe('ChangeEventPlugin', () => { |
235 | 255 | expect(called).toBe(2); |
236 | 256 | }); |
237 | 257 |
|
238 | | - it('should not fire change setting the value programmatically', () => { |
| 258 | + it('should not fire change setting the value programmatically', async () => { |
239 | 259 | let called = 0; |
240 | 260 |
|
241 | 261 | function cb(e) { |
242 | 262 | called++; |
243 | 263 | expect(e.type).toBe('change'); |
244 | 264 | } |
245 | 265 |
|
246 | | - const input = ReactDOM.render( |
247 | | - <input type="text" defaultValue="foo" onChange={cb} />, |
248 | | - container, |
249 | | - ); |
| 266 | + const root = ReactDOMClient.createRoot(container); |
| 267 | + await act(() => { |
| 268 | + root.render(<input type="text" defaultValue="foo" onChange={cb} />); |
| 269 | + }); |
| 270 | + |
| 271 | + const input = container.firstChild; |
250 | 272 |
|
251 | 273 | // Set it programmatically. |
252 | 274 | input.value = 'bar'; |
@@ -271,18 +293,20 @@ describe('ChangeEventPlugin', () => { |
271 | 293 | expect(called).toBe(1); |
272 | 294 | }); |
273 | 295 |
|
274 | | - it('should not distinguish equal string and number values', () => { |
| 296 | + it('should not distinguish equal string and number values', async () => { |
275 | 297 | let called = 0; |
276 | 298 |
|
277 | 299 | function cb(e) { |
278 | 300 | called++; |
279 | 301 | expect(e.type).toBe('change'); |
280 | 302 | } |
281 | 303 |
|
282 | | - const input = ReactDOM.render( |
283 | | - <input type="text" defaultValue="42" onChange={cb} />, |
284 | | - container, |
285 | | - ); |
| 304 | + const root = ReactDOMClient.createRoot(container); |
| 305 | + await act(() => { |
| 306 | + root.render(<input type="text" defaultValue="42" onChange={cb} />); |
| 307 | + }); |
| 308 | + |
| 309 | + const input = container.firstChild; |
286 | 310 |
|
287 | 311 | // When we set `value` as a property, React updates the "current" value |
288 | 312 | // that it tracks internally. The "current" value is later used to determine |
@@ -296,18 +320,22 @@ describe('ChangeEventPlugin', () => { |
296 | 320 | }); |
297 | 321 |
|
298 | 322 | // See a similar input test above for a detailed description of why. |
299 | | - it('should not fire change when setting checked programmatically', () => { |
| 323 | + it('should not fire change when setting checked programmatically', async () => { |
300 | 324 | let called = 0; |
301 | 325 |
|
302 | 326 | function cb(e) { |
303 | 327 | called++; |
304 | 328 | expect(e.type).toBe('change'); |
305 | 329 | } |
306 | 330 |
|
307 | | - const input = ReactDOM.render( |
308 | | - <input type="checkbox" onChange={cb} defaultChecked={false} />, |
309 | | - container, |
310 | | - ); |
| 331 | + const root = ReactDOMClient.createRoot(container); |
| 332 | + await act(() => { |
| 333 | + root.render( |
| 334 | + <input type="checkbox" onChange={cb} defaultChecked={false} />, |
| 335 | + ); |
| 336 | + }); |
| 337 | + |
| 338 | + const input = container.firstChild; |
311 | 339 |
|
312 | 340 | // Set the value, updating the "current" value that React tracks to true. |
313 | 341 | input.checked = true; |
@@ -326,32 +354,39 @@ describe('ChangeEventPlugin', () => { |
326 | 354 | expect(called).toBe(1); |
327 | 355 | }); |
328 | 356 |
|
329 | | - it('should unmount', () => { |
330 | | - const input = ReactDOM.render(<input />, container); |
| 357 | + it('should unmount', async () => { |
| 358 | + const root = ReactDOMClient.createRoot(container); |
| 359 | + await act(() => { |
| 360 | + root.render(<input />); |
| 361 | + }); |
| 362 | + |
| 363 | + const input = container.firstChild; |
331 | 364 |
|
332 | 365 | ReactDOM.unmountComponentAtNode(container); |
333 | 366 | }); |
334 | 367 |
|
335 | | - it('should only fire change for checked radio button once', () => { |
| 368 | + it('should only fire change for checked radio button once', async () => { |
336 | 369 | let called = 0; |
337 | 370 |
|
338 | 371 | function cb(e) { |
339 | 372 | called++; |
340 | 373 | expect(e.type).toBe('change'); |
341 | 374 | } |
342 | 375 |
|
343 | | - const input = ReactDOM.render( |
344 | | - <input type="radio" onChange={cb} />, |
345 | | - container, |
346 | | - ); |
| 376 | + const root = ReactDOMClient.createRoot(container); |
| 377 | + await act(() => { |
| 378 | + root.render(<input type="radio" onChange={cb} />); |
| 379 | + }); |
| 380 | + |
| 381 | + const input = container.firstChild; |
347 | 382 |
|
348 | 383 | setUntrackedChecked.call(input, true); |
349 | 384 | input.dispatchEvent(new Event('click', {bubbles: true, cancelable: true})); |
350 | 385 | input.dispatchEvent(new Event('click', {bubbles: true, cancelable: true})); |
351 | 386 | expect(called).toBe(1); |
352 | 387 | }); |
353 | 388 |
|
354 | | - it('should track radio button cousins in a group', () => { |
| 389 | + it('should track radio button cousins in a group', async () => { |
355 | 390 | let called1 = 0; |
356 | 391 | let called2 = 0; |
357 | 392 |
|
@@ -365,13 +400,17 @@ describe('ChangeEventPlugin', () => { |
365 | 400 | expect(e.type).toBe('change'); |
366 | 401 | } |
367 | 402 |
|
368 | | - const div = ReactDOM.render( |
369 | | - <div> |
370 | | - <input type="radio" name="group" onChange={cb1} /> |
371 | | - <input type="radio" name="group" onChange={cb2} /> |
372 | | - </div>, |
373 | | - container, |
374 | | - ); |
| 403 | + const root = ReactDOMClient.createRoot(container); |
| 404 | + await act(() => { |
| 405 | + root.render( |
| 406 | + <div> |
| 407 | + <input type="radio" name="group" onChange={cb1} /> |
| 408 | + <input type="radio" name="group" onChange={cb2} /> |
| 409 | + </div>, |
| 410 | + ); |
| 411 | + }); |
| 412 | + |
| 413 | + const div = container.firstChild; |
375 | 414 | const option1 = div.childNodes[0]; |
376 | 415 | const option2 = div.childNodes[1]; |
377 | 416 |
|
@@ -455,18 +494,20 @@ describe('ChangeEventPlugin', () => { |
455 | 494 | }); |
456 | 495 | }); |
457 | 496 |
|
458 | | - it('should listen for both change and input events when supported', () => { |
| 497 | + it('should listen for both change and input events when supported', async () => { |
459 | 498 | let called = 0; |
460 | 499 |
|
461 | 500 | function cb(e) { |
462 | 501 | called++; |
463 | 502 | expect(e.type).toBe('change'); |
464 | 503 | } |
465 | 504 |
|
466 | | - const input = ReactDOM.render( |
467 | | - <input type="range" onChange={cb} />, |
468 | | - container, |
469 | | - ); |
| 505 | + const root = ReactDOMClient.createRoot(container); |
| 506 | + await act(() => { |
| 507 | + root.render(<input type="range" onChange={cb} />); |
| 508 | + }); |
| 509 | + |
| 510 | + const input = container.firstChild; |
470 | 511 |
|
471 | 512 | setUntrackedValue.call(input, 10); |
472 | 513 | input.dispatchEvent(new Event('input', {bubbles: true, cancelable: true})); |
@@ -477,18 +518,20 @@ describe('ChangeEventPlugin', () => { |
477 | 518 | expect(called).toBe(2); |
478 | 519 | }); |
479 | 520 |
|
480 | | - it('should only fire events when the value changes for range inputs', () => { |
| 521 | + it('should only fire events when the value changes for range inputs', async () => { |
481 | 522 | let called = 0; |
482 | 523 |
|
483 | 524 | function cb(e) { |
484 | 525 | called++; |
485 | 526 | expect(e.type).toBe('change'); |
486 | 527 | } |
487 | 528 |
|
488 | | - const input = ReactDOM.render( |
489 | | - <input type="range" onChange={cb} />, |
490 | | - container, |
491 | | - ); |
| 529 | + const root = ReactDOMClient.createRoot(container); |
| 530 | + await act(() => { |
| 531 | + root.render(<input type="range" onChange={cb} />); |
| 532 | + }); |
| 533 | + |
| 534 | + const input = container.firstChild; |
492 | 535 | setUntrackedValue.call(input, '40'); |
493 | 536 | input.dispatchEvent(new Event('input', {bubbles: true, cancelable: true})); |
494 | 537 | input.dispatchEvent(new Event('change', {bubbles: true, cancelable: true})); |
|