/
ReactTransition-test.js
962 lines (862 loc) · 27.6 KB
/
ReactTransition-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
* @jest-environment node
*/
'use strict';
let React;
let ReactNoop;
let Scheduler;
let Suspense;
let useState;
let useLayoutEffect;
let useTransition;
let startTransition;
let act;
let getCacheForType;
let caches;
let seededCache;
describe('ReactTransition', () => {
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactNoop = require('react-noop-renderer');
Scheduler = require('scheduler');
useState = React.useState;
useLayoutEffect = React.useLayoutEffect;
useTransition = React.unstable_useTransition;
Suspense = React.Suspense;
startTransition = React.unstable_startTransition;
getCacheForType = React.unstable_getCacheForType;
act = ReactNoop.act;
caches = [];
seededCache = null;
});
function createTextCache() {
if (seededCache !== null) {
// Trick to seed a cache before it exists.
// TODO: Need a built-in API to seed data before the initial render (i.e.
// not a refresh because nothing has mounted yet).
const cache = seededCache;
seededCache = null;
return cache;
}
const data = new Map();
const version = caches.length + 1;
const cache = {
version,
data,
resolve(text) {
const record = data.get(text);
if (record === undefined) {
const newRecord = {
status: 'resolved',
value: text,
};
data.set(text, newRecord);
} else if (record.status === 'pending') {
const thenable = record.value;
record.status = 'resolved';
record.value = text;
thenable.pings.forEach(t => t());
}
},
reject(text, error) {
const record = data.get(text);
if (record === undefined) {
const newRecord = {
status: 'rejected',
value: error,
};
data.set(text, newRecord);
} else if (record.status === 'pending') {
const thenable = record.value;
record.status = 'rejected';
record.value = error;
thenable.pings.forEach(t => t());
}
},
};
caches.push(cache);
return cache;
}
function readText(text) {
const textCache = getCacheForType(createTextCache);
const record = textCache.data.get(text);
if (record !== undefined) {
switch (record.status) {
case 'pending':
Scheduler.unstable_yieldValue(`Suspend! [${text}]`);
throw record.value;
case 'rejected':
Scheduler.unstable_yieldValue(`Error! [${text}]`);
throw record.value;
case 'resolved':
return textCache.version;
}
} else {
Scheduler.unstable_yieldValue(`Suspend! [${text}]`);
const thenable = {
pings: [],
then(resolve) {
if (newRecord.status === 'pending') {
thenable.pings.push(resolve);
} else {
Promise.resolve().then(() => resolve(newRecord.value));
}
},
};
const newRecord = {
status: 'pending',
value: thenable,
};
textCache.data.set(text, newRecord);
throw thenable;
}
}
function Text({text}) {
Scheduler.unstable_yieldValue(text);
return text;
}
function AsyncText({text}) {
readText(text);
Scheduler.unstable_yieldValue(text);
return text;
}
function seedNextTextCache(text) {
if (seededCache === null) {
seededCache = createTextCache();
}
seededCache.resolve(text);
}
function resolveText(text) {
if (caches.length === 0) {
throw Error('Cache does not exist.');
} else {
// Resolve the most recently created cache. An older cache can by
// resolved with `caches[index].resolve(text)`.
caches[caches.length - 1].resolve(text);
}
}
// @gate experimental
// @gate enableCache
test('isPending works even if called from outside an input event', async () => {
let start;
function App() {
const [show, setShow] = useState(false);
const [isPending, _start] = useTransition();
start = () => _start(() => setShow(true));
return (
<Suspense fallback={<Text text="Loading..." />}>
{isPending ? <Text text="Pending..." /> : null}
{show ? <AsyncText text="Async" /> : <Text text="(empty)" />}
</Suspense>
);
}
const root = ReactNoop.createRoot();
await act(async () => {
root.render(<App />);
});
expect(Scheduler).toHaveYielded(['(empty)']);
expect(root).toMatchRenderedOutput('(empty)');
await act(async () => {
start();
expect(Scheduler).toFlushAndYield([
'Pending...',
'(empty)',
'Suspend! [Async]',
'Loading...',
]);
expect(root).toMatchRenderedOutput('Pending...(empty)');
await resolveText('Async');
});
expect(Scheduler).toHaveYielded(['Async']);
expect(root).toMatchRenderedOutput('Async');
});
// @gate experimental
// @gate enableCache
test(
'when multiple transitions update the same queue, only the most recent ' +
'one is allowed to finish (no intermediate states)',
async () => {
let update;
function App() {
const [isContentPending, startContentChange] = useTransition();
const [label, setLabel] = useState('A');
const [contents, setContents] = useState('A');
update = value => {
ReactNoop.discreteUpdates(() => {
setLabel(value);
startContentChange(() => {
setContents(value);
});
});
};
return (
<>
<Text
text={
label + ' label' + (isContentPending ? ' (loading...)' : '')
}
/>
<div>
<Suspense fallback={<Text text="Loading..." />}>
<AsyncText text={contents + ' content'} />
</Suspense>
</div>
</>
);
}
// Initial render
const root = ReactNoop.createRoot();
await act(async () => {
seedNextTextCache('A content');
root.render(<App />);
});
expect(Scheduler).toHaveYielded(['A label', 'A content']);
expect(root).toMatchRenderedOutput(
<>
A label<div>A content</div>
</>,
);
// Switch to B
await act(async () => {
update('B');
});
expect(Scheduler).toHaveYielded([
// Commit pending state
'B label (loading...)',
'A content',
// Attempt to render B, but it suspends
'B label',
'Suspend! [B content]',
'Loading...',
]);
// This is a refresh transition so it shouldn't show a fallback
expect(root).toMatchRenderedOutput(
<>
B label (loading...)<div>A content</div>
</>,
);
// Before B finishes loading, switch to C
await act(async () => {
update('C');
});
expect(Scheduler).toHaveYielded([
// Commit pending state
'C label (loading...)',
'A content',
// Attempt to render C, but it suspends
'C label',
'Suspend! [C content]',
'Loading...',
]);
expect(root).toMatchRenderedOutput(
<>
C label (loading...)<div>A content</div>
</>,
);
// Finish loading B. But we're not allowed to render B because it's
// entangled with C. So we're still pending.
await act(async () => {
resolveText('B content');
});
expect(Scheduler).toHaveYielded([
// Attempt to render C, but it suspends
'C label',
'Suspend! [C content]',
'Loading...',
]);
expect(root).toMatchRenderedOutput(
<>
C label (loading...)<div>A content</div>
</>,
);
// Now finish loading C. This is the terminal update, so it can finish.
await act(async () => {
resolveText('C content');
});
expect(Scheduler).toHaveYielded(['C label', 'C content']);
expect(root).toMatchRenderedOutput(
<>
C label<div>C content</div>
</>,
);
},
);
// Same as previous test, but for class update queue.
// @gate experimental
// @gate enableCache
test(
'when multiple transitions update the same queue, only the most recent ' +
'one is allowed to finish (no intermediate states) (classes)',
async () => {
let update;
class App extends React.Component {
state = {
label: 'A',
contents: 'A',
};
render() {
update = value => {
ReactNoop.discreteUpdates(() => {
this.setState({label: value});
startTransition(() => {
this.setState({contents: value});
});
});
};
const label = this.state.label;
const contents = this.state.contents;
const isContentPending = label !== contents;
return (
<>
<Text
text={
label + ' label' + (isContentPending ? ' (loading...)' : '')
}
/>
<div>
<Suspense fallback={<Text text="Loading..." />}>
<AsyncText text={contents + ' content'} />
</Suspense>
</div>
</>
);
}
}
// Initial render
const root = ReactNoop.createRoot();
await act(async () => {
seedNextTextCache('A content');
root.render(<App />);
});
expect(Scheduler).toHaveYielded(['A label', 'A content']);
expect(root).toMatchRenderedOutput(
<>
A label<div>A content</div>
</>,
);
// Switch to B
await act(async () => {
update('B');
});
expect(Scheduler).toHaveYielded([
// Commit pending state
'B label (loading...)',
'A content',
// Attempt to render B, but it suspends
'B label',
'Suspend! [B content]',
'Loading...',
]);
// This is a refresh transition so it shouldn't show a fallback
expect(root).toMatchRenderedOutput(
<>
B label (loading...)<div>A content</div>
</>,
);
// Before B finishes loading, switch to C
await act(async () => {
update('C');
});
expect(Scheduler).toHaveYielded([
// Commit pending state
'C label (loading...)',
'A content',
// Attempt to render C, but it suspends
'C label',
'Suspend! [C content]',
'Loading...',
]);
expect(root).toMatchRenderedOutput(
<>
C label (loading...)<div>A content</div>
</>,
);
// Finish loading B. But we're not allowed to render B because it's
// entangled with C. So we're still pending.
await act(async () => {
resolveText('B content');
});
expect(Scheduler).toHaveYielded([
// Attempt to render C, but it suspends
'C label',
'Suspend! [C content]',
'Loading...',
]);
expect(root).toMatchRenderedOutput(
<>
C label (loading...)<div>A content</div>
</>,
);
// Now finish loading C. This is the terminal update, so it can finish.
await act(async () => {
resolveText('C content');
});
expect(Scheduler).toHaveYielded(['C label', 'C content']);
expect(root).toMatchRenderedOutput(
<>
C label<div>C content</div>
</>,
);
},
);
// @gate experimental
// @gate enableCache
test(
'when multiple transitions update overlapping queues, all the transitions ' +
'across all the queues are entangled',
async () => {
let setShowA;
let setShowB;
let setShowC;
function App() {
const [showA, _setShowA] = useState(false);
const [showB, _setShowB] = useState(false);
const [showC, _setShowC] = useState(false);
setShowA = _setShowA;
setShowB = _setShowB;
setShowC = _setShowC;
// Only one of these children should be visible at a time. Except
// instead of being modeled as a single state, it's three separate
// states that are updated simultaneously. This may seem a bit
// contrived, but it's more common than you might think. Usually via
// a framework or indirection. For example, consider a tooltip manager
// that only shows a single tooltip at a time. Or a router that
// highlights links to the active route.
return (
<>
<Suspense fallback={<Text text="Loading..." />}>
{showA ? <AsyncText text="A" /> : null}
{showB ? <AsyncText text="B" /> : null}
{showC ? <AsyncText text="C" /> : null}
</Suspense>
</>
);
}
// Initial render. Start with all children hidden.
const root = ReactNoop.createRoot();
await act(async () => {
root.render(<App />);
});
expect(Scheduler).toHaveYielded([]);
expect(root).toMatchRenderedOutput(null);
// Switch to A.
await act(async () => {
startTransition(() => {
setShowA(true);
});
});
expect(Scheduler).toHaveYielded(['Suspend! [A]', 'Loading...']);
expect(root).toMatchRenderedOutput(null);
// Before A loads, switch to B. This should entangle A with B.
await act(async () => {
startTransition(() => {
setShowA(false);
setShowB(true);
});
});
expect(Scheduler).toHaveYielded(['Suspend! [B]', 'Loading...']);
expect(root).toMatchRenderedOutput(null);
// Before A or B loads, switch to C. This should entangle C with B, and
// transitively entangle C with A.
await act(async () => {
startTransition(() => {
setShowB(false);
setShowC(true);
});
});
expect(Scheduler).toHaveYielded(['Suspend! [C]', 'Loading...']);
expect(root).toMatchRenderedOutput(null);
// Now the data starts resolving out of order.
// First resolve B. This will attempt to render C, since everything is
// entangled.
await act(async () => {
startTransition(() => {
resolveText('B');
});
});
expect(Scheduler).toHaveYielded(['Suspend! [C]', 'Loading...']);
expect(root).toMatchRenderedOutput(null);
// Now resolve A. Again, this will attempt to render C, since everything
// is entangled.
await act(async () => {
startTransition(() => {
resolveText('A');
});
});
expect(Scheduler).toHaveYielded(['Suspend! [C]', 'Loading...']);
expect(root).toMatchRenderedOutput(null);
// Finally, resolve C. This time we can finish.
await act(async () => {
startTransition(() => {
resolveText('C');
});
});
expect(Scheduler).toHaveYielded(['C']);
expect(root).toMatchRenderedOutput('C');
},
);
// @gate experimental
// @gate enableCache
test('interrupt a refresh transition if a new transition is scheduled', async () => {
const root = ReactNoop.createRoot();
await ReactNoop.act(async () => {
root.render(
<>
<Suspense fallback={<Text text="Loading..." />} />
<Text text="Initial" />
</>,
);
});
expect(Scheduler).toHaveYielded(['Initial']);
expect(root).toMatchRenderedOutput('Initial');
await ReactNoop.act(async () => {
// Start a refresh transition
startTransition(() => {
root.render(
<>
<Suspense fallback={<Text text="Loading..." />}>
<AsyncText text="Async" />
</Suspense>
<Text text="After Suspense" />
<Text text="Sibling" />
</>,
);
});
// Partially render it.
expect(Scheduler).toFlushAndYieldThrough([
// Once we the update suspends, we know it's a refresh transition,
// because the Suspense boundary has already mounted.
'Suspend! [Async]',
'Loading...',
'After Suspense',
]);
// Schedule a new transition
startTransition(async () => {
root.render(
<>
<Suspense fallback={<Text text="Loading..." />} />
<Text text="Updated" />
</>,
);
});
});
// Because the first one is going to suspend regardless, we should
// immediately switch to rendering the new transition.
expect(Scheduler).toHaveYielded(['Updated']);
expect(root).toMatchRenderedOutput('Updated');
});
// @gate experimental
// @gate enableCache
test(
"interrupt a refresh transition when something suspends and we've " +
'already bailed out on another transition in a parent',
async () => {
let setShouldSuspend;
function Parent({children}) {
const [shouldHideInParent, _setShouldHideInParent] = useState(false);
setShouldHideInParent = _setShouldHideInParent;
Scheduler.unstable_yieldValue(
'shouldHideInParent: ' + shouldHideInParent,
);
if (shouldHideInParent) {
return <Text text="(empty)" />;
}
return children;
}
let setShouldHideInParent;
function App() {
const [shouldSuspend, _setShouldSuspend] = useState(false);
setShouldSuspend = _setShouldSuspend;
return (
<>
<Text text="A" />
<Parent>
<Suspense fallback={<Text text="Loading..." />}>
{shouldSuspend ? <AsyncText text="Async" /> : null}
</Suspense>
</Parent>
<Text text="B" />
<Text text="C" />
</>
);
}
const root = ReactNoop.createRoot();
await act(async () => {
root.render(<App />);
expect(Scheduler).toFlushAndYield([
'A',
'shouldHideInParent: false',
'B',
'C',
]);
expect(root).toMatchRenderedOutput('ABC');
// Schedule an update
startTransition(() => {
setShouldSuspend(true);
});
// Now we need to trigger schedule another transition in a different
// lane from the first one. At the time this was written, all transitions are worked on
// simultaneously, unless a transition was already in progress when a
// new one was scheduled. So, partially render the first transition.
expect(Scheduler).toFlushAndYieldThrough(['A']);
// Now schedule a second transition. We won't interrupt the first one.
React.unstable_startTransition(() => {
setShouldHideInParent(true);
});
// Continue rendering the first transition.
expect(Scheduler).toFlushAndYieldThrough([
'shouldHideInParent: false',
'Suspend! [Async]',
'Loading...',
'B',
]);
// Should not have committed loading state
expect(root).toMatchRenderedOutput('ABC');
// At this point, we've processed the parent update queue, so we know
// that it has a pending update from the second transition, even though
// we skipped it during this render. And we know this is a refresh
// transition, because we had to render a loading state. So the next
// time we re-enter the work loop (we don't interrupt immediately, we
// just wait for the next time slice), we should throw out the
// suspended first transition and try the second one.
expect(Scheduler).toFlushUntilNextPaint([
'shouldHideInParent: true',
'(empty)',
]);
expect(root).toMatchRenderedOutput('A(empty)BC');
// Since the two transitions are not entangled, we then later go back
// and finish retry the first transition. Not really relevant to this
// test but I'll assert the result anyway.
expect(Scheduler).toFlushAndYield([
'A',
'shouldHideInParent: true',
'(empty)',
'B',
'C',
]);
expect(root).toMatchRenderedOutput('A(empty)BC');
});
},
);
// @gate experimental
// @gate enableCache
test(
'interrupt a refresh transition when something suspends and a parent ' +
'component received an interleaved update after its queue was processed',
async () => {
// Title is confusing so I'll try to explain further: This is similar to
// the previous test, except instead of skipped over a transition update
// in a parent, the parent receives an interleaved update *after* its
// begin phase has already finished.
function App({shouldSuspend, step}) {
return (
<>
<Text text={`A${step}`} />
<Suspense fallback={<Text text="Loading..." />}>
{shouldSuspend ? <AsyncText text="Async" ms={2000} /> : null}
</Suspense>
<Text text={`B${step}`} />
<Text text={`C${step}`} />
</>
);
}
const root = ReactNoop.createRoot();
await ReactNoop.act(async () => {
root.render(<App shouldSuspend={false} step={0} />);
});
expect(Scheduler).toHaveYielded(['A0', 'B0', 'C0']);
expect(root).toMatchRenderedOutput('A0B0C0');
await ReactNoop.act(async () => {
// This update will suspend.
startTransition(() => {
root.render(<App shouldSuspend={true} step={1} />);
});
// Flush past the root, but stop before the async component.
expect(Scheduler).toFlushAndYieldThrough(['A1']);
// Schedule another transition on the root, which already completed.
startTransition(() => {
root.render(<App shouldSuspend={false} step={2} />);
});
// We'll keep working on the first update.
expect(Scheduler).toFlushAndYieldThrough([
// Now the async component suspends
'Suspend! [Async]',
'Loading...',
'B1',
]);
// Should not have committed loading state
expect(root).toMatchRenderedOutput('A0B0C0');
// After suspending, should abort the first update and switch to the
// second update. So, C1 should not appear in the log.
// TODO: This should work even if React does not yield to the main
// thread. Should use same mechanism as selective hydration to interrupt
// the render before the end of the current slice of work.
expect(Scheduler).toFlushAndYield(['A2', 'B2', 'C2']);
expect(root).toMatchRenderedOutput('A2B2C2');
});
},
);
// @gate experimental
// @gate enableCache
it('should render normal pri updates scheduled after transitions before transitions', async () => {
let updateTransitionPri;
let updateNormalPri;
function App() {
const [normalPri, setNormalPri] = useState(0);
const [transitionPri, setTransitionPri] = useState(0);
updateTransitionPri = () =>
startTransition(() => setTransitionPri(n => n + 1));
updateNormalPri = () => setNormalPri(n => n + 1);
useLayoutEffect(() => {
Scheduler.unstable_yieldValue('Commit');
});
return (
<Suspense fallback={<Text text="Loading..." />}>
<Text text={'Transition pri: ' + transitionPri} />
{', '}
<Text text={'Normal pri: ' + normalPri} />
</Suspense>
);
}
const root = ReactNoop.createRoot();
await act(async () => {
root.render(<App />);
});
// Initial render.
expect(Scheduler).toHaveYielded([
'Transition pri: 0',
'Normal pri: 0',
'Commit',
]);
expect(root).toMatchRenderedOutput('Transition pri: 0, Normal pri: 0');
await act(async () => {
updateTransitionPri();
updateNormalPri();
});
expect(Scheduler).toHaveYielded([
// Normal update first.
'Transition pri: 0',
'Normal pri: 1',
'Commit',
// Then transition update.
'Transition pri: 1',
'Normal pri: 1',
'Commit',
]);
expect(root).toMatchRenderedOutput('Transition pri: 1, Normal pri: 1');
});
// @gate experimental
// @gate enableCache
it('should render normal pri updates before transition suspense retries', async () => {
let updateTransitionPri;
let updateNormalPri;
function App() {
const [transitionPri, setTransitionPri] = useState(false);
const [normalPri, setNormalPri] = useState(0);
updateTransitionPri = () => startTransition(() => setTransitionPri(true));
updateNormalPri = () => setNormalPri(n => n + 1);
useLayoutEffect(() => {
Scheduler.unstable_yieldValue('Commit');
});
return (
<Suspense fallback={<Text text="Loading..." />}>
{transitionPri ? <AsyncText text="Async" /> : <Text text="(empty)" />}
{', '}
<Text text={'Normal pri: ' + normalPri} />
</Suspense>
);
}
const root = ReactNoop.createRoot();
await act(async () => {
root.render(<App />);
});
// Initial render.
expect(Scheduler).toHaveYielded(['(empty)', 'Normal pri: 0', 'Commit']);
expect(root).toMatchRenderedOutput('(empty), Normal pri: 0');
await act(async () => {
updateTransitionPri();
});
expect(Scheduler).toHaveYielded([
// Suspend.
'Suspend! [Async]',
'Normal pri: 0',
'Loading...',
]);
expect(root).toMatchRenderedOutput('(empty), Normal pri: 0');
await act(async () => {
await resolveText('Async');
updateNormalPri();
});
expect(Scheduler).toHaveYielded([
// Normal pri update.
'(empty)',
'Normal pri: 1',
'Commit',
// Promise resolved, retry flushed.
'Async',
'Normal pri: 1',
'Commit',
]);
expect(root).toMatchRenderedOutput('Async, Normal pri: 1');
});
// @gate experimental
// @gate enableCache
it('should not interrupt transitions with normal pri updates', async () => {
let updateNormalPri;
let updateTransitionPri;
function App() {
const [transitionPri, setTransitionPri] = useState(0);
const [normalPri, setNormalPri] = useState(0);
updateTransitionPri = () =>
startTransition(() => setTransitionPri(n => n + 1));
updateNormalPri = () => setNormalPri(n => n + 1);
useLayoutEffect(() => {
Scheduler.unstable_yieldValue('Commit');
});
return (
<>
<Text text={'Transition pri: ' + transitionPri} />
{', '}
<Text text={'Normal pri: ' + normalPri} />
</>
);
}
const root = ReactNoop.createRoot();
await ReactNoop.act(async () => {
root.render(<App />);
});
expect(Scheduler).toHaveYielded([
'Transition pri: 0',
'Normal pri: 0',
'Commit',
]);
expect(root).toMatchRenderedOutput('Transition pri: 0, Normal pri: 0');
await ReactNoop.act(async () => {
updateTransitionPri();
expect(Scheduler).toFlushAndYieldThrough([
// Start transition update.
'Transition pri: 1',
]);
// Schedule normal pri update during transition update.
// This should not interrupt.
updateNormalPri();
});
expect(Scheduler).toHaveYielded([
// Finish transition update.
'Normal pri: 0',
'Commit',
// Normal pri update.
'Transition pri: 1',
'Normal pri: 1',
'Commit',
]);
expect(root).toMatchRenderedOutput('Transition pri: 1, Normal pri: 1');
});
});