Skip to content

Commit f0aafa1

Browse files
authored
Convert a few more tests to waitFor test helpers (#26509)
Continuing my journey to migrate all the Scheduler flush* methods to async versions of the same helpers.
1 parent 90995ef commit f0aafa1

File tree

5 files changed

+49
-44
lines changed

5 files changed

+49
-44
lines changed

Diff for: packages/internal-test-utils/ReactInternalTestUtils.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@ import enqueueTask from './enqueueTask';
1212

1313
export {act} from './internalAct';
1414

15-
function assertYieldsWereCleared(Scheduler) {
16-
const actualYields = Scheduler.unstable_clearLog();
15+
function assertYieldsWereCleared(caller) {
16+
const actualYields = SchedulerMock.unstable_clearLog();
1717
if (actualYields.length !== 0) {
1818
const error = Error(
1919
'The event log is not empty. Call assertLog(...) first.',
2020
);
21-
Error.captureStackTrace(error, assertYieldsWereCleared);
21+
Error.captureStackTrace(error, caller);
2222
throw error;
2323
}
2424
}
2525

26-
async function waitForMicrotasks() {
26+
export async function waitForMicrotasks() {
2727
return new Promise(resolve => {
2828
enqueueTask(() => resolve());
2929
});
3030
}
3131

3232
export async function waitFor(expectedLog, options) {
33-
assertYieldsWereCleared(SchedulerMock);
33+
assertYieldsWereCleared(waitFor);
3434

3535
// Create the error object before doing any async work, to get a better
3636
// stack trace.
@@ -79,7 +79,7 @@ ${diff(expectedLog, actualLog)}
7979
}
8080

8181
export async function waitForAll(expectedLog) {
82-
assertYieldsWereCleared(SchedulerMock);
82+
assertYieldsWereCleared(waitForAll);
8383

8484
// Create the error object before doing any async work, to get a better
8585
// stack trace.
@@ -110,7 +110,7 @@ ${diff(expectedLog, actualLog)}
110110
}
111111

112112
export async function waitForThrow(expectedError: mixed): mixed {
113-
assertYieldsWereCleared(SchedulerMock);
113+
assertYieldsWereCleared(waitForThrow);
114114

115115
// Create the error object before doing any async work, to get a better
116116
// stack trace.
@@ -160,7 +160,7 @@ ${diff(expectedError, x)}
160160
// avoid using it in tests. It's really only for testing a particular
161161
// implementation detail (update starvation prevention).
162162
export async function unstable_waitForExpired(expectedLog): mixed {
163-
assertYieldsWereCleared(SchedulerMock);
163+
assertYieldsWereCleared(unstable_waitForExpired);
164164

165165
// Create the error object before doing any async work, to get a better
166166
// stack trace.
@@ -189,7 +189,7 @@ ${diff(expectedLog, actualLog)}
189189
// now because that's how untable_flushUntilNextPaint already worked, but maybe
190190
// we should split these use cases into separate APIs.
191191
export async function waitForPaint(expectedLog) {
192-
assertYieldsWereCleared(SchedulerMock);
192+
assertYieldsWereCleared(waitForPaint);
193193

194194
// Create the error object before doing any async work, to get a better
195195
// stack trace.
@@ -219,7 +219,7 @@ ${diff(expectedLog, actualLog)}
219219
}
220220

221221
export async function waitForDiscrete(expectedLog) {
222-
assertYieldsWereCleared(SchedulerMock);
222+
assertYieldsWereCleared(waitForDiscrete);
223223

224224
// Create the error object before doing any async work, to get a better
225225
// stack trace.

Diff for: packages/react-dom/src/__tests__/ReactMultiChildText-test.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
'use strict';
1111

1212
const React = require('react');
13-
const ReactDOM = require('react-dom');
13+
const ReactDOMClient = require('react-dom/client');
1414
const ReactTestUtils = require('react-dom/test-utils');
15+
const act = require('internal-test-utils').act;
1516

1617
// Helpers
17-
const testAllPermutations = function (testCases) {
18+
const testAllPermutations = async function (testCases) {
1819
for (let i = 0; i < testCases.length; i += 2) {
1920
const renderWithChildren = testCases[i];
2021
const expectedResultAfterRender = testCases[i + 1];
@@ -24,10 +25,11 @@ const testAllPermutations = function (testCases) {
2425
const expectedResultAfterUpdate = testCases[j + 1];
2526

2627
const container = document.createElement('div');
27-
ReactDOM.render(<div>{renderWithChildren}</div>, container);
28+
const root = ReactDOMClient.createRoot(container);
29+
await act(() => root.render(<div>{renderWithChildren}</div>));
2830
expectChildren(container, expectedResultAfterRender);
2931

30-
ReactDOM.render(<div>{updateWithChildren}</div>, container);
32+
await act(() => root.render(<div>{updateWithChildren}</div>));
3133
expectChildren(container, expectedResultAfterUpdate);
3234
}
3335
}
@@ -75,10 +77,12 @@ const expectChildren = function (container, children) {
7577
* faster to render and update.
7678
*/
7779
describe('ReactMultiChildText', () => {
78-
it('should correctly handle all possible children for render and update', () => {
79-
expect(() => {
80+
jest.setTimeout(20000);
81+
82+
it('should correctly handle all possible children for render and update', async () => {
83+
await expect(async () => {
8084
// prettier-ignore
81-
testAllPermutations([
85+
await testAllPermutations([
8286
// basic values
8387
undefined, [],
8488
null, [],

Diff for: packages/react-reconciler/src/__tests__/ReactHooks-test.internal.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -1703,17 +1703,14 @@ describe('ReactHooks', () => {
17031703
return null;
17041704
}
17051705

1706-
await act(() => {
1707-
ReactTestRenderer.unstable_batchedUpdates(() => {
1708-
ReactTestRenderer.create(
1709-
<>
1710-
<A />
1711-
<B />
1712-
</>,
1713-
);
1714-
expect(() => Scheduler.unstable_flushAll()).toThrow('Hello');
1715-
});
1716-
});
1706+
expect(() => {
1707+
ReactTestRenderer.create(
1708+
<>
1709+
<A />
1710+
<B />
1711+
</>,
1712+
);
1713+
}).toThrow('Hello');
17171714

17181715
if (__DEV__) {
17191716
expect(console.error).toHaveBeenCalledTimes(2);

Diff for: packages/react-reconciler/src/__tests__/ReactIsomorphicAct-test.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ let use;
1616
let Suspense;
1717
let DiscreteEventPriority;
1818
let startTransition;
19+
let waitForMicrotasks;
1920

2021
describe('isomorphic act()', () => {
2122
beforeEach(() => {
@@ -28,6 +29,8 @@ describe('isomorphic act()', () => {
2829
use = React.use;
2930
Suspense = React.Suspense;
3031
startTransition = React.startTransition;
32+
33+
waitForMicrotasks = require('internal-test-utils').waitForMicrotasks;
3134
});
3235

3336
beforeEach(() => {
@@ -51,7 +54,7 @@ describe('isomorphic act()', () => {
5154
// Nothing has rendered yet
5255
expect(root).toMatchRenderedOutput(null);
5356
// Flush the microtasks by awaiting
54-
await null;
57+
await waitForMicrotasks();
5558
expect(root).toMatchRenderedOutput('A');
5659

5760
// Now do the same thing but wrap the update with `act`. No
@@ -229,9 +232,7 @@ describe('isomorphic act()', () => {
229232
//
230233
// The exact number of microtasks is an implementation detail; just needs
231234
// to happen when the microtask queue is flushed.
232-
await null;
233-
await null;
234-
await null;
235+
await waitForMicrotasks();
235236

236237
expect(console.error).toHaveBeenCalledTimes(1);
237238
expect(console.error.mock.calls[0][0]).toContain(
@@ -282,9 +283,7 @@ describe('isomorphic act()', () => {
282283
//
283284
// The exact number of microtasks is an implementation detail; just needs
284285
// to happen when the microtask queue is flushed.
285-
await null;
286-
await null;
287-
await null;
286+
await waitForMicrotasks();
288287

289288
expect(console.error).toHaveBeenCalledTimes(0);
290289

Diff for: packages/react-reconciler/src/__tests__/ReactSchedulerIntegration-test.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ describe('ReactSchedulerIntegration', () => {
102102
const root = ReactNoop.createRoot();
103103
root.render('Initial');
104104
await waitForAll([]);
105+
expect(root).toMatchRenderedOutput('Initial');
105106

106107
scheduleCallback(NormalPriority, () => Scheduler.log('A'));
107108
scheduleCallback(NormalPriority, () => Scheduler.log('B'));
@@ -112,16 +113,22 @@ describe('ReactSchedulerIntegration', () => {
112113
root.render('Update');
113114
});
114115

115-
// Advance time just to be sure the next tasks have lower priority
116-
Scheduler.unstable_advanceTime(2000);
116+
// Perform just a little bit of work. By now, the React task will have
117+
// already been scheduled, behind A, B, and C.
118+
await waitFor(['A']);
117119

120+
// Schedule some additional tasks. These won't fire until after the React
121+
// update has finished.
118122
scheduleCallback(NormalPriority, () => Scheduler.log('D'));
119123
scheduleCallback(NormalPriority, () => Scheduler.log('E'));
120124

121125
// Flush everything up to the next paint. Should yield after the
122126
// React commit.
123-
Scheduler.unstable_flushUntilNextPaint();
124-
assertLog(['A', 'B', 'C']);
127+
await waitForPaint(['B', 'C']);
128+
expect(root).toMatchRenderedOutput('Update');
129+
130+
// Now flush the rest of the work.
131+
await waitForAll(['D', 'E']);
125132
});
126133

127134
// @gate www
@@ -213,6 +220,7 @@ describe(
213220
waitForPaint = InternalTestUtils.waitForPaint;
214221
assertLog = InternalTestUtils.assertLog;
215222
waitFor = InternalTestUtils.waitFor;
223+
act = InternalTestUtils.act;
216224
});
217225

218226
afterEach(() => {
@@ -293,8 +301,7 @@ describe(
293301
// Start logging whenever shouldYield is called
294302
logDuringShouldYield = true;
295303
// Let's call it once to confirm the mock actually works
296-
Scheduler.unstable_shouldYield();
297-
assertLog(['shouldYield']);
304+
await waitFor(['shouldYield']);
298305

299306
// Expire the task
300307
Scheduler.unstable_advanceTime(10000);
@@ -307,11 +314,9 @@ describe(
307314
startTransition(() => {
308315
ReactNoop.render(<App />);
309316
});
310-
311317
// Because the render expired, React should finish the tree without
312318
// consulting `shouldYield` again
313-
Scheduler.unstable_flushNumberOfYields(1);
314-
assertLog(['B', 'C']);
319+
await waitFor(['B', 'C']);
315320
});
316321
});
317322
},

0 commit comments

Comments
 (0)