@@ -21,6 +21,7 @@ let useEffect;
21
21
let assertLog ;
22
22
let waitFor ;
23
23
let waitForAll ;
24
+ let unstable_waitForExpired ;
24
25
25
26
describe ( 'ReactExpiration' , ( ) => {
26
27
beforeEach ( ( ) => {
@@ -38,6 +39,7 @@ describe('ReactExpiration', () => {
38
39
assertLog = InternalTestUtils . assertLog ;
39
40
waitFor = InternalTestUtils . waitFor ;
40
41
waitForAll = InternalTestUtils . waitForAll ;
42
+ unstable_waitForExpired = InternalTestUtils . unstable_waitForExpired ;
41
43
42
44
const textCache = new Map ( ) ;
43
45
@@ -124,18 +126,17 @@ describe('ReactExpiration', () => {
124
126
expect ( ReactNoop ) . toMatchRenderedOutput ( 'Step 1' ) ;
125
127
126
128
// Nothing has expired yet because time hasn't advanced.
127
- Scheduler . unstable_flushExpired ( ) ;
129
+ await unstable_waitForExpired ( [ ] ) ;
128
130
expect ( ReactNoop ) . toMatchRenderedOutput ( 'Step 1' ) ;
129
131
130
132
// Advance time a bit, but not enough to expire the low pri update.
131
133
ReactNoop . expire ( 4500 ) ;
132
- Scheduler . unstable_flushExpired ( ) ;
134
+ await unstable_waitForExpired ( [ ] ) ;
133
135
expect ( ReactNoop ) . toMatchRenderedOutput ( 'Step 1' ) ;
134
136
135
137
// Advance by a little bit more. Now the update should expire and flush.
136
138
ReactNoop . expire ( 500 ) ;
137
- Scheduler . unstable_flushExpired ( ) ;
138
- assertLog ( [ 'Step 2' ] ) ;
139
+ await unstable_waitForExpired ( [ 'Step 2' ] ) ;
139
140
expect ( ReactNoop ) . toMatchRenderedOutput ( 'Step 2' ) ;
140
141
} ) ;
141
142
@@ -339,8 +340,7 @@ describe('ReactExpiration', () => {
339
340
340
341
Scheduler . unstable_advanceTime ( 10000 ) ;
341
342
342
- Scheduler . unstable_flushExpired ( ) ;
343
- assertLog ( [ 'D' , 'E' ] ) ;
343
+ await unstable_waitForExpired ( [ 'D' , 'E' ] ) ;
344
344
expect ( root ) . toMatchRenderedOutput ( 'ABCDE' ) ;
345
345
} ) ;
346
346
@@ -369,8 +369,7 @@ describe('ReactExpiration', () => {
369
369
370
370
Scheduler . unstable_advanceTime ( 10000 ) ;
371
371
372
- Scheduler . unstable_flushExpired ( ) ;
373
- assertLog ( [ 'D' , 'E' ] ) ;
372
+ await unstable_waitForExpired ( [ 'D' , 'E' ] ) ;
374
373
expect ( root ) . toMatchRenderedOutput ( 'ABCDE' ) ;
375
374
} ) ;
376
375
@@ -383,6 +382,7 @@ describe('ReactExpiration', () => {
383
382
const InternalTestUtils = require ( 'internal-test-utils' ) ;
384
383
waitFor = InternalTestUtils . waitFor ;
385
384
assertLog = InternalTestUtils . assertLog ;
385
+ unstable_waitForExpired = InternalTestUtils . unstable_waitForExpired ;
386
386
387
387
// Before importing the renderer, advance the current time by a number
388
388
// larger than the maximum allowed for bitwise operations.
@@ -401,19 +401,17 @@ describe('ReactExpiration', () => {
401
401
await waitFor ( [ 'Step 1' ] ) ;
402
402
403
403
// The update should not have expired yet.
404
- Scheduler . unstable_flushExpired ( ) ;
405
- assertLog ( [ ] ) ;
404
+ await unstable_waitForExpired ( [ ] ) ;
406
405
407
406
expect ( ReactNoop ) . toMatchRenderedOutput ( 'Step 1' ) ;
408
407
409
408
// Advance the time some more to expire the update.
410
409
Scheduler . unstable_advanceTime ( 10000 ) ;
411
- Scheduler . unstable_flushExpired ( ) ;
412
- assertLog ( [ 'Step 2' ] ) ;
410
+ await unstable_waitForExpired ( [ 'Step 2' ] ) ;
413
411
expect ( ReactNoop ) . toMatchRenderedOutput ( 'Step 2' ) ;
414
412
} ) ;
415
413
416
- it ( 'should measure callback timeout relative to current time, not start-up time' , ( ) => {
414
+ it ( 'should measure callback timeout relative to current time, not start-up time' , async ( ) => {
417
415
// Corresponds to a bugfix: https://github.com/facebook/react/pull/15479
418
416
// The bug wasn't caught by other tests because we use virtual times that
419
417
// default to 0, and most tests don't advance time.
@@ -424,15 +422,13 @@ describe('ReactExpiration', () => {
424
422
React . startTransition ( ( ) => {
425
423
ReactNoop . render ( 'Hi' ) ;
426
424
} ) ;
427
- Scheduler . unstable_flushExpired ( ) ;
428
- assertLog ( [ ] ) ;
425
+ await unstable_waitForExpired ( [ ] ) ;
429
426
expect ( ReactNoop ) . toMatchRenderedOutput ( null ) ;
430
427
431
428
// Advancing by ~5 seconds should be sufficient to expire the update. (I
432
429
// used a slightly larger number to allow for possible rounding.)
433
430
Scheduler . unstable_advanceTime ( 6000 ) ;
434
- Scheduler . unstable_flushExpired ( ) ;
435
- assertLog ( [ ] ) ;
431
+ await unstable_waitForExpired ( [ ] ) ;
436
432
expect ( ReactNoop ) . toMatchRenderedOutput ( 'Hi' ) ;
437
433
} ) ;
438
434
@@ -476,9 +472,9 @@ describe('ReactExpiration', () => {
476
472
// The remaining work hasn't expired, so the render phase is time sliced.
477
473
// In other words, we can flush just the first child without flushing
478
474
// the rest.
479
- Scheduler . unstable_flushNumberOfYields ( 1 ) ;
475
+ //
480
476
// Yield right after first child.
481
- assertLog ( [ 'Sync pri: 1' ] ) ;
477
+ await waitFor ( [ 'Sync pri: 1' ] ) ;
482
478
// Now do the rest.
483
479
await waitForAll ( [ 'Normal pri: 1' ] ) ;
484
480
} ) ;
@@ -502,8 +498,9 @@ describe('ReactExpiration', () => {
502
498
503
499
// The remaining work _has_ expired, so the render phase is _not_ time
504
500
// sliced. Attempting to flush just the first child also flushes the rest.
505
- Scheduler . unstable_flushNumberOfYields ( 1 ) ;
506
- assertLog ( [ 'Sync pri: 2' , 'Normal pri: 2' ] ) ;
501
+ await waitFor ( [ 'Sync pri: 2' ] , {
502
+ additionalLogsAfterAttemptingToYield : [ 'Normal pri: 2' ] ,
503
+ } ) ;
507
504
} ) ;
508
505
expect ( root ) . toMatchRenderedOutput ( 'Sync pri: 2, Normal pri: 2' ) ;
509
506
} ) ;
@@ -606,18 +603,22 @@ describe('ReactExpiration', () => {
606
603
startTransition ( ( ) => {
607
604
setB ( 1 ) ;
608
605
} ) ;
606
+ await waitFor ( [ 'B0' ] ) ;
607
+
609
608
// Expire both the transitions
610
609
Scheduler . unstable_advanceTime ( 10000 ) ;
611
610
// Both transitions have expired, but since they aren't related
612
611
// (entangled), we should be able to finish the in-progress transition
613
612
// without also including the next one.
614
- Scheduler . unstable_flushNumberOfYields ( 1 ) ;
615
- assertLog ( [ 'B0' , 'C' ] ) ;
613
+ await waitFor ( [ ] , {
614
+ additionalLogsAfterAttemptingToYield : [ 'C' ] ,
615
+ } ) ;
616
616
expect ( root ) . toMatchRenderedOutput ( 'A1B0C' ) ;
617
617
618
618
// The next transition also finishes without yielding.
619
- Scheduler . unstable_flushNumberOfYields ( 1 ) ;
620
- assertLog ( [ 'A1' , 'B1' , 'C' ] ) ;
619
+ await waitFor ( [ 'A1' ] , {
620
+ additionalLogsAfterAttemptingToYield : [ 'B1' , 'C' ] ,
621
+ } ) ;
621
622
expect ( root ) . toMatchRenderedOutput ( 'A1B1C' ) ;
622
623
} ) ;
623
624
} ) ;
@@ -662,8 +663,9 @@ describe('ReactExpiration', () => {
662
663
Scheduler . unstable_advanceTime ( 10000 ) ;
663
664
664
665
// The rest of the update finishes without yielding.
665
- Scheduler . unstable_flushNumberOfYields ( 1 ) ;
666
- assertLog ( [ 'B' , 'C' ] ) ;
666
+ await waitFor ( [ ] , {
667
+ additionalLogsAfterAttemptingToYield : [ 'B' , 'C' ] ,
668
+ } ) ;
667
669
} ) ;
668
670
} ) ;
669
671
@@ -705,8 +707,9 @@ describe('ReactExpiration', () => {
705
707
706
708
// Now flush the original update. Because it expired, it should finish
707
709
// without yielding.
708
- Scheduler . unstable_flushNumberOfYields ( 1 ) ;
709
- assertLog ( [ 'A1' , 'B1' ] ) ;
710
+ await waitFor ( [ 'A1' ] , {
711
+ additionalLogsAfterAttemptingToYield : [ 'B1' ] ,
712
+ } ) ;
710
713
} ) ;
711
714
} ) ;
712
715
@@ -731,16 +734,19 @@ describe('ReactExpiration', () => {
731
734
assertLog ( [ 'A0' , 'B0' , 'C0' , 'Effect: 0' ] ) ;
732
735
expect ( root ) . toMatchRenderedOutput ( 'A0B0C0' ) ;
733
736
734
- await act ( ( ) => {
737
+ await act ( async ( ) => {
735
738
startTransition ( ( ) => {
736
739
root . render ( < App step = { 1 } /> ) ;
737
740
} ) ;
741
+ await waitFor ( [ 'A1' ] ) ;
742
+
738
743
// Expire the update
739
744
Scheduler . unstable_advanceTime ( 10000 ) ;
740
745
741
746
// The update finishes without yielding. But it does not flush the effect.
742
- Scheduler . unstable_flushNumberOfYields ( 1 ) ;
743
- assertLog ( [ 'A1' , 'B1' , 'C1' ] ) ;
747
+ await waitFor ( [ 'B1' ] , {
748
+ additionalLogsAfterAttemptingToYield : [ 'C1' ] ,
749
+ } ) ;
744
750
} ) ;
745
751
// The effect flushes after paint.
746
752
assertLog ( [ 'Effect: 1' ] ) ;
0 commit comments