Skip to content

Commit e64a8f4

Browse files
authored
Codemod tests to waitFor pattern (3/?) (#26299)
This converts some of our test suite to use the `waitFor` test pattern, instead of the `expect(Scheduler).toFlushAndYield` pattern. Most of these changes are automated with jscodeshift, with some slight manual cleanup in certain cases. See #26285 for full context.
1 parent ce8a72f commit e64a8f4

15 files changed

+406
-400
lines changed

packages/react-dom/src/__tests__/ReactDOMImageLoad-test.internal.js

Lines changed: 41 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ let images = [];
2424
let onLoadSpy = null;
2525
let actualLoadSpy = null;
2626

27+
let waitForAll;
28+
let waitFor;
29+
let assertLog;
30+
2731
function PhaseMarkers({children}) {
2832
Scheduler.unstable_yieldValue('render start');
2933
React.useLayoutEffect(() => {
@@ -94,6 +98,11 @@ describe('ReactDOMImageLoad', () => {
9498
ReactDOMClient = require('react-dom/client');
9599
// Suspense = React.Suspense;
96100

101+
const InternalTestUtils = require('internal-test-utils');
102+
waitForAll = InternalTestUtils.waitForAll;
103+
waitFor = InternalTestUtils.waitFor;
104+
assertLog = InternalTestUtils.assertLog;
105+
97106
onLoadSpy = jest.fn(reactEvent => {
98107
const src = reactEvent.target.getAttribute('src');
99108
Scheduler.unstable_yieldValue('onLoadSpy [' + src + ']');
@@ -206,26 +215,17 @@ describe('ReactDOMImageLoad', () => {
206215
),
207216
);
208217

209-
expect(Scheduler).toFlushAndYieldThrough([
210-
'render start',
211-
'Img default',
212-
'Yield',
213-
]);
218+
await waitFor(['render start', 'Img default', 'Yield']);
214219
const img = last(images);
215220
loadImage(img);
216-
expect(Scheduler).toHaveYielded([
221+
assertLog([
217222
'actualLoadSpy [default]',
218223
// no onLoadSpy since we have not completed render
219224
]);
220-
expect(Scheduler).toFlushAndYield([
221-
'a',
222-
'load triggered',
223-
'last layout',
224-
'last passive',
225-
]);
225+
await waitForAll(['a', 'load triggered', 'last layout', 'last passive']);
226226
expect(img.__needsDispatch).toBe(true);
227227
loadImage(img);
228-
expect(Scheduler).toHaveYielded([
228+
assertLog([
229229
'actualLoadSpy [default]', // the browser reloading of the image causes this to yield again
230230
'onLoadSpy [default]',
231231
]);
@@ -244,7 +244,7 @@ describe('ReactDOMImageLoad', () => {
244244
),
245245
);
246246

247-
expect(Scheduler).toFlushAndYieldThrough([
247+
await waitFor([
248248
'render start',
249249
'Img default',
250250
'load triggered',
@@ -253,11 +253,8 @@ describe('ReactDOMImageLoad', () => {
253253
Scheduler.unstable_requestPaint();
254254
const img = last(images);
255255
loadImage(img);
256-
expect(Scheduler).toHaveYielded([
257-
'actualLoadSpy [default]',
258-
'onLoadSpy [default]',
259-
]);
260-
expect(Scheduler).toFlushAndYield(['last passive']);
256+
assertLog(['actualLoadSpy [default]', 'onLoadSpy [default]']);
257+
await waitForAll(['last passive']);
261258
expect(img.__needsDispatch).toBe(false);
262259
expect(onLoadSpy).toHaveBeenCalledTimes(1);
263260
});
@@ -286,16 +283,12 @@ describe('ReactDOMImageLoad', () => {
286283

287284
React.startTransition(() => root.render(<Base />));
288285

289-
expect(Scheduler).toFlushAndYieldThrough([
290-
'render start',
291-
'Img a',
292-
'Yield',
293-
]);
286+
await waitFor(['render start', 'Img a', 'Yield']);
294287
const img = last(images);
295288
loadImage(img);
296-
expect(Scheduler).toHaveYielded(['actualLoadSpy [a]']);
289+
assertLog(['actualLoadSpy [a]']);
297290

298-
expect(Scheduler).toFlushAndYieldThrough([
291+
await waitFor([
299292
'load triggered',
300293
'last layout',
301294
// the update in layout causes a passive effects flush before a sync render
@@ -309,7 +302,7 @@ describe('ReactDOMImageLoad', () => {
309302
]);
310303
expect(images.length).toBe(1);
311304
loadImage(img);
312-
expect(Scheduler).toHaveYielded(['actualLoadSpy [b]', 'onLoadSpy [b]']);
305+
assertLog(['actualLoadSpy [b]', 'onLoadSpy [b]']);
313306
expect(onLoadSpy).toHaveBeenCalledTimes(1);
314307
});
315308

@@ -323,7 +316,7 @@ describe('ReactDOMImageLoad', () => {
323316
</PhaseMarkers>,
324317
);
325318

326-
expect(Scheduler).toFlushAndYield([
319+
await waitForAll([
327320
'render start',
328321
'Img default',
329322
'load triggered',
@@ -332,10 +325,7 @@ describe('ReactDOMImageLoad', () => {
332325
]);
333326
const img = last(images);
334327
loadImage(img);
335-
expect(Scheduler).toHaveYielded([
336-
'actualLoadSpy [default]',
337-
'onLoadSpy [default]',
338-
]);
328+
assertLog(['actualLoadSpy [default]', 'onLoadSpy [default]']);
339329
expect(onLoadSpy).toHaveBeenCalledTimes(1);
340330
});
341331

@@ -365,26 +355,17 @@ describe('ReactDOMImageLoad', () => {
365355
),
366356
);
367357

368-
expect(Scheduler).toFlushAndYieldThrough([
369-
'render start',
370-
'Img default',
371-
'Yield',
372-
]);
358+
await waitFor(['render start', 'Img default', 'Yield']);
373359
const img = last(images);
374360
loadImage(img);
375-
expect(Scheduler).toHaveYielded(['actualLoadSpy [default]']);
376-
expect(Scheduler).toFlushAndYield([
377-
'a',
378-
'load triggered',
379-
'last layout',
380-
'last passive',
381-
]);
361+
assertLog(['actualLoadSpy [default]']);
362+
await waitForAll(['a', 'load triggered', 'last layout', 'last passive']);
382363
expect(img.__needsDispatch).toBe(true);
383364
loadImage(img);
384365
// we expect the browser to load the image again but since we are no longer rendering
385366
// the img there will be no onLoad called
386-
expect(Scheduler).toHaveYielded(['actualLoadSpy [default]']);
387-
expect(Scheduler).toFlushWithoutYielding();
367+
assertLog(['actualLoadSpy [default]']);
368+
await waitForAll([]);
388369
expect(onLoadSpy).not.toHaveBeenCalled();
389370
});
390371

@@ -426,16 +407,16 @@ describe('ReactDOMImageLoad', () => {
426407
),
427408
);
428409

429-
expect(Scheduler).toFlushAndYieldThrough([
410+
await waitFor([
430411
// initial render
431412
'render start',
432413
'Img default',
433414
'Yield',
434415
]);
435416
const img = last(images);
436417
loadImage(img);
437-
expect(Scheduler).toHaveYielded(['actualLoadSpy [default]']);
438-
expect(Scheduler).toFlushAndYield([
418+
assertLog(['actualLoadSpy [default]']);
419+
await waitForAll([
439420
'a',
440421
'load triggered',
441422
// img is present at first
@@ -450,8 +431,8 @@ describe('ReactDOMImageLoad', () => {
450431
loadImage(img);
451432
// we expect the browser to load the image again but since we are no longer rendering
452433
// the img there will be no onLoad called
453-
expect(Scheduler).toHaveYielded(['actualLoadSpy [default]']);
454-
expect(Scheduler).toFlushWithoutYielding();
434+
assertLog(['actualLoadSpy [default]']);
435+
await waitForAll([]);
455436
expect(onLoadSpy).not.toHaveBeenCalled();
456437
});
457438

@@ -548,22 +529,18 @@ describe('ReactDOMImageLoad', () => {
548529

549530
root.render(<Base />);
550531

551-
expect(Scheduler).toFlushWithoutYielding();
532+
await waitForAll([]);
552533

553534
React.startTransition(() => externalSetSrc('a'));
554535

555-
expect(Scheduler).toFlushAndYieldThrough([
556-
'YieldingWithImage',
557-
'Img a',
558-
'Yield',
559-
]);
536+
await waitFor(['YieldingWithImage', 'Img a', 'Yield']);
560537
let img = last(images);
561538
loadImage(img);
562-
expect(Scheduler).toHaveYielded(['actualLoadSpy [a]']);
539+
assertLog(['actualLoadSpy [a]']);
563540

564541
ReactDOM.flushSync(() => externalSetSrcAlt('b'));
565542

566-
expect(Scheduler).toHaveYielded([
543+
assertLog([
567544
'YieldingWithImage',
568545
'Img b',
569546
'Yield',
@@ -576,18 +553,12 @@ describe('ReactDOMImageLoad', () => {
576553
expect(img.__needsDispatch).toBe(true);
577554
loadImage(img);
578555

579-
expect(Scheduler).toHaveYielded(['actualLoadSpy [b]', 'onLoadSpy [b]']);
556+
assertLog(['actualLoadSpy [b]', 'onLoadSpy [b]']);
580557
// why is there another update here?
581-
expect(Scheduler).toFlushAndYield([
582-
'YieldingWithImage',
583-
'Img b',
584-
'Yield',
585-
'b',
586-
'Committed',
587-
]);
558+
await waitForAll(['YieldingWithImage', 'Img b', 'Yield', 'b', 'Committed']);
588559
});
589560

590-
it('preserves the src property / attribute when triggering a potential new load event', () => {
561+
it('preserves the src property / attribute when triggering a potential new load event', async () => {
591562
// this test covers a regression identified in https://github.com/mui/material-ui/pull/31263
592563
// where the resetting of the src property caused the property to change from relative to fully qualified
593564

@@ -612,17 +583,13 @@ describe('ReactDOMImageLoad', () => {
612583
);
613584

614585
// render to yield to capture state of img src attribute and property before commit
615-
expect(Scheduler).toFlushAndYieldThrough([
616-
'render start',
617-
'Img default',
618-
'Yield',
619-
]);
586+
await waitFor(['render start', 'Img default', 'Yield']);
620587
const img = last(images);
621588
const renderSrcProperty = img.src;
622589
const renderSrcAttr = img.getAttribute('src');
623590

624591
// finish render and commit causing the src property to be rewritten
625-
expect(Scheduler).toFlushAndYield(['a', 'last layout', 'last passive']);
592+
await waitForAll(['a', 'last layout', 'last passive']);
626593
const commitSrcProperty = img.src;
627594
const commitSrcAttr = img.getAttribute('src');
628595

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ let ReactDOM;
1515
let ReactDOMClient;
1616
let Scheduler;
1717
let act;
18+
let assertLog;
19+
let waitFor;
1820

1921
describe('ReactDOMNativeEventHeuristic-test', () => {
2022
let container;
@@ -28,6 +30,10 @@ describe('ReactDOMNativeEventHeuristic-test', () => {
2830
Scheduler = require('scheduler');
2931
act = require('jest-react').act;
3032

33+
const InternalTestUtils = require('internal-test-utils');
34+
assertLog = InternalTestUtils.assertLog;
35+
waitFor = InternalTestUtils.waitFor;
36+
3137
document.body.appendChild(container);
3238
});
3339

@@ -301,10 +307,10 @@ describe('ReactDOMNativeEventHeuristic-test', () => {
301307
dispatchAndSetCurrentEvent(target.current, mouseEnterEvent);
302308

303309
// Since mouse end is not discrete, should not have updated yet
304-
expect(Scheduler).toHaveYielded(['not hovered']);
310+
assertLog(['not hovered']);
305311
expect(container.textContent).toEqual('not hovered');
306312

307-
expect(Scheduler).toFlushAndYieldThrough(['hovered']);
313+
await waitFor(['hovered']);
308314
expect(container.textContent).toEqual('hovered');
309315
});
310316
expect(container.textContent).toEqual('hovered');
@@ -381,7 +387,7 @@ describe('ReactDOMNativeEventHeuristic-test', () => {
381387
pressEvent.initEvent('click', true, true);
382388
dispatchAndSetCurrentEvent(target, pressEvent);
383389

384-
expect(Scheduler).toHaveYielded(['Count: 0 [after batchedUpdates]']);
390+
assertLog(['Count: 0 [after batchedUpdates]']);
385391
expect(container.textContent).toEqual('Count: 0');
386392

387393
// Intentionally not using `act` so we can observe in between the click

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('ReactDOMNestedEvents', () => {
1515
let Scheduler;
1616
let act;
1717
let useState;
18+
let assertLog;
1819

1920
beforeEach(() => {
2021
jest.resetModules();
@@ -23,6 +24,9 @@ describe('ReactDOMNestedEvents', () => {
2324
Scheduler = require('scheduler');
2425
act = require('jest-react').act;
2526
useState = React.useState;
27+
28+
const InternalTestUtils = require('internal-test-utils');
29+
assertLog = InternalTestUtils.assertLog;
2630
});
2731

2832
test('nested event dispatches should not cause updates to flush', async () => {
@@ -67,9 +71,7 @@ describe('ReactDOMNestedEvents', () => {
6771
await act(async () => {
6872
buttonRef.current.click();
6973
});
70-
expect(Scheduler).toHaveYielded([
71-
'Value right after focus call: Clicked: false, Focused: false',
72-
]);
74+
assertLog(['Value right after focus call: Clicked: false, Focused: false']);
7375
expect(buttonRef.current.innerHTML).toEqual('Clicked: true, Focused: true');
7476
});
7577
});

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ let ReactDOMServer = require('react-dom/server');
1616
let Scheduler = require('scheduler');
1717
let act;
1818
let useEffect;
19+
let assertLog;
20+
let waitFor;
1921

2022
describe('ReactDOMRoot', () => {
2123
let container;
@@ -30,6 +32,10 @@ describe('ReactDOMRoot', () => {
3032
Scheduler = require('scheduler');
3133
act = require('jest-react').act;
3234
useEffect = React.useEffect;
35+
36+
const InternalTestUtils = require('internal-test-utils');
37+
assertLog = InternalTestUtils.assertLog;
38+
waitFor = InternalTestUtils.waitFor;
3339
});
3440

3541
it('renders children', () => {
@@ -255,7 +261,7 @@ describe('ReactDOMRoot', () => {
255261
Scheduler.unstable_yieldValue('callback');
256262
});
257263
expect(container.textContent).toEqual('Hi');
258-
expect(Scheduler).toHaveYielded(['callback']);
264+
assertLog(['callback']);
259265
});
260266

261267
it('warns when unmounting with legacy API (no previous content)', () => {
@@ -401,10 +407,10 @@ describe('ReactDOMRoot', () => {
401407
await act(async () => {
402408
root.render(<Foo value="b" />);
403409

404-
expect(Scheduler).toHaveYielded(['a']);
410+
assertLog(['a']);
405411
expect(container.textContent).toEqual('a');
406412

407-
expect(Scheduler).toFlushAndYieldThrough(['b']);
413+
await waitFor(['b']);
408414
if (gate(flags => flags.allowConcurrentByDefault)) {
409415
expect(container.textContent).toEqual('a');
410416
} else {

0 commit comments

Comments
 (0)