Skip to content

Commit 0eb8ec4

Browse files
committed
[tests] Fix assertions not flushed before act
1 parent a5aedd1 commit 0eb8ec4

9 files changed

+29
-23
lines changed

packages/react-dom/src/__tests__/ReactCompositeComponent-test.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -910,15 +910,13 @@ describe('ReactCompositeComponent', () => {
910910
await act(() => {
911911
root.render(<Wrapper name="A" />);
912912
});
913+
914+
assertLog(['A componentWillMount', 'A render', 'A componentDidMount']);
913915
await act(() => {
914916
root.render(<Wrapper name="B" />);
915917
});
916918

917919
assertLog([
918-
'A componentWillMount',
919-
'A render',
920-
'A componentDidMount',
921-
922920
'B componentWillMount',
923921
'B render',
924922
'A componentWillUnmount',

packages/react-dom/src/__tests__/ReactDOMFizzDeferredValue-test.js

+2
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@ describe('ReactDOMFizzForm', () => {
111111
await readIntoContainer(stream);
112112
expect(container.textContent).toEqual('Loading...');
113113

114+
assertLog(['Loading...']);
114115
// After hydration, it's updated to the final value
115116
await act(() => ReactDOMClient.hydrateRoot(container, <App />));
116117
expect(container.textContent).toEqual('Final');
118+
assertLog(['Loading...', 'Final']);
117119
},
118120
);
119121

packages/react-dom/src/__tests__/ReactDOMNativeEventHeuristic-test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ describe('ReactDOMNativeEventHeuristic-test', () => {
297297
});
298298
expect(container.textContent).toEqual('not hovered');
299299

300+
assertLog(['not hovered']);
300301
await act(async () => {
301302
// Note: React does not use native mouseenter/mouseleave events
302303
// but we should still correctly determine their priority.
@@ -308,7 +309,7 @@ describe('ReactDOMNativeEventHeuristic-test', () => {
308309
dispatchAndSetCurrentEvent(target.current, mouseEnterEvent);
309310

310311
// Since mouse end is not discrete, should not have updated yet
311-
assertLog(['not hovered']);
312+
assertLog([]);
312313
expect(container.textContent).toEqual('not hovered');
313314

314315
await waitFor(['hovered']);

packages/react-dom/src/__tests__/ReactDOMRoot-test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,13 @@ describe('ReactDOMRoot', () => {
342342
root.render(<Foo value="a" />);
343343
});
344344

345+
assertLog(['a']);
345346
expect(container.textContent).toEqual('a');
346347

347348
await act(async () => {
348349
root.render(<Foo value="b" />);
349350

350-
assertLog(['a']);
351+
assertLog([]);
351352
expect(container.textContent).toEqual('a');
352353

353354
await waitFor(['b']);

packages/react-dom/src/__tests__/ReactDOMSuspensePlaceholder-test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ let Suspense;
1717
let Scheduler;
1818
let act;
1919
let textCache;
20+
let assertLog;
2021

2122
describe('ReactDOMSuspensePlaceholder', () => {
2223
let container;
@@ -30,6 +31,7 @@ describe('ReactDOMSuspensePlaceholder', () => {
3031
ReactDOMClient = require('react-dom/client');
3132
Scheduler = require('scheduler');
3233
act = require('internal-test-utils').act;
34+
assertLog = require('internal-test-utils').assertLog;
3335
Suspense = React.Suspense;
3436
container = document.createElement('div');
3537
document.body.appendChild(container);
@@ -156,11 +158,11 @@ describe('ReactDOMSuspensePlaceholder', () => {
156158
});
157159

158160
expect(container.textContent).toEqual('Loading...');
159-
161+
assertLog(['A', 'Suspend! [B]', 'Loading...']);
160162
await act(() => {
161163
resolveText('B');
162164
});
163-
165+
assertLog(['A', 'B', 'C']);
164166
expect(container.textContent).toEqual('ABC');
165167
});
166168

packages/react-dom/src/__tests__/ReactEmptyComponent-test.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,14 @@ describe('ReactEmptyComponent', () => {
114114
root1.render(instance1);
115115
});
116116

117+
assertLog(['mount undefined', 'update DIV']);
118+
117119
const root2 = ReactDOMClient.createRoot(container2);
118120
await act(() => {
119121
root2.render(instance2);
120122
});
121123

122-
assertLog([
123-
'mount undefined',
124-
'update DIV',
125-
'mount DIV',
126-
'update undefined',
127-
]);
124+
assertLog(['mount DIV', 'update undefined']);
128125
});
129126

130127
it('should be able to switch in a list of children', async () => {

packages/react-reconciler/src/__tests__/ReactUpdaters-test.internal.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,13 @@ describe('updaters', () => {
112112
root.render(<Parent />);
113113
});
114114
expect(allSchedulerTags).toEqual([[HostRoot]]);
115+
assertLog(['onCommitRoot']);
115116

116117
await act(() => {
117118
root.render(<Parent />);
118119
});
119120
expect(allSchedulerTags).toEqual([[HostRoot], [HostRoot]]);
121+
assertLog(['onCommitRoot']);
120122
});
121123

122124
it('should report a function component as the scheduler for a hooks update', async () => {
@@ -148,12 +150,13 @@ describe('updaters', () => {
148150
expect(scheduleForA).not.toBeNull();
149151
expect(scheduleForB).not.toBeNull();
150152
expect(allSchedulerTypes).toEqual([[null]]);
153+
assertLog(['onCommitRoot']);
151154

152155
await act(() => {
153156
scheduleForA();
154157
});
155158
expect(allSchedulerTypes).toEqual([[null], [SchedulingComponentA]]);
156-
159+
assertLog(['onCommitRoot']);
157160
await act(() => {
158161
scheduleForB();
159162
});
@@ -162,6 +165,7 @@ describe('updaters', () => {
162165
[SchedulingComponentA],
163166
[SchedulingComponentB],
164167
]);
168+
assertLog(['onCommitRoot']);
165169
});
166170

167171
it('should report a class component as the scheduler for a setState update', async () => {
@@ -180,7 +184,7 @@ describe('updaters', () => {
180184
root.render(<Parent />);
181185
});
182186
expect(allSchedulerTypes).toEqual([[null]]);
183-
187+
assertLog(['onCommitRoot']);
184188
expect(instance).not.toBeNull();
185189
await act(() => {
186190
instance.setState({});

packages/use-subscription/src/__tests__/useSubscription-test.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -338,20 +338,16 @@ describe('useSubscription', () => {
338338
observableB.next('b-3');
339339
});
340340

341+
assertLog(['Grandchild: b-0', 'Child: b-3', 'Grandchild: b-3']);
342+
341343
// Update again
342344
await act(() => root.render(<Parent observed={observableA} />));
343345

344346
// Flush everything and ensure that the correct subscribable is used
345347
// We expect the last emitted update to be rendered (because of the commit phase value check)
346348
// But the intermediate ones should be ignored,
347349
// 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']);
355351
expect(log).toEqual([
356352
'Parent.componentDidMount',
357353
'Parent.componentDidUpdate',

packages/use-sync-external-store/src/__tests__/useSyncExternalStoreShared-test.js

+5
Original file line numberDiff line numberDiff line change
@@ -660,14 +660,17 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
660660
// Object.is algorithm to compare values.
661661
await act(() => root.render(<App />));
662662
expect(container.textContent).toEqual('NaN');
663+
assertLog([NaN]);
663664

664665
// Update to real number
665666
await act(() => store.set(123));
666667
expect(container.textContent).toEqual('123');
668+
assertLog([123]);
667669

668670
// Update back to NaN
669671
await act(() => store.set('not a number'));
670672
expect(container.textContent).toEqual('NaN');
673+
assertLog([NaN]);
671674
});
672675

673676
describe('extra features implemented in user-space', () => {
@@ -968,6 +971,7 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
968971
),
969972
);
970973

974+
assertLog(['A']);
971975
expect(container.textContent).toEqual('A');
972976

973977
if (__DEV__ && gate(flags => flags.enableUseSyncExternalStoreShim)) {
@@ -1017,6 +1021,7 @@ describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
10171021
),
10181022
);
10191023

1024+
assertLog(['A']);
10201025
expect(container.textContent).toEqual('A');
10211026

10221027
if (__DEV__ && gate(flags => flags.enableUseSyncExternalStoreShim)) {

0 commit comments

Comments
 (0)