GeezerVison is showing github.com — Improve tests that use discrete events (#20667) · react/react@9c32622
File treeExpand file treeCollapse file tree Open diff view settings react-reconciler/src/__tests__ react-refresh/src/__tests__
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMHooks-test.js +11-6Lines changed: 11 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 12 | 12 | let React; | 13 | 13 | let ReactDOM; | 14 | 14 | let Scheduler; | | 15 | +let act; | 15 | 16 |
| 16 | 17 | describe('ReactDOMHooks', () => { | 17 | 18 | let container; | @@ -22,6 +23,7 @@ describe('ReactDOMHooks', () => { | 22 | 23 | React = require('react'); | 23 | 24 | ReactDOM = require('react-dom'); | 24 | 25 | Scheduler = require('scheduler'); | | 26 | + act = require('react-dom/test-utils').unstable_concurrentAct; | 25 | 27 |
| 26 | 28 | container = document.createElement('div'); | 27 | 29 | document.body.appendChild(container); | @@ -106,7 +108,7 @@ describe('ReactDOMHooks', () => { | 106 | 108 | }); | 107 | 109 |
| 108 | 110 | // @gate experimental | 109 | | - it('should not bail out when an update is scheduled from within an event handler in Concurrent Mode', () => { | | 111 | + it('should not bail out when an update is scheduled from within an event handler in Concurrent Mode', async () => { | 110 | 112 | const {createRef, useCallback, useState} = React; | 111 | 113 |
| 112 | 114 | const Example = ({inputRef, labelRef}) => { | @@ -132,11 +134,14 @@ describe('ReactDOMHooks', () => { | 132 | 134 | Scheduler.unstable_flushAll(); | 133 | 135 |
| 134 | 136 | inputRef.current.value = 'abc'; | 135 | | - inputRef.current.dispatchEvent( | 136 | | - new Event('input', {bubbles: true, cancelable: true}), | 137 | | - ); | 138 | | - | 139 | | - Scheduler.unstable_flushAll(); | | 137 | + await act(async () => { | | 138 | + inputRef.current.dispatchEvent( | | 139 | + new Event('input', { | | 140 | + bubbles: true, | | 141 | + cancelable: true, | | 142 | + }), | | 143 | + ); | | 144 | + }); | 140 | 145 |
| 141 | 146 | expect(labelRef.current.innerHTML).toBe('abc'); | 142 | 147 | }); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -187,7 +187,7 @@ function runActTests(label, render, unmount, rerender) { | 187 | 187 | expect(Scheduler).toHaveYielded([100]); | 188 | 188 | }); | 189 | 189 |
| 190 | | - it('flushes effects on every call', () => { | | 190 | + it('flushes effects on every call', async () => { | 191 | 191 | function App() { | 192 | 192 | const [ctr, setCtr] = React.useState(0); | 193 | 193 | React.useEffect(() => { | @@ -209,16 +209,16 @@ function runActTests(label, render, unmount, rerender) { | 209 | 209 | button.dispatchEvent(new MouseEvent('click', {bubbles: true})); | 210 | 210 | } | 211 | 211 |
| 212 | | - act(() => { | | 212 | + await act(async () => { | 213 | 213 | click(); | 214 | 214 | click(); | 215 | 215 | click(); | 216 | 216 | }); | 217 | 217 | // it consolidates the 3 updates, then fires the effect | 218 | 218 | expect(Scheduler).toHaveYielded([3]); | 219 | | - act(click); | | 219 | + await act(async () => click()); | 220 | 220 | expect(Scheduler).toHaveYielded([4]); | 221 | | - act(click); | | 221 | + await act(async () => click()); | 222 | 222 | expect(Scheduler).toHaveYielded([5]); | 223 | 223 | expect(button.innerHTML).toBe('5'); | 224 | 224 | }); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/events/plugins/__tests__/ChangeEventPlugin-test.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -685,7 +685,7 @@ describe('ChangeEventPlugin', () => { | 685 | 685 | }); | 686 | 686 |
| 687 | 687 | // @gate experimental | 688 | | - it('is async for non-input events', () => { | | 688 | + it('is async for non-input events', async () => { | 689 | 689 | const root = ReactDOM.unstable_createRoot(container); | 690 | 690 | let input; | 691 | 691 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/events/plugins/__tests__/SimpleEventPlugin-test.js +12-8Lines changed: 12 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,6 +13,7 @@ describe('SimpleEventPlugin', function() { | 13 | 13 | let React; | 14 | 14 | let ReactDOM; | 15 | 15 | let Scheduler; | | 16 | + let TestUtils; | 16 | 17 |
| 17 | 18 | let onClick; | 18 | 19 | let container; | @@ -39,6 +40,7 @@ describe('SimpleEventPlugin', function() { | 39 | 40 | React = require('react'); | 40 | 41 | ReactDOM = require('react-dom'); | 41 | 42 | Scheduler = require('scheduler'); | | 43 | + TestUtils = require('react-dom/test-utils'); | 42 | 44 |
| 43 | 45 | onClick = jest.fn(); | 44 | 46 | }); | @@ -314,7 +316,7 @@ describe('SimpleEventPlugin', function() { | 314 | 316 | }); | 315 | 317 |
| 316 | 318 | // @gate experimental | 317 | | - it('end result of many interactive updates is deterministic', () => { | | 319 | + it('end result of many interactive updates is deterministic', async () => { | 318 | 320 | container = document.createElement('div'); | 319 | 321 | const root = ReactDOM.unstable_createRoot(container); | 320 | 322 | document.body.appendChild(container); | @@ -361,12 +363,14 @@ describe('SimpleEventPlugin', function() { | 361 | 363 | expect(button.textContent).toEqual('Count: 0'); | 362 | 364 |
| 363 | 365 | // Click the button many more times | 364 | | - click(); | 365 | | - click(); | 366 | | - click(); | 367 | | - click(); | 368 | | - click(); | 369 | | - click(); | | 366 | + await TestUtils.act(async () => { | | 367 | + click(); | | 368 | + click(); | | 369 | + click(); | | 370 | + click(); | | 371 | + click(); | | 372 | + click(); | | 373 | + }); | 370 | 374 |
| 371 | 375 | // Flush the remaining work | 372 | 376 | Scheduler.unstable_flushAll(); | @@ -375,7 +379,7 @@ describe('SimpleEventPlugin', function() { | 375 | 379 | }); | 376 | 380 |
| 377 | 381 | // @gate experimental | 378 | | - it('flushes discrete updates in order', () => { | | 382 | + it('flushes discrete updates in order', async () => { | 379 | 383 | container = document.createElement('div'); | 380 | 384 | document.body.appendChild(container); | 381 | 385 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactIncrementalErrorHandling-test.internal.js +27-24Lines changed: 27 additions & 24 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -232,7 +232,8 @@ describe('ReactIncrementalErrorHandling', () => { | 232 | 232 | expect(ReactNoop.getChildren()).toEqual([span('Caught an error: oops!')]); | 233 | 233 | }); | 234 | 234 |
| 235 | | - it("retries at a lower priority if there's additional pending work", () => { | | 235 | + // @gate experimental | | 236 | + it("retries at a lower priority if there's additional pending work", async () => { | 236 | 237 | function App(props) { | 237 | 238 | if (props.isBroken) { | 238 | 239 | Scheduler.unstable_yieldValue('error'); | @@ -252,14 +253,14 @@ describe('ReactIncrementalErrorHandling', () => { | 252 | 253 | }); | 253 | 254 | } | 254 | 255 |
| 255 | | - ReactNoop.discreteUpdates(() => { | 256 | | - ReactNoop.render(<App isBroken={true} />, onCommit); | 257 | | - }); | | 256 | + ReactNoop.render(<App isBroken={true} />, onCommit); | 258 | 257 | expect(Scheduler).toFlushAndYieldThrough(['error']); | 259 | 258 | interrupt(); | 260 | 259 |
| 261 | | - // This update is in a separate batch | 262 | | - ReactNoop.render(<App isBroken={false} />, onCommit); | | 260 | + React.unstable_startTransition(() => { | | 261 | + // This update is in a separate batch | | 262 | + ReactNoop.render(<App isBroken={false} />, onCommit); | | 263 | + }); | 263 | 264 |
| 264 | 265 | expect(Scheduler).toFlushAndYieldThrough([ | 265 | 266 | // The first render fails. But because there's a lower priority pending | @@ -311,16 +312,16 @@ describe('ReactIncrementalErrorHandling', () => { | 311 | 312 | }); | 312 | 313 | } | 313 | 314 |
| 314 | | - ReactNoop.discreteUpdates(() => { | 315 | | - ReactNoop.render(<App isBroken={true} />, onCommit); | 316 | | - }); | | 315 | + ReactNoop.render(<App isBroken={true} />, onCommit); | 317 | 316 | expect(Scheduler).toFlushAndYieldThrough(['error']); | 318 | 317 | interrupt(); | 319 | 318 |
| 320 | 319 | expect(ReactNoop).toMatchRenderedOutput(null); | 321 | 320 |
| 322 | | - // This update is in a separate batch | 323 | | - ReactNoop.render(<App isBroken={false} />, onCommit); | | 321 | + React.unstable_startTransition(() => { | | 322 | + // This update is in a separate batch | | 323 | + ReactNoop.render(<App isBroken={false} />, onCommit); | | 324 | + }); | 324 | 325 |
| 325 | 326 | expect(Scheduler).toFlushAndYieldThrough([ | 326 | 327 | // The first render fails. But because there's a lower priority pending | @@ -1786,6 +1787,7 @@ describe('ReactIncrementalErrorHandling', () => { | 1786 | 1787 | }); | 1787 | 1788 | } | 1788 | 1789 |
| | 1790 | + // @gate experimental | 1789 | 1791 | it('uncaught errors should be discarded if the render is aborted', async () => { | 1790 | 1792 | const root = ReactNoop.createRoot(); | 1791 | 1793 |
| @@ -1795,22 +1797,24 @@ describe('ReactIncrementalErrorHandling', () => { | 1795 | 1797 | } | 1796 | 1798 |
| 1797 | 1799 | await ReactNoop.act(async () => { | 1798 | | - ReactNoop.discreteUpdates(() => { | 1799 | | - root.render(<Oops />); | 1800 | | - }); | | 1800 | + root.render(<Oops />); | | 1801 | + | 1801 | 1802 | // Render past the component that throws, then yield. | 1802 | 1803 | expect(Scheduler).toFlushAndYieldThrough(['Oops']); | 1803 | 1804 | expect(root).toMatchRenderedOutput(null); | 1804 | 1805 | // Interleaved update. When the root completes, instead of throwing the | 1805 | 1806 | // error, it should try rendering again. This update will cause it to | 1806 | 1807 | // recover gracefully. | 1807 | | - root.render('Everything is fine.'); | | 1808 | + React.unstable_startTransition(() => { | | 1809 | + root.render('Everything is fine.'); | | 1810 | + }); | 1808 | 1811 | }); | 1809 | 1812 |
| 1810 | 1813 | // Should finish without throwing. | 1811 | 1814 | expect(root).toMatchRenderedOutput('Everything is fine.'); | 1812 | 1815 | }); | 1813 | 1816 |
| | 1817 | + // @gate experimental | 1814 | 1818 | it('uncaught errors are discarded if the render is aborted, case 2', async () => { | 1815 | 1819 | const {useState} = React; | 1816 | 1820 | const root = ReactNoop.createRoot(); | @@ -1835,21 +1839,20 @@ describe('ReactIncrementalErrorHandling', () => { | 1835 | 1839 | }); | 1836 | 1840 |
| 1837 | 1841 | await ReactNoop.act(async () => { | 1838 | | - // Schedule a high pri and a low pri update on the root. | 1839 | | - ReactNoop.discreteUpdates(() => { | 1840 | | - root.render(<Oops />); | | 1842 | + // Schedule a default pri and a low pri update on the root. | | 1843 | + root.render(<Oops />); | | 1844 | + React.unstable_startTransition(() => { | | 1845 | + root.render(<AllGood />); | 1841 | 1846 | }); | 1842 | | - root.render(<AllGood />); | 1843 | | - // Render through just the high pri update. The low pri update remains on | | 1847 | + | | 1848 | + // Render through just the default pri update. The low pri update remains on | 1844 | 1849 | // the queue. | 1845 | 1850 | expect(Scheduler).toFlushAndYieldThrough(['Everything is fine.']); | 1846 | 1851 |
| 1847 | | - // Schedule a high pri update on a child that triggers an error. | | 1852 | + // Schedule a default pri update on a child that triggers an error. | 1848 | 1853 | // The root should capture this error. But since there's still a pending | 1849 | 1854 | // update on the root, the error should be suppressed. | 1850 | | - ReactNoop.discreteUpdates(() => { | 1851 | | - setShouldThrow(true); | 1852 | | - }); | | 1855 | + setShouldThrow(true); | 1853 | 1856 | }); | 1854 | 1857 | // Should render the final state without throwing the error. | 1855 | 1858 | expect(Scheduler).toHaveYielded(['Everything is fine.']); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactSuspenseWithNoopRenderer-test.js +44-47Lines changed: 44 additions & 47 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -570,14 +570,13 @@ describe('ReactSuspenseWithNoopRenderer', () => { | 570 | 570 | ); | 571 | 571 | } | 572 | 572 |
| 573 | | - // Schedule a high pri update and a low pri update, without rendering in | 574 | | - // between. | 575 | | - ReactNoop.discreteUpdates(() => { | 576 | | - // High pri | 577 | | - ReactNoop.render(<App />); | 578 | | - }); | | 573 | + // Schedule a default pri update and a low pri update, without rendering in between. | | 574 | + // Default pri | | 575 | + ReactNoop.render(<App />); | 579 | 576 | // Low pri | 580 | | - ReactNoop.render(<App hide={true} />); | | 577 | + React.unstable_startTransition(() => { | | 578 | + ReactNoop.render(<App hide={true} />); | | 579 | + }); | 581 | 580 |
| 582 | 581 | expect(Scheduler).toFlushAndYield([ | 583 | 582 | // The first update suspends | @@ -1879,9 +1878,7 @@ describe('ReactSuspenseWithNoopRenderer', () => { | 1879 | 1878 | ReactNoop.render(<Foo />); | 1880 | 1879 | expect(Scheduler).toFlushAndYield(['Foo']); | 1881 | 1880 |
| 1882 | | - ReactNoop.discreteUpdates(() => | 1883 | | - ReactNoop.render(<Foo renderContent={true} />), | 1884 | | - ); | | 1881 | + ReactNoop.render(<Foo renderContent={true} />); | 1885 | 1882 | expect(Scheduler).toFlushAndYieldThrough(['Foo']); | 1886 | 1883 |
| 1887 | 1884 | // Advance some time. | @@ -3080,48 +3077,48 @@ describe('ReactSuspenseWithNoopRenderer', () => { | 3080 | 3077 | // Schedule an update inside the Suspense boundary that suspends. | 3081 | 3078 | setAppText('B'); | 3082 | 3079 | expect(Scheduler).toFlushAndYield(['Suspend! [B]', 'Loading...']); | | 3080 | + }); | 3083 | 3081 |
| 3084 | | - // Commit the placeholder | 3085 | | - await advanceTimers(250); | 3086 | | - expect(root).toMatchRenderedOutput( | 3087 | | - <> | 3088 | | - <span hidden={true} prop="A" /> | 3089 | | - <span prop="Loading..." /> | 3090 | | - </>, | 3091 | | - ); | | 3082 | + expect(root).toMatchRenderedOutput( | | 3083 | + <> | | 3084 | + <span hidden={true} prop="A" /> | | 3085 | + <span prop="Loading..." /> | | 3086 | + </>, | | 3087 | + ); | 3092 | 3088 |
| 3093 | | - // Schedule a high pri update on the boundary, and a lower pri update | 3094 | | - // on the fallback. We're testing to make sure the fallback can still | 3095 | | - // update even though the primary tree is suspended.{ | 3096 | | - ReactNoop.discreteUpdates(() => { | 3097 | | - setAppText('C'); | | 3089 | + // Schedule a default pri update on the boundary, and a lower pri update | | 3090 | + // on the fallback. We're testing to make sure the fallback can still | | 3091 | + // update even though the primary tree is suspended. | | 3092 | + await ReactNoop.act(async () => { | | 3093 | + setAppText('C'); | | 3094 | + React.unstable_startTransition(() => { | | 3095 | + setFallbackText('Still loading...'); | 3098 | 3096 | }); | 3099 | | - setFallbackText('Still loading...'); | | 3097 | + }); | 3100 | 3098 |
| 3101 | | - expect(Scheduler).toFlushAndYield([ | 3102 | | - // First try to render the high pri update. Still suspended. | 3103 | | - 'Suspend! [C]', | 3104 | | - 'Loading...', | | 3099 | + expect(Scheduler).toHaveYielded([ | | 3100 | + // First try to render the high pri update. Still suspended. | | 3101 | + 'Suspend! [C]', | | 3102 | + 'Loading...', | 3105 | 3103 |
| 3106 | | - // In the expiration times model, once the high pri update suspends, | 3107 | | - // we can't be sure if there's additional work at a lower priority | 3108 | | - // that might unblock the tree. We do know that there's a lower | 3109 | | - // priority update *somehwere* in the entire root, though (the update | 3110 | | - // to the fallback). So we try rendering one more time, just in case. | 3111 | | - // TODO: We shouldn't need to do this with lanes, because we always | 3112 | | - // know exactly which lanes have pending work in each tree. | 3113 | | - 'Suspend! [C]', | 3114 | | - | 3115 | | - // Then complete the update to the fallback. | 3116 | | - 'Still loading...', | 3117 | | - ]); | 3118 | | - expect(root).toMatchRenderedOutput( | 3119 | | - <> | 3120 | | - <span hidden={true} prop="A" /> | 3121 | | - <span prop="Still loading..." /> | 3122 | | - </>, | 3123 | | - ); | 3124 | | - }); | | 3104 | + // In the expiration times model, once the high pri update suspends, | | 3105 | + // we can't be sure if there's additional work at a lower priority | | 3106 | + // that might unblock the tree. We do know that there's a lower | | 3107 | + // priority update *somehwere* in the entire root, though (the update | | 3108 | + // to the fallback). So we try rendering one more time, just in case. | | 3109 | + // TODO: We shouldn't need to do this with lanes, because we always | | 3110 | + // know exactly which lanes have pending work in each tree. | | 3111 | + 'Suspend! [C]', | | 3112 | + | | 3113 | + // Then complete the update to the fallback. | | 3114 | + 'Still loading...', | | 3115 | + ]); | | 3116 | + expect(root).toMatchRenderedOutput( | | 3117 | + <> | | 3118 | + <span hidden={true} prop="A" /> | | 3119 | + <span prop="Still loading..." /> | | 3120 | + </>, | | 3121 | + ); | 3125 | 3122 | }, | 3126 | 3123 | ); | 3127 | 3124 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/useMutableSource-test.internal.js +5-4Lines changed: 5 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1397,12 +1397,13 @@ describe('useMutableSource', () => { | 1397 | 1397 | // Now mutate A. Both hooks should update. | 1398 | 1398 | // This is at high priority so that it doesn't get batched with default | 1399 | 1399 | // priority updates that might fire during the passive effect | 1400 | | - ReactNoop.discreteUpdates(() => { | 1401 | | - mutateA('a1'); | | 1400 | + await ReactNoop.act(async () => { | | 1401 | + ReactNoop.discreteUpdates(() => { | | 1402 | + mutateA('a1'); | | 1403 | + }); | 1402 | 1404 | }); | 1403 | | - expect(Scheduler).toFlushUntilNextPaint([]); | 1404 | 1405 |
| 1405 | | - expect(root.getChildrenAsJSX()).toEqual('first: a1, second: a1'); | | 1406 | + expect(root).toMatchRenderedOutput('first: a1, second: a1'); | 1406 | 1407 | }); | 1407 | 1408 |
| 1408 | 1409 | expect(root.getChildrenAsJSX()).toEqual('first: a1, second: a1'); | |
|