GeezerVison is showing github.com — [tests] Fix assertions not flushed before act (#28745) · react/react-native@42eff4b
File treeExpand file treeCollapse file tree Open diff view settings react-reconciler/src/__tests__ use-subscription/src/__tests__ use-sync-external-store/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__/ReactCompositeComponent-test.js +2-4Lines changed: 2 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -913,15 +913,13 @@ describe('ReactCompositeComponent', () => { | 913 | 913 | await act(() => { | 914 | 914 | root.render(<Wrapper name="A" />); | 915 | 915 | }); | | 916 | + | | 917 | + assertLog(['A componentWillMount', 'A render', 'A componentDidMount']); | 916 | 918 | await act(() => { | 917 | 919 | root.render(<Wrapper name="B" />); | 918 | 920 | }); | 919 | 921 |
| 920 | 922 | assertLog([ | 921 | | - 'A componentWillMount', | 922 | | - 'A render', | 923 | | - 'A componentDidMount', | 924 | | - | 925 | 923 | 'B componentWillMount', | 926 | 924 | 'B render', | 927 | 925 | 'A componentWillUnmount', | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMFizzDeferredValue-test.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -111,9 +111,11 @@ describe('ReactDOMFizzForm', () => { | 111 | 111 | await readIntoContainer(stream); | 112 | 112 | expect(container.textContent).toEqual('Loading...'); | 113 | 113 |
| | 114 | + assertLog(['Loading...']); | 114 | 115 | // After hydration, it's updated to the final value | 115 | 116 | await act(() => ReactDOMClient.hydrateRoot(container, <App />)); | 116 | 117 | expect(container.textContent).toEqual('Final'); | | 118 | + assertLog(['Loading...', 'Final']); | 117 | 119 | }, | 118 | 120 | ); | 119 | 121 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMNativeEventHeuristic-test.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -297,6 +297,7 @@ describe('ReactDOMNativeEventHeuristic-test', () => { | 297 | 297 | }); | 298 | 298 | expect(container.textContent).toEqual('not hovered'); | 299 | 299 |
| | 300 | + assertLog(['not hovered']); | 300 | 301 | await act(async () => { | 301 | 302 | // Note: React does not use native mouseenter/mouseleave events | 302 | 303 | // but we should still correctly determine their priority. | @@ -308,7 +309,7 @@ describe('ReactDOMNativeEventHeuristic-test', () => { | 308 | 309 | dispatchAndSetCurrentEvent(target.current, mouseEnterEvent); | 309 | 310 |
| 310 | 311 | // Since mouse end is not discrete, should not have updated yet | 311 | | - assertLog(['not hovered']); | | 312 | + assertLog([]); | 312 | 313 | expect(container.textContent).toEqual('not hovered'); | 313 | 314 |
| 314 | 315 | await waitFor(['hovered']); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMRoot-test.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -342,12 +342,13 @@ describe('ReactDOMRoot', () => { | 342 | 342 | root.render(<Foo value="a" />); | 343 | 343 | }); | 344 | 344 |
| | 345 | + assertLog(['a']); | 345 | 346 | expect(container.textContent).toEqual('a'); | 346 | 347 |
| 347 | 348 | await act(async () => { | 348 | 349 | root.render(<Foo value="b" />); | 349 | 350 |
| 350 | | - assertLog(['a']); | | 351 | + assertLog([]); | 351 | 352 | expect(container.textContent).toEqual('a'); | 352 | 353 |
| 353 | 354 | await waitFor(['b']); | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMSuspensePlaceholder-test.js +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,6 +17,7 @@ let Suspense; | 17 | 17 | let Scheduler; | 18 | 18 | let act; | 19 | 19 | let textCache; | | 20 | +let assertLog; | 20 | 21 |
| 21 | 22 | describe('ReactDOMSuspensePlaceholder', () => { | 22 | 23 | let container; | @@ -31,6 +32,7 @@ describe('ReactDOMSuspensePlaceholder', () => { | 31 | 32 | ReactDOMClient = require('react-dom/client'); | 32 | 33 | Scheduler = require('scheduler'); | 33 | 34 | act = require('internal-test-utils').act; | | 35 | + assertLog = require('internal-test-utils').assertLog; | 34 | 36 | Suspense = React.Suspense; | 35 | 37 | container = document.createElement('div'); | 36 | 38 | document.body.appendChild(container); | @@ -157,11 +159,11 @@ describe('ReactDOMSuspensePlaceholder', () => { | 157 | 159 | }); | 158 | 160 |
| 159 | 161 | expect(container.textContent).toEqual('Loading...'); | 160 | | - | | 162 | + assertLog(['A', 'Suspend! [B]', 'Loading...']); | 161 | 163 | await act(() => { | 162 | 164 | resolveText('B'); | 163 | 165 | }); | 164 | | - | | 166 | + assertLog(['A', 'B', 'C']); | 165 | 167 | expect(container.textContent).toEqual('ABC'); | 166 | 168 | }); | 167 | 169 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactEmptyComponent-test.js +3-6Lines changed: 3 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -115,17 +115,14 @@ describe('ReactEmptyComponent', () => { | 115 | 115 | root1.render(instance1); | 116 | 116 | }); | 117 | 117 |
| | 118 | + assertLog(['mount undefined', 'update DIV']); | | 119 | + | 118 | 120 | const root2 = ReactDOMClient.createRoot(container2); | 119 | 121 | await act(() => { | 120 | 122 | root2.render(instance2); | 121 | 123 | }); | 122 | 124 |
| 123 | | - assertLog([ | 124 | | - 'mount undefined', | 125 | | - 'update DIV', | 126 | | - 'mount DIV', | 127 | | - 'update undefined', | 128 | | - ]); | | 125 | + assertLog(['mount DIV', 'update undefined']); | 129 | 126 | }); | 130 | 127 |
| 131 | 128 | it('should be able to switch in a list of children', async () => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/__tests__/ReactUpdaters-test.internal.js +6-2Lines changed: 6 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -112,11 +112,13 @@ describe('updaters', () => { | 112 | 112 | root.render(<Parent />); | 113 | 113 | }); | 114 | 114 | expect(allSchedulerTags).toEqual([[HostRoot]]); | | 115 | + assertLog(['onCommitRoot']); | 115 | 116 |
| 116 | 117 | await act(() => { | 117 | 118 | root.render(<Parent />); | 118 | 119 | }); | 119 | 120 | expect(allSchedulerTags).toEqual([[HostRoot], [HostRoot]]); | | 121 | + assertLog(['onCommitRoot']); | 120 | 122 | }); | 121 | 123 |
| 122 | 124 | it('should report a function component as the scheduler for a hooks update', async () => { | @@ -148,12 +150,13 @@ describe('updaters', () => { | 148 | 150 | expect(scheduleForA).not.toBeNull(); | 149 | 151 | expect(scheduleForB).not.toBeNull(); | 150 | 152 | expect(allSchedulerTypes).toEqual([[null]]); | | 153 | + assertLog(['onCommitRoot']); | 151 | 154 |
| 152 | 155 | await act(() => { | 153 | 156 | scheduleForA(); | 154 | 157 | }); | 155 | 158 | expect(allSchedulerTypes).toEqual([[null], [SchedulingComponentA]]); | 156 | | - | | 159 | + assertLog(['onCommitRoot']); | 157 | 160 | await act(() => { | 158 | 161 | scheduleForB(); | 159 | 162 | }); | @@ -162,6 +165,7 @@ describe('updaters', () => { | 162 | 165 | [SchedulingComponentA], | 163 | 166 | [SchedulingComponentB], | 164 | 167 | ]); | | 168 | + assertLog(['onCommitRoot']); | 165 | 169 | }); | 166 | 170 |
| 167 | 171 | it('should report a class component as the scheduler for a setState update', async () => { | @@ -180,7 +184,7 @@ describe('updaters', () => { | 180 | 184 | root.render(<Parent />); | 181 | 185 | }); | 182 | 186 | expect(allSchedulerTypes).toEqual([[null]]); | 183 | | - | | 187 | + assertLog(['onCommitRoot']); | 184 | 188 | expect(instance).not.toBeNull(); | 185 | 189 | await act(() => { | 186 | 190 | instance.setState({}); | |
Collapse file Copy file name to clipboardExpand all lines: packages/use-subscription/src/__tests__/useSubscription-test.js +3-7Lines changed: 3 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -338,20 +338,16 @@ describe('useSubscription', () => { | 338 | 338 | observableB.next('b-3'); | 339 | 339 | }); | 340 | 340 |
| | 341 | + assertLog(['Grandchild: b-0', 'Child: b-3', 'Grandchild: b-3']); | | 342 | + | 341 | 343 | // Update again | 342 | 344 | await act(() => root.render(<Parent observed={observableA} />)); | 343 | 345 |
| 344 | 346 | // Flush everything and ensure that the correct subscribable is used | 345 | 347 | // We expect the last emitted update to be rendered (because of the commit phase value check) | 346 | 348 | // But the intermediate ones should be ignored, | 347 | 349 | // And the final rendered output should be the higher-priority observable. | 348 | | - assertLog([ | 349 | | - 'Grandchild: b-0', | 350 | | - 'Child: b-3', | 351 | | - 'Grandchild: b-3', | 352 | | - 'Child: a-0', | 353 | | - 'Grandchild: a-0', | 354 | | - ]); | | 350 | + assertLog(['Child: a-0', 'Grandchild: a-0']); | 355 | 351 | expect(log).toEqual([ | 356 | 352 | 'Parent.componentDidMount', | 357 | 353 | 'Parent.componentDidUpdate', | |
Collapse file Copy file name to clipboardExpand all lines: packages/use-sync-external-store/src/__tests__/useSyncExternalStoreShared-test.js +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -660,14 +660,17 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => { | 660 | 660 | // Object.is algorithm to compare values. | 661 | 661 | await act(() => root.render(<App />)); | 662 | 662 | expect(container.textContent).toEqual('NaN'); | | 663 | + assertLog([NaN]); | 663 | 664 |
| 664 | 665 | // Update to real number | 665 | 666 | await act(() => store.set(123)); | 666 | 667 | expect(container.textContent).toEqual('123'); | | 668 | + assertLog([123]); | 667 | 669 |
| 668 | 670 | // Update back to NaN | 669 | 671 | await act(() => store.set('not a number')); | 670 | 672 | expect(container.textContent).toEqual('NaN'); | | 673 | + assertLog([NaN]); | 671 | 674 | }); | 672 | 675 |
| 673 | 676 | describe('extra features implemented in user-space', () => { | @@ -968,6 +971,7 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => { | 968 | 971 | ), | 969 | 972 | ); | 970 | 973 |
| | 974 | + assertLog(['A']); | 971 | 975 | expect(container.textContent).toEqual('A'); | 972 | 976 |
| 973 | 977 | if (__DEV__ && gate(flags => flags.enableUseSyncExternalStoreShim)) { | @@ -1017,6 +1021,7 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => { | 1017 | 1021 | ), | 1018 | 1022 | ); | 1019 | 1023 |
| | 1024 | + assertLog(['A']); | 1020 | 1025 | expect(container.textContent).toEqual('A'); | 1021 | 1026 |
| 1022 | 1027 | if (__DEV__ && gate(flags => flags.enableUseSyncExternalStoreShim)) { | |
|