forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathReactSuspense-test.internal.js
More file actions
927 lines (815 loc) · 27.4 KB
/
ReactSuspense-test.internal.js
File metadata and controls
927 lines (815 loc) · 27.4 KB
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
let React;
let ReactTestRenderer;
let ReactFeatureFlags;
let ReactCache;
let Suspense;
// let JestReact;
let TextResource;
let textResourceShouldFail;
// Additional tests can be found in ReactSuspenseWithNoopRenderer. Plan is
// to gradually migrate those to this file.
describe('ReactSuspense', () => {
beforeEach(() => {
jest.resetModules();
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false;
ReactFeatureFlags.replayFailedUnitOfWorkWithInvokeGuardedCallback = false;
ReactFeatureFlags.enableHooks = true;
React = require('react');
ReactTestRenderer = require('react-test-renderer');
// JestReact = require('jest-react');
ReactCache = require('react-cache');
Suspense = React.Suspense;
TextResource = ReactCache.unstable_createResource(([text, ms = 0]) => {
let listeners = null;
let status = 'pending';
let value = null;
return {
then(resolve, reject) {
switch (status) {
case 'pending': {
if (listeners === null) {
listeners = [{resolve, reject}];
setTimeout(() => {
if (textResourceShouldFail) {
ReactTestRenderer.unstable_yield(
`Promise rejected [${text}]`,
);
status = 'rejected';
value = new Error('Failed to load: ' + text);
listeners.forEach(listener => listener.reject(value));
} else {
ReactTestRenderer.unstable_yield(
`Promise resolved [${text}]`,
);
status = 'resolved';
value = text;
listeners.forEach(listener => listener.resolve(value));
}
}, ms);
} else {
listeners.push({resolve, reject});
}
break;
}
case 'resolved': {
resolve(value);
break;
}
case 'rejected': {
reject(value);
break;
}
}
},
};
}, ([text, ms]) => text);
textResourceShouldFail = false;
});
function Text(props) {
ReactTestRenderer.unstable_yield(props.text);
return props.text;
}
function AsyncText(props) {
const text = props.text;
try {
TextResource.read([props.text, props.ms]);
ReactTestRenderer.unstable_yield(text);
return text;
} catch (promise) {
if (typeof promise.then === 'function') {
ReactTestRenderer.unstable_yield(`Suspend! [${text}]`);
} else {
ReactTestRenderer.unstable_yield(`Error! [${text}]`);
}
throw promise;
}
}
it('suspends rendering and continues later', () => {
function Bar(props) {
ReactTestRenderer.unstable_yield('Bar');
return props.children;
}
function Foo() {
ReactTestRenderer.unstable_yield('Foo');
return (
<Suspense fallback={<Text text="Loading..." />}>
<Bar>
<AsyncText text="A" ms={100} />
<Text text="B" />
</Bar>
</Suspense>
);
}
const root = ReactTestRenderer.create(<Foo />, {
unstable_isConcurrent: true,
});
expect(root).toFlushAndYield([
'Foo',
'Bar',
// A suspends
'Suspend! [A]',
// But we keep rendering the siblings
'B',
'Loading...',
]);
expect(root).toMatchRenderedOutput(null);
// Flush some of the time
jest.advanceTimersByTime(50);
// Still nothing...
expect(root).toFlushWithoutYielding();
expect(root).toMatchRenderedOutput(null);
// Flush the promise completely
jest.advanceTimersByTime(50);
// Renders successfully
expect(ReactTestRenderer).toHaveYielded(['Promise resolved [A]']);
expect(root).toFlushAndYield(['Foo', 'Bar', 'A', 'B']);
expect(root).toMatchRenderedOutput('AB');
});
it('suspends siblings and later recovers each independently', () => {
// Render two sibling Suspense components
const root = ReactTestRenderer.create(
<React.Fragment>
<Suspense maxDuration={1000} fallback={<Text text="Loading A..." />}>
<AsyncText text="A" ms={5000} />
</Suspense>
<Suspense maxDuration={3000} fallback={<Text text="Loading B..." />}>
<AsyncText text="B" ms={6000} />
</Suspense>
</React.Fragment>,
{
unstable_isConcurrent: true,
},
);
expect(root).toFlushAndYield([
'Suspend! [A]',
'Loading A...',
'Suspend! [B]',
'Loading B...',
]);
expect(root).toMatchRenderedOutput(null);
// Advance time by enough to timeout both components and commit their placeholders
jest.advanceTimersByTime(4000);
expect(root).toFlushWithoutYielding();
expect(root).toMatchRenderedOutput('Loading A...Loading B...');
// Advance time by enough that the first Suspense's promise resolves and
// switches back to the normal view. The second Suspense should still
// show the placeholder
jest.advanceTimersByTime(1000);
// TODO: Should we throw if you forget to call toHaveYielded?
expect(ReactTestRenderer).toHaveYielded(['Promise resolved [A]']);
expect(root).toFlushAndYield(['A']);
expect(root).toMatchRenderedOutput('ALoading B...');
// Advance time by enough that the second Suspense's promise resolves
// and switches back to the normal view
jest.advanceTimersByTime(1000);
expect(ReactTestRenderer).toHaveYielded(['Promise resolved [B]']);
expect(root).toFlushAndYield(['B']);
expect(root).toMatchRenderedOutput('AB');
});
it('interrupts current render if promise resolves before current render phase', () => {
let didResolve = false;
let listeners = [];
const thenable = {
then(resolve) {
if (!didResolve) {
listeners.push(resolve);
} else {
resolve();
}
},
};
function resolveThenable() {
didResolve = true;
listeners.forEach(l => l());
}
function Async() {
if (!didResolve) {
ReactTestRenderer.unstable_yield('Suspend!');
throw thenable;
}
ReactTestRenderer.unstable_yield('Async');
return 'Async';
}
const root = ReactTestRenderer.create(
<Suspense maxDuration={1000} fallback={<Text text="Loading..." />}>
<Async />
<Text text="Sibling" />
</Suspense>,
{
unstable_isConcurrent: true,
},
);
expect(root).toFlushAndYieldThrough(['Suspend!']);
// The promise resolves before the current render phase has completed
resolveThenable();
expect(ReactTestRenderer).toHaveYielded([]);
// Start over from the root, instead of continuing.
expect(root).toFlushAndYield([
// Async renders again *before* Sibling
'Async',
'Sibling',
]);
expect(root).toMatchRenderedOutput('AsyncSibling');
});
it('mounts a lazy class component in non-concurrent mode', async () => {
class Class extends React.Component {
componentDidMount() {
ReactTestRenderer.unstable_yield('Did mount: ' + this.props.label);
}
componentDidUpdate() {
ReactTestRenderer.unstable_yield('Did update: ' + this.props.label);
}
render() {
return <Text text={this.props.label} />;
}
}
async function fakeImport(result) {
return {default: result};
}
const LazyClass = React.lazy(() => fakeImport(Class));
const root = ReactTestRenderer.create(
<Suspense fallback={<Text text="Loading..." />}>
<LazyClass label="Hi" />
</Suspense>,
);
expect(ReactTestRenderer).toHaveYielded(['Loading...']);
expect(root).toMatchRenderedOutput('Loading...');
await LazyClass;
expect(ReactTestRenderer).toHaveYielded(['Hi', 'Did mount: Hi']);
expect(root).toMatchRenderedOutput('Hi');
});
it('only captures if `fallback` is defined', () => {
const root = ReactTestRenderer.create(
<Suspense fallback={<Text text="Loading..." />}>
<Suspense maxDuration={100}>
<AsyncText text="Hi" ms={5000} />
</Suspense>
</Suspense>,
{
unstable_isConcurrent: true,
},
);
expect(root).toFlushAndYield([
'Suspend! [Hi]',
// The outer fallback should be rendered, because the inner one does not
// have a `fallback` prop
'Loading...',
]);
jest.advanceTimersByTime(1000);
expect(ReactTestRenderer).toHaveYielded([]);
expect(root).toFlushAndYield([]);
expect(root).toMatchRenderedOutput('Loading...');
jest.advanceTimersByTime(5000);
expect(ReactTestRenderer).toHaveYielded(['Promise resolved [Hi]']);
expect(root).toFlushAndYield(['Hi']);
expect(root).toMatchRenderedOutput('Hi');
});
it('throws if tree suspends and none of the Suspense ancestors have a fallback', () => {
const root = ReactTestRenderer.create(
<Suspense>
<AsyncText text="Hi" ms={1000} />
</Suspense>,
{
unstable_isConcurrent: true,
},
);
let err;
try {
root.unstable_flushAll();
} catch (e) {
err = e;
}
expect(err.message.replace(/at .+?:\d+/g, 'at **')).toBe(
'AsyncText suspended while rendering, but no fallback UI was specified.\n' +
'\n' +
'Add a <Suspense fallback=...> component higher in the tree to provide ' +
'a loading indicator or placeholder to display.\n' +
(__DEV__ ? ' in AsyncText (at **)\n' : ' in AsyncText\n') +
(__DEV__ ? ' in Suspense (at **)' : ' in Suspense'),
);
expect(ReactTestRenderer).toHaveYielded(['Suspend! [Hi]', 'Suspend! [Hi]']);
});
describe('outside concurrent mode', () => {
it('a mounted class component can suspend without losing state', () => {
class TextWithLifecycle extends React.Component {
componentDidMount() {
ReactTestRenderer.unstable_yield(`Mount [${this.props.text}]`);
}
componentDidUpdate() {
ReactTestRenderer.unstable_yield(`Update [${this.props.text}]`);
}
componentWillUnmount() {
ReactTestRenderer.unstable_yield(`Unmount [${this.props.text}]`);
}
render() {
return <Text {...this.props} />;
}
}
let instance;
class AsyncTextWithLifecycle extends React.Component {
state = {step: 1};
componentDidMount() {
ReactTestRenderer.unstable_yield(
`Mount [${this.props.text}:${this.state.step}]`,
);
}
componentDidUpdate() {
ReactTestRenderer.unstable_yield(
`Update [${this.props.text}:${this.state.step}]`,
);
}
componentWillUnmount() {
ReactTestRenderer.unstable_yield(
`Unmount [${this.props.text}:${this.state.step}]`,
);
}
render() {
instance = this;
const text = `${this.props.text}:${this.state.step}`;
const ms = this.props.ms;
try {
TextResource.read([text, ms]);
ReactTestRenderer.unstable_yield(text);
return text;
} catch (promise) {
if (typeof promise.then === 'function') {
ReactTestRenderer.unstable_yield(`Suspend! [${text}]`);
} else {
ReactTestRenderer.unstable_yield(`Error! [${text}]`);
}
throw promise;
}
}
}
function App() {
return (
<Suspense
maxDuration={1000}
fallback={<TextWithLifecycle text="Loading..." />}>
<TextWithLifecycle text="A" />
<AsyncTextWithLifecycle ms={100} text="B" ref={instance} />
<TextWithLifecycle text="C" />
</Suspense>
);
}
const root = ReactTestRenderer.create(<App />);
expect(ReactTestRenderer).toHaveYielded([
'A',
'Suspend! [B:1]',
'C',
'Loading...',
'Mount [A]',
// B's lifecycle should not fire because it suspended
// 'Mount [B]',
'Mount [C]',
'Mount [Loading...]',
]);
expect(root).toMatchRenderedOutput('Loading...');
jest.advanceTimersByTime(100);
expect(ReactTestRenderer).toHaveYielded([
'Promise resolved [B:1]',
'B:1',
'Unmount [Loading...]',
// Should be a mount, not an update
'Mount [B:1]',
]);
expect(root).toMatchRenderedOutput('AB:1C');
instance.setState({step: 2});
expect(ReactTestRenderer).toHaveYielded([
'Suspend! [B:2]',
'Loading...',
'Mount [Loading...]',
]);
expect(root).toMatchRenderedOutput('Loading...');
jest.advanceTimersByTime(100);
expect(ReactTestRenderer).toHaveYielded([
'Promise resolved [B:2]',
'B:2',
'Unmount [Loading...]',
'Update [B:2]',
]);
expect(root).toMatchRenderedOutput('AB:2C');
});
it('bails out on timed-out primary children even if they receive an update', () => {
let instance;
class Stateful extends React.Component {
state = {step: 1};
render() {
instance = this;
return <Text text={`Stateful: ${this.state.step}`} />;
}
}
function App(props) {
return (
<Suspense fallback={<Text text="Loading..." />}>
<Stateful />
<AsyncText ms={1000} text={props.text} />
</Suspense>
);
}
const root = ReactTestRenderer.create(<App text="A" />);
expect(ReactTestRenderer).toHaveYielded([
'Stateful: 1',
'Suspend! [A]',
'Loading...',
]);
jest.advanceTimersByTime(1000);
expect(ReactTestRenderer).toHaveYielded(['Promise resolved [A]', 'A']);
expect(root).toMatchRenderedOutput('Stateful: 1A');
root.update(<App text="B" />);
expect(ReactTestRenderer).toHaveYielded([
'Stateful: 1',
'Suspend! [B]',
'Loading...',
]);
expect(root).toMatchRenderedOutput('Loading...');
instance.setState({step: 2});
expect(ReactTestRenderer).toHaveYielded(['Stateful: 2', 'Suspend! [B]']);
expect(root).toMatchRenderedOutput('Loading...');
jest.advanceTimersByTime(1000);
expect(ReactTestRenderer).toHaveYielded([
'Promise resolved [B]',
'B',
'B',
]);
expect(root).toMatchRenderedOutput('Stateful: 2B');
});
it('when updating a timed-out tree, always retries the suspended component', () => {
let instance;
class Stateful extends React.Component {
state = {step: 1};
render() {
instance = this;
return <Text text={`Stateful: ${this.state.step}`} />;
}
}
const Indirection = React.Fragment;
function App(props) {
return (
<Suspense fallback={<Text text="Loading..." />}>
<Stateful />
<Indirection>
<Indirection>
<Indirection>
<AsyncText ms={1000} text={props.text} />
</Indirection>
</Indirection>
</Indirection>
</Suspense>
);
}
const root = ReactTestRenderer.create(<App text="A" />);
expect(ReactTestRenderer).toHaveYielded([
'Stateful: 1',
'Suspend! [A]',
'Loading...',
]);
jest.advanceTimersByTime(1000);
expect(ReactTestRenderer).toHaveYielded(['Promise resolved [A]', 'A']);
expect(root).toMatchRenderedOutput('Stateful: 1A');
root.update(<App text="B" />);
expect(ReactTestRenderer).toHaveYielded([
'Stateful: 1',
'Suspend! [B]',
'Loading...',
]);
expect(root).toMatchRenderedOutput('Loading...');
instance.setState({step: 2});
expect(ReactTestRenderer).toHaveYielded([
'Stateful: 2',
// The suspended component should suspend again. If it doesn't, the
// likely mistake is that the suspended fiber wasn't marked with
// pending work, so it was improperly treated as complete.
'Suspend! [B]',
]);
expect(root).toMatchRenderedOutput('Loading...');
jest.advanceTimersByTime(1000);
expect(ReactTestRenderer).toHaveYielded([
'Promise resolved [B]',
'B',
'B',
]);
expect(root).toMatchRenderedOutput('Stateful: 2B');
});
it('suspends in a class that has componentWillUnmount and is then deleted', () => {
class AsyncTextWithUnmount extends React.Component {
componentWillUnmount() {
ReactTestRenderer.unstable_yield('will unmount');
}
render() {
const text = this.props.text;
const ms = this.props.ms;
try {
TextResource.read([text, ms]);
ReactTestRenderer.unstable_yield(text);
return text;
} catch (promise) {
if (typeof promise.then === 'function') {
ReactTestRenderer.unstable_yield(`Suspend! [${text}]`);
} else {
ReactTestRenderer.unstable_yield(`Error! [${text}]`);
}
throw promise;
}
}
}
function App({text}) {
return (
<Suspense fallback={<Text text="Loading..." />}>
<AsyncTextWithUnmount text={text} ms={100} />
</Suspense>
);
}
const root = ReactTestRenderer.create(<App text="A" />);
expect(ReactTestRenderer).toHaveYielded(['Suspend! [A]', 'Loading...']);
root.update(<Text text="B" />);
// Should not fire componentWillUnmount
expect(ReactTestRenderer).toHaveYielded(['B']);
expect(root).toMatchRenderedOutput('B');
});
it('suspends in a component that also contains useEffect', () => {
const {useLayoutEffect} = React;
function AsyncTextWithEffect(props) {
const text = props.text;
useLayoutEffect(
() => {
ReactTestRenderer.unstable_yield('Did commit: ' + text);
},
[text],
);
try {
TextResource.read([props.text, props.ms]);
ReactTestRenderer.unstable_yield(text);
return text;
} catch (promise) {
if (typeof promise.then === 'function') {
ReactTestRenderer.unstable_yield(`Suspend! [${text}]`);
} else {
ReactTestRenderer.unstable_yield(`Error! [${text}]`);
}
throw promise;
}
}
function App({text}) {
return (
<Suspense fallback={<Text text="Loading..." />}>
<AsyncTextWithEffect text={text} ms={100} />
</Suspense>
);
}
ReactTestRenderer.create(<App text="A" />);
expect(ReactTestRenderer).toHaveYielded(['Suspend! [A]', 'Loading...']);
jest.advanceTimersByTime(500);
expect(ReactTestRenderer).toHaveYielded([
'Promise resolved [A]',
'A',
'Did commit: A',
]);
});
it('retries when an update is scheduled on a timed out tree', () => {
let instance;
class Stateful extends React.Component {
state = {step: 1};
render() {
instance = this;
return <AsyncText ms={1000} text={`Step: ${this.state.step}`} />;
}
}
function App(props) {
return (
<Suspense maxDuration={10} fallback={<Text text="Loading..." />}>
<Stateful />
</Suspense>
);
}
const root = ReactTestRenderer.create(<App />, {
unstable_isConcurrent: true,
});
// Initial render
expect(root).toFlushAndYield(['Suspend! [Step: 1]', 'Loading...']);
jest.advanceTimersByTime(1000);
expect(ReactTestRenderer).toHaveYielded(['Promise resolved [Step: 1]']);
expect(root).toFlushAndYield(['Step: 1']);
expect(root).toMatchRenderedOutput('Step: 1');
// Update that suspends
instance.setState({step: 2});
expect(root).toFlushAndYield(['Suspend! [Step: 2]', 'Loading...']);
jest.advanceTimersByTime(500);
expect(root).toMatchRenderedOutput('Loading...');
// Update while still suspended
instance.setState({step: 3});
expect(root).toFlushAndYield(['Suspend! [Step: 3]']);
expect(root).toMatchRenderedOutput('Loading...');
jest.advanceTimersByTime(1000);
expect(ReactTestRenderer).toHaveYielded([
'Promise resolved [Step: 2]',
'Promise resolved [Step: 3]',
]);
expect(root).toFlushAndYield(['Step: 3']);
expect(root).toMatchRenderedOutput('Step: 3');
});
it('does not remount the fallback while suspended children resolve in sync mode', () => {
let mounts = 0;
class ShouldMountOnce extends React.Component {
componentDidMount() {
mounts++;
}
render() {
return <Text text="Loading..." />;
}
}
function App(props) {
return (
<Suspense maxDuration={10} fallback={<ShouldMountOnce />}>
<AsyncText ms={1000} text="Child 1" />
<AsyncText ms={2000} text="Child 2" />
<AsyncText ms={3000} text="Child 3" />
</Suspense>
);
}
const root = ReactTestRenderer.create(<App />);
// Initial render
expect(ReactTestRenderer).toHaveYielded([
'Suspend! [Child 1]',
'Suspend! [Child 2]',
'Suspend! [Child 3]',
'Loading...',
]);
expect(root).toFlushAndYield([]);
jest.advanceTimersByTime(1000);
expect(ReactTestRenderer).toHaveYielded([
'Promise resolved [Child 1]',
'Child 1',
'Suspend! [Child 2]',
'Suspend! [Child 3]',
]);
jest.advanceTimersByTime(1000);
expect(ReactTestRenderer).toHaveYielded([
'Promise resolved [Child 2]',
'Child 2',
'Suspend! [Child 3]',
// TODO: This suspends twice because there were two pings, once for each
// time Child 2 suspended. This is only an issue in sync mode because
// pings aren't batched.
'Suspend! [Child 3]',
]);
jest.advanceTimersByTime(1000);
expect(ReactTestRenderer).toHaveYielded([
'Promise resolved [Child 3]',
'Child 3',
]);
expect(root).toMatchRenderedOutput(
['Child 1', 'Child 2', 'Child 3'].join(''),
);
expect(mounts).toBe(1);
});
it('does not get stuck with fallback in concurrent mode for a large delay', () => {
function App(props) {
return (
<Suspense maxDuration={10} fallback={<Text text="Loading..." />}>
<AsyncText ms={1000} text="Child 1" />
<AsyncText ms={7000} text="Child 2" />
</Suspense>
);
}
const root = ReactTestRenderer.create(<App />, {
unstable_isConcurrent: true,
});
expect(root).toFlushAndYield([
'Suspend! [Child 1]',
'Suspend! [Child 2]',
'Loading...',
]);
jest.advanceTimersByTime(1000);
expect(ReactTestRenderer).toHaveYielded(['Promise resolved [Child 1]']);
expect(root).toFlushAndYield(['Child 1', 'Suspend! [Child 2]']);
jest.advanceTimersByTime(6000);
expect(ReactTestRenderer).toHaveYielded(['Promise resolved [Child 2]']);
expect(root).toFlushAndYield(['Child 1', 'Child 2']);
expect(root).toMatchRenderedOutput(['Child 1', 'Child 2'].join(''));
});
it('reuses effects, including deletions, from the suspended tree', () => {
const {useState} = React;
let setTab;
function App() {
const [tab, _setTab] = useState(0);
setTab = _setTab;
return (
<Suspense fallback={<Text text="Loading..." />}>
<AsyncText key={tab} text={'Tab: ' + tab} ms={1000} />
<Text key={tab + 'sibling'} text=" + sibling" />
</Suspense>
);
}
const root = ReactTestRenderer.create(<App />);
expect(ReactTestRenderer).toHaveYielded([
'Suspend! [Tab: 0]',
' + sibling',
'Loading...',
]);
expect(root).toMatchRenderedOutput('Loading...');
jest.advanceTimersByTime(1000);
expect(ReactTestRenderer).toHaveYielded([
'Promise resolved [Tab: 0]',
'Tab: 0',
]);
expect(root).toMatchRenderedOutput('Tab: 0 + sibling');
setTab(1);
expect(ReactTestRenderer).toHaveYielded([
'Suspend! [Tab: 1]',
' + sibling',
'Loading...',
]);
expect(root).toMatchRenderedOutput('Loading...');
jest.advanceTimersByTime(1000);
expect(ReactTestRenderer).toHaveYielded([
'Promise resolved [Tab: 1]',
'Tab: 1',
]);
expect(root).toMatchRenderedOutput('Tab: 1 + sibling');
setTab(2);
expect(ReactTestRenderer).toHaveYielded([
'Suspend! [Tab: 2]',
' + sibling',
'Loading...',
]);
expect(root).toMatchRenderedOutput('Loading...');
jest.advanceTimersByTime(1000);
expect(ReactTestRenderer).toHaveYielded([
'Promise resolved [Tab: 2]',
'Tab: 2',
]);
expect(root).toMatchRenderedOutput('Tab: 2 + sibling');
});
it('does not warn if an mounted component is pinged', () => {
const {useState} = React;
const root = ReactTestRenderer.create(null);
let setStep;
function UpdatingText({text, ms}) {
const [step, _setStep] = useState(0);
setStep = _setStep;
const fullText = `${text}:${step}`;
try {
TextResource.read([fullText, ms]);
ReactTestRenderer.unstable_yield(fullText);
return fullText;
} catch (promise) {
if (typeof promise.then === 'function') {
ReactTestRenderer.unstable_yield(`Suspend! [${fullText}]`);
} else {
ReactTestRenderer.unstable_yield(`Error! [${fullText}]`);
}
throw promise;
}
}
root.update(
<Suspense fallback={<Text text="Loading..." />}>
<UpdatingText text="A" ms={1000} />
</Suspense>,
);
expect(ReactTestRenderer).toHaveYielded(['Suspend! [A:0]', 'Loading...']);
jest.advanceTimersByTime(1000);
expect(ReactTestRenderer).toHaveYielded([
'Promise resolved [A:0]',
'A:0',
]);
expect(root).toMatchRenderedOutput('A:0');
setStep(1);
expect(ReactTestRenderer).toHaveYielded(['Suspend! [A:1]', 'Loading...']);
expect(root).toMatchRenderedOutput('Loading...');
root.update(null);
expect(root).toFlushWithoutYielding();
jest.advanceTimersByTime(1000);
});
it('#14162', () => {
const {lazy} = React;
function Hello() {
return <span>hello</span>;
}
async function fetchComponent() {
return new Promise(r => {
// simulating a delayed import() call
setTimeout(r, 1000, {default: Hello});
});
}
const LazyHello = lazy(fetchComponent);
class App extends React.Component {
state = {render: false};
componentDidMount() {
setTimeout(() => this.setState({render: true}));
}
render() {
return (
<Suspense fallback={<span>loading...</span>}>
{this.state.render && <LazyHello />}
</Suspense>
);
}
}
const root = ReactTestRenderer.create(null);
root.update(<App name="world" />);
jest.advanceTimersByTime(1000);
});
});
});