forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActivity-test.js
2056 lines (1807 loc) · 52.6 KB
/
Activity-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
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
let React;
let ReactNoop;
let Scheduler;
let act;
let LegacyHidden;
let Activity;
let useState;
let useLayoutEffect;
let useEffect;
let useMemo;
let useRef;
let startTransition;
let waitForPaint;
let waitFor;
let assertLog;
describe('Activity', () => {
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactNoop = require('react-noop-renderer');
Scheduler = require('scheduler');
act = require('internal-test-utils').act;
LegacyHidden = React.unstable_LegacyHidden;
Activity = React.unstable_Activity;
useState = React.useState;
useLayoutEffect = React.useLayoutEffect;
useEffect = React.useEffect;
useMemo = React.useMemo;
useRef = React.useRef;
startTransition = React.startTransition;
const InternalTestUtils = require('internal-test-utils');
waitForPaint = InternalTestUtils.waitForPaint;
waitFor = InternalTestUtils.waitFor;
assertLog = InternalTestUtils.assertLog;
});
function Text(props) {
Scheduler.log(props.text);
return <span prop={props.text}>{props.children}</span>;
}
function LoggedText({text, children}) {
useEffect(() => {
Scheduler.log(`mount ${text}`);
return () => {
Scheduler.log(`unmount ${text}`);
};
});
useLayoutEffect(() => {
Scheduler.log(`mount layout ${text}`);
return () => {
Scheduler.log(`unmount layout ${text}`);
};
});
return <Text text={text}>{children}</Text>;
}
// @gate enableLegacyHidden
it('unstable-defer-without-hiding should never toggle the visibility of its children', async () => {
function App({mode}) {
return (
<>
<Text text="Normal" />
<LegacyHidden mode={mode}>
<Text text="Deferred" />
</LegacyHidden>
</>
);
}
// Test the initial mount
const root = ReactNoop.createRoot();
await act(async () => {
root.render(<App mode="unstable-defer-without-hiding" />);
await waitForPaint(['Normal']);
expect(root).toMatchRenderedOutput(<span prop="Normal" />);
});
assertLog(['Deferred']);
expect(root).toMatchRenderedOutput(
<>
<span prop="Normal" />
<span prop="Deferred" />
</>,
);
// Now try after an update
await act(() => {
root.render(<App mode="visible" />);
});
assertLog(['Normal', 'Deferred']);
expect(root).toMatchRenderedOutput(
<>
<span prop="Normal" />
<span prop="Deferred" />
</>,
);
await act(async () => {
root.render(<App mode="unstable-defer-without-hiding" />);
await waitForPaint(['Normal']);
expect(root).toMatchRenderedOutput(
<>
<span prop="Normal" />
<span prop="Deferred" />
</>,
);
});
assertLog(['Deferred']);
expect(root).toMatchRenderedOutput(
<>
<span prop="Normal" />
<span prop="Deferred" />
</>,
);
});
// @gate enableLegacyHidden && !disableLegacyMode
it('does not defer in legacy mode', async () => {
let setState;
function Foo() {
const [state, _setState] = useState('A');
setState = _setState;
return <Text text={state} />;
}
const root = ReactNoop.createLegacyRoot();
await act(() => {
root.render(
<>
<LegacyHidden mode="hidden">
<Foo />
</LegacyHidden>
<Text text="Outside" />
</>,
);
ReactNoop.flushSync();
// Should not defer the hidden tree
assertLog(['A', 'Outside']);
});
expect(root).toMatchRenderedOutput(
<>
<span prop="A" />
<span prop="Outside" />
</>,
);
// Test that the children can be updated
await act(() => {
setState('B');
});
assertLog(['B']);
expect(root).toMatchRenderedOutput(
<>
<span prop="B" />
<span prop="Outside" />
</>,
);
});
// @gate enableLegacyHidden
it('does defer in concurrent mode', async () => {
let setState;
function Foo() {
const [state, _setState] = useState('A');
setState = _setState;
return <Text text={state} />;
}
const root = ReactNoop.createRoot();
await act(async () => {
root.render(
<>
<LegacyHidden mode="hidden">
<Foo />
</LegacyHidden>
<Text text="Outside" />
</>,
);
// Should defer the hidden tree.
await waitForPaint(['Outside']);
});
// The hidden tree was rendered at lower priority.
assertLog(['A']);
expect(root).toMatchRenderedOutput(
<>
<span prop="A" />
<span prop="Outside" />
</>,
);
// Test that the children can be updated
await act(() => {
setState('B');
});
assertLog(['B']);
expect(root).toMatchRenderedOutput(
<>
<span prop="B" />
<span prop="Outside" />
</>,
);
});
// @gate enableActivity
it('mounts without layout effects when hidden', async () => {
function Child({text}) {
useLayoutEffect(() => {
Scheduler.log('Mount layout');
return () => {
Scheduler.log('Unmount layout');
};
}, []);
return <Text text="Child" />;
}
const root = ReactNoop.createRoot();
// Mount hidden tree.
await act(() => {
root.render(
<Activity mode="hidden">
<Child />
</Activity>,
);
});
// No layout effect.
assertLog(['Child']);
expect(root).toMatchRenderedOutput(<span hidden={true} prop="Child" />);
// Unhide the tree. The layout effect is mounted.
await act(() => {
root.render(
<Activity mode="visible">
<Child />
</Activity>,
);
});
assertLog(['Child', 'Mount layout']);
expect(root).toMatchRenderedOutput(<span prop="Child" />);
});
// @gate enableActivity
it('mounts/unmounts layout effects when visibility changes (starting visible)', async () => {
function Child({text}) {
useLayoutEffect(() => {
Scheduler.log('Mount layout');
return () => {
Scheduler.log('Unmount layout');
};
}, []);
return <Text text="Child" />;
}
const root = ReactNoop.createRoot();
await act(() => {
root.render(
<Activity mode="visible">
<Child />
</Activity>,
);
});
assertLog(['Child', 'Mount layout']);
expect(root).toMatchRenderedOutput(<span prop="Child" />);
// Hide the tree. The layout effect is unmounted.
await act(() => {
root.render(
<Activity mode="hidden">
<Child />
</Activity>,
);
});
assertLog(['Unmount layout', 'Child']);
expect(root).toMatchRenderedOutput(<span hidden={true} prop="Child" />);
// Unhide the tree. The layout effect is re-mounted.
await act(() => {
root.render(
<Activity mode="visible">
<Child />
</Activity>,
);
});
assertLog(['Child', 'Mount layout']);
expect(root).toMatchRenderedOutput(<span prop="Child" />);
});
// @gate enableActivity
it('nested offscreen does not call componentWillUnmount when hidden', async () => {
// This is a bug that appeared during production test of <unstable_Activity />.
// It is a very specific scenario with nested Offscreens. The inner offscreen
// goes from visible to hidden in synchronous update.
class ClassComponent extends React.Component {
render() {
return <Text text="child" />;
}
componentWillUnmount() {
Scheduler.log('componentWillUnmount');
}
componentDidMount() {
Scheduler.log('componentDidMount');
}
}
const root = ReactNoop.createRoot();
await act(() => {
// Outer and inner offscreen are hidden.
root.render(
<Activity mode={'hidden'}>
<Activity mode={'hidden'}>
<ClassComponent />
</Activity>
</Activity>,
);
});
assertLog(['child']);
expect(root).toMatchRenderedOutput(<span hidden={true} prop="child" />);
await act(() => {
// Inner offscreen is visible.
root.render(
<Activity mode={'hidden'}>
<Activity mode={'visible'}>
<ClassComponent />
</Activity>
</Activity>,
);
});
assertLog(['child']);
expect(root).toMatchRenderedOutput(<span hidden={true} prop="child" />);
await act(() => {
// Inner offscreen is hidden.
root.render(
<Activity mode={'hidden'}>
<Activity mode={'hidden'}>
<ClassComponent />
</Activity>
</Activity>,
);
});
assertLog(['child']);
expect(root).toMatchRenderedOutput(<span hidden={true} prop="child" />);
await act(() => {
// Inner offscreen is visible.
root.render(
<Activity mode={'hidden'}>
<Activity mode={'visible'}>
<ClassComponent />
</Activity>
</Activity>,
);
});
Scheduler.unstable_clearLog();
await act(() => {
// Outer offscreen is visible.
// Inner offscreen is hidden.
root.render(
<Activity mode={'visible'}>
<Activity mode={'hidden'}>
<ClassComponent />
</Activity>
</Activity>,
);
});
assertLog(['child']);
await act(() => {
// Outer offscreen is hidden.
// Inner offscreen is visible.
root.render(
<Activity mode={'hidden'}>
<Activity mode={'visible'}>
<ClassComponent />
</Activity>
</Activity>,
);
});
assertLog(['child']);
});
// @gate enableActivity
it('mounts/unmounts layout effects when visibility changes (starting hidden)', async () => {
function Child({text}) {
useLayoutEffect(() => {
Scheduler.log('Mount layout');
return () => {
Scheduler.log('Unmount layout');
};
}, []);
return <Text text="Child" />;
}
const root = ReactNoop.createRoot();
await act(() => {
// Start the tree hidden. The layout effect is not mounted.
root.render(
<Activity mode="hidden">
<Child />
</Activity>,
);
});
assertLog(['Child']);
expect(root).toMatchRenderedOutput(<span hidden={true} prop="Child" />);
// Show the tree. The layout effect is mounted.
await act(() => {
root.render(
<Activity mode="visible">
<Child />
</Activity>,
);
});
assertLog(['Child', 'Mount layout']);
expect(root).toMatchRenderedOutput(<span prop="Child" />);
// Hide the tree again. The layout effect is un-mounted.
await act(() => {
root.render(
<Activity mode="hidden">
<Child />
</Activity>,
);
});
assertLog(['Unmount layout', 'Child']);
expect(root).toMatchRenderedOutput(<span hidden={true} prop="Child" />);
});
// @gate enableActivity
it('hides children of offscreen after layout effects are destroyed', async () => {
const root = ReactNoop.createRoot();
function Child({text}) {
useLayoutEffect(() => {
Scheduler.log('Mount layout');
return () => {
// The child should not be hidden yet.
expect(root).toMatchRenderedOutput(<span prop="Child" />);
Scheduler.log('Unmount layout');
};
}, []);
return <Text text="Child" />;
}
await act(() => {
root.render(
<Activity mode="visible">
<Child />
</Activity>,
);
});
assertLog(['Child', 'Mount layout']);
expect(root).toMatchRenderedOutput(<span prop="Child" />);
// Hide the tree. The layout effect is unmounted.
await act(() => {
root.render(
<Activity mode="hidden">
<Child />
</Activity>,
);
});
assertLog(['Unmount layout', 'Child']);
// After the layout effect is unmounted, the child is hidden.
expect(root).toMatchRenderedOutput(<span hidden={true} prop="Child" />);
});
// @gate enableLegacyHidden
it('does not toggle effects for LegacyHidden component', async () => {
// LegacyHidden is meant to be the same as offscreen except it doesn't
// do anything to effects. Only used by www, as a temporary migration step.
function Child({text}) {
useLayoutEffect(() => {
Scheduler.log('Mount layout');
return () => {
Scheduler.log('Unmount layout');
};
}, []);
return <Text text="Child" />;
}
const root = ReactNoop.createRoot();
await act(() => {
root.render(
<LegacyHidden mode="visible">
<Child />
</LegacyHidden>,
);
});
assertLog(['Child', 'Mount layout']);
await act(() => {
root.render(
<LegacyHidden mode="hidden">
<Child />
</LegacyHidden>,
);
});
assertLog(['Child']);
await act(() => {
root.render(
<LegacyHidden mode="visible">
<Child />
</LegacyHidden>,
);
});
assertLog(['Child']);
await act(() => {
root.render(null);
});
assertLog(['Unmount layout']);
});
// @gate enableActivity
it('hides new insertions into an already hidden tree', async () => {
const root = ReactNoop.createRoot();
await act(() => {
root.render(
<Activity mode="hidden">
<span>Hi</span>
</Activity>,
);
});
expect(root).toMatchRenderedOutput(<span hidden={true}>Hi</span>);
// Insert a new node into the hidden tree
await act(() => {
root.render(
<Activity mode="hidden">
<span>Hi</span>
<span>Something new</span>
</Activity>,
);
});
expect(root).toMatchRenderedOutput(
<>
<span hidden={true}>Hi</span>
{/* This new node should also be hidden */}
<span hidden={true}>Something new</span>
</>,
);
});
// @gate enableActivity
it('hides updated nodes inside an already hidden tree', async () => {
const root = ReactNoop.createRoot();
await act(() => {
root.render(
<Activity mode="hidden">
<span>Hi</span>
</Activity>,
);
});
expect(root).toMatchRenderedOutput(<span hidden={true}>Hi</span>);
// Set the `hidden` prop to on an already hidden node
await act(() => {
root.render(
<Activity mode="hidden">
<span hidden={false}>Hi</span>
</Activity>,
);
});
// It should still be hidden, because the Activity container overrides it
expect(root).toMatchRenderedOutput(<span hidden={true}>Hi</span>);
// Unhide the boundary
await act(() => {
root.render(
<Activity mode="visible">
<span hidden={true}>Hi</span>
</Activity>,
);
});
// It should still be hidden, because of the prop
expect(root).toMatchRenderedOutput(<span hidden={true}>Hi</span>);
// Remove the `hidden` prop
await act(() => {
root.render(
<Activity mode="visible">
<span>Hi</span>
</Activity>,
);
});
// Now it's visible
expect(root).toMatchRenderedOutput(<span>Hi</span>);
});
// @gate enableActivity
it('revealing a hidden tree at high priority does not cause tearing', async () => {
// When revealing an offscreen tree, we need to include updates that were
// previously deferred because the tree was hidden, even if they are lower
// priority than the current render. However, we should *not* include low
// priority updates that are entangled with updates outside of the hidden
// tree, because that can cause tearing.
//
// This test covers a scenario where an update multiple updates inside a
// hidden tree share the same lane, but are processed at different times
// because of the timing of when they were scheduled.
// This functions checks whether the "outer" and "inner" states are
// consistent in the rendered output.
let currentOuter = null;
let currentInner = null;
function areOuterAndInnerConsistent() {
return (
currentOuter === null ||
currentInner === null ||
currentOuter === currentInner
);
}
let setInner;
function Child() {
const [inner, _setInner] = useState(0);
setInner = _setInner;
useEffect(() => {
currentInner = inner;
return () => {
currentInner = null;
};
}, [inner]);
return <Text text={'Inner: ' + inner} />;
}
let setOuter;
function App({show}) {
const [outer, _setOuter] = useState(0);
setOuter = _setOuter;
useEffect(() => {
currentOuter = outer;
return () => {
currentOuter = null;
};
}, [outer]);
return (
<>
<Text text={'Outer: ' + outer} />
<Activity mode={show ? 'visible' : 'hidden'}>
<Child />
</Activity>
</>
);
}
// Render a hidden tree
const root = ReactNoop.createRoot();
await act(() => {
root.render(<App show={false} />);
});
assertLog(['Outer: 0', 'Inner: 0']);
expect(root).toMatchRenderedOutput(
<>
<span prop="Outer: 0" />
<span hidden={true} prop="Inner: 0" />
</>,
);
expect(areOuterAndInnerConsistent()).toBe(true);
await act(async () => {
// Update a value both inside and outside the hidden tree. These values
// must always be consistent.
setOuter(1);
setInner(1);
// Only the outer updates finishes because the inner update is inside a
// hidden tree. The outer update is deferred to a later render.
await waitForPaint(['Outer: 1']);
expect(root).toMatchRenderedOutput(
<>
<span prop="Outer: 1" />
<span hidden={true} prop="Inner: 0" />
</>,
);
// Before the inner update can finish, we receive another pair of updates.
React.startTransition(() => {
setOuter(2);
setInner(2);
});
// Also, before either of these new updates are processed, the hidden
// tree is revealed at high priority.
ReactNoop.flushSync(() => {
root.render(<App show={true} />);
});
assertLog([
'Outer: 1',
// There are two pending updates on Inner, but only the first one
// is processed, even though they share the same lane. If the second
// update were erroneously processed, then Inner would be inconsistent
// with Outer.
'Inner: 1',
]);
expect(root).toMatchRenderedOutput(
<>
<span prop="Outer: 1" />
<span prop="Inner: 1" />
</>,
);
expect(areOuterAndInnerConsistent()).toBe(true);
});
assertLog(['Outer: 2', 'Inner: 2']);
expect(root).toMatchRenderedOutput(
<>
<span prop="Outer: 2" />
<span prop="Inner: 2" />
</>,
);
expect(areOuterAndInnerConsistent()).toBe(true);
});
// @gate enableActivity
it('regression: Activity instance is sometimes null during setState', async () => {
let setState;
function Child() {
const [state, _setState] = useState('Initial');
setState = _setState;
return <Text text={state} />;
}
const root = ReactNoop.createRoot();
await act(() => {
root.render(<Activity hidden={false} />);
});
assertLog([]);
expect(root).toMatchRenderedOutput(null);
await act(async () => {
// Partially render a component
startTransition(() => {
root.render(
<Activity hidden={false}>
<Child />
<Text text="Sibling" />
</Activity>,
);
});
await waitFor(['Initial']);
// Before it finishes rendering, the whole tree gets deleted
ReactNoop.flushSync(() => {
root.render(null);
});
// Something attempts to update the never-mounted component. When this
// regression test was written, we would walk up the component's return
// path and reach an unmounted Activity component fiber. Its `stateNode`
// would be null because it was nulled out when it was deleted, but there
// was no null check before we accessed it. A weird edge case but we must
// account for it.
expect(() => {
setState('Updated');
}).toErrorDev(
"Can't perform a React state update on a component that hasn't mounted yet",
);
});
expect(root).toMatchRenderedOutput(null);
});
// @gate enableActivity
it('class component setState callbacks do not fire until tree is visible', async () => {
const root = ReactNoop.createRoot();
let child;
class Child extends React.Component {
state = {text: 'A'};
render() {
child = this;
return <Text text={this.state.text} />;
}
}
// Initial render
await act(() => {
root.render(
<Activity mode="hidden">
<Child />
</Activity>,
);
});
assertLog(['A']);
expect(root).toMatchRenderedOutput(<span hidden={true} prop="A" />);
// Schedule an update to a hidden class component. The update will finish
// rendering in the background, but the callback shouldn't fire yet, because
// the component isn't visible.
await act(() => {
child.setState({text: 'B'}, () => {
Scheduler.log('B update finished');
});
});
assertLog(['B']);
expect(root).toMatchRenderedOutput(<span hidden={true} prop="B" />);
// Now reveal the hidden component. Simultaneously, schedule another
// update with a callback to the same component. When the component is
// revealed, both the B callback and C callback should fire, in that order.
await act(() => {
root.render(
<Activity mode="visible">
<Child />
</Activity>,
);
child.setState({text: 'C'}, () => {
Scheduler.log('C update finished');
});
});
assertLog(['C', 'B update finished', 'C update finished']);
expect(root).toMatchRenderedOutput(<span prop="C" />);
});
// @gate enableActivity
it('does not call componentDidUpdate when reappearing a hidden class component', async () => {
class Child extends React.Component {
componentDidMount() {
Scheduler.log('componentDidMount');
}
componentDidUpdate() {
Scheduler.log('componentDidUpdate');
}
componentWillUnmount() {
Scheduler.log('componentWillUnmount');
}
render() {
return 'Child';
}
}
// Initial mount
const root = ReactNoop.createRoot();
await act(() => {
root.render(
<Activity mode="visible">
<Child />
</Activity>,
);
});
assertLog(['componentDidMount']);
// Hide the class component
await act(() => {
root.render(
<Activity mode="hidden">
<Child />
</Activity>,
);
});
assertLog(['componentWillUnmount']);
// Reappear the class component. componentDidMount should fire, not
// componentDidUpdate.
await act(() => {
root.render(
<Activity mode="visible">
<Child />
</Activity>,
);
});
assertLog(['componentDidMount']);
});
// @gate enableActivity
it(
'when reusing old components (hidden -> visible), layout effects fire ' +
'with same timing as if it were brand new',
async () => {
function Child({label}) {
useLayoutEffect(() => {
Scheduler.log('Mount ' + label);
return () => {
Scheduler.log('Unmount ' + label);
};
}, [label]);
return label;
}
// Initial mount
const root = ReactNoop.createRoot();
await act(() => {
root.render(
<Activity mode="visible">
<Child key="B" label="B" />
</Activity>,
);
});
assertLog(['Mount B']);
// Hide the component
await act(() => {
root.render(
<Activity mode="hidden">
<Child key="B" label="B" />
</Activity>,
);
});
assertLog(['Unmount B']);
// Reappear the component and also add some new siblings.
await act(() => {
root.render(
<Activity mode="visible">
<Child key="A" label="A" />
<Child key="B" label="B" />
<Child key="C" label="C" />
</Activity>,
);
});
// B's effect should fire in between A and C even though it's been reused
// from a previous render. In other words, it's the same order as if all
// three siblings were brand new.
assertLog(['Mount A', 'Mount B', 'Mount C']);
},
);
// @gate enableActivity
it(
'when reusing old components (hidden -> visible), layout effects fire ' +
'with same timing as if it were brand new (includes setState callback)',
async () => {
class Child extends React.Component {
componentDidMount() {
Scheduler.log('Mount ' + this.props.label);
}
componentWillUnmount() {
Scheduler.log('Unmount ' + this.props.label);
}
render() {
return this.props.label;
}
}
// Initial mount
const bRef = React.createRef();
const root = ReactNoop.createRoot();
await act(() => {
root.render(
<Activity mode="visible">
<Child key="B" ref={bRef} label="B" />
</Activity>,
);
});
assertLog(['Mount B']);
// We're going to schedule an update on a hidden component, so stash a
// reference to its setState before the ref gets detached
const setStateB = bRef.current.setState.bind(bRef.current);
// Hide the component
await act(() => {
root.render(
<Activity mode="hidden">
<Child key="B" ref={bRef} label="B" />
</Activity>,
);
});
assertLog(['Unmount B']);
// Reappear the component and also add some new siblings.
await act(() => {
setStateB(null, () => {
Scheduler.log('setState callback B');
});
root.render(
<Activity mode="visible">
<Child key="A" label="A" />
<Child key="B" ref={bRef} label="B" />
<Child key="C" label="C" />
</Activity>,
);
});
// B's effect should fire in between A and C even though it's been reused
// from a previous render. In other words, it's the same order as if all
// three siblings were brand new.