-
Notifications
You must be signed in to change notification settings - Fork 48.4k
/
Copy pathuseMutableSource-test.internal.js
1854 lines (1630 loc) · 54.9 KB
/
useMutableSource-test.internal.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
/**
* 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
*/
/* eslint-disable no-func-assign */
'use strict';
let React;
let ReactFeatureFlags;
let ReactNoop;
let Scheduler;
let useMutableSource;
let act;
function loadModules() {
jest.resetModules();
jest.useFakeTimers();
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.enableSchedulerTracing = true;
ReactFeatureFlags.enableProfilerTimer = true;
React = require('react');
ReactNoop = require('react-noop-renderer');
Scheduler = require('scheduler');
useMutableSource = React.useMutableSource;
act = ReactNoop.act;
}
describe('useMutableSource', () => {
const defaultGetSnapshot = source => source.value;
const defaultSubscribe = (source, callback) => source.subscribe(callback);
function createComplexSource(initialValueA, initialValueB) {
const callbacksA = [];
const callbacksB = [];
let revision = 0;
let valueA = initialValueA;
let valueB = initialValueB;
const subscribeHelper = (callbacks, callback) => {
if (callbacks.indexOf(callback) < 0) {
callbacks.push(callback);
}
return () => {
const index = callbacks.indexOf(callback);
if (index >= 0) {
callbacks.splice(index, 1);
}
};
};
return {
subscribeA(callback) {
return subscribeHelper(callbacksA, callback);
},
subscribeB(callback) {
return subscribeHelper(callbacksB, callback);
},
get listenerCountA() {
return callbacksA.length;
},
get listenerCountB() {
return callbacksB.length;
},
set valueA(newValue) {
revision++;
valueA = newValue;
callbacksA.forEach(callback => callback());
},
get valueA() {
return valueA;
},
set valueB(newValue) {
revision++;
valueB = newValue;
callbacksB.forEach(callback => callback());
},
get valueB() {
return valueB;
},
get version() {
return revision;
},
};
}
function createSource(initialValue) {
const callbacks = [];
let revision = 0;
let value = initialValue;
return {
subscribe(callback) {
if (callbacks.indexOf(callback) < 0) {
callbacks.push(callback);
}
return () => {
const index = callbacks.indexOf(callback);
if (index >= 0) {
callbacks.splice(index, 1);
}
};
},
get listenerCount() {
return callbacks.length;
},
set value(newValue) {
revision++;
value = newValue;
callbacks.forEach(callback => callback());
},
get value() {
return value;
},
get version() {
return revision;
},
};
}
function createMutableSource(source) {
return React.createMutableSource(source, param => param.version);
}
function Component({getSnapshot, label, mutableSource, subscribe}) {
const snapshot = useMutableSource(mutableSource, getSnapshot, subscribe);
Scheduler.unstable_yieldValue(`${label}:${snapshot}`);
return <div>{`${label}:${snapshot}`}</div>;
}
beforeEach(loadModules);
// @gate experimental
it('should subscribe to a source and schedule updates when it changes', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source);
act(() => {
ReactNoop.renderToRootWithID(
<>
<Component
label="a"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
<Component
label="b"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
</>,
'root',
() => Scheduler.unstable_yieldValue('Sync effect'),
);
expect(Scheduler).toFlushAndYieldThrough([
'a:one',
'b:one',
'Sync effect',
]);
// Subscriptions should be passive
expect(source.listenerCount).toBe(0);
ReactNoop.flushPassiveEffects();
expect(source.listenerCount).toBe(2);
// Changing values should schedule an update with React
source.value = 'two';
expect(Scheduler).toFlushAndYieldThrough(['a:two', 'b:two']);
// Umounting a component should remove its subscription.
ReactNoop.renderToRootWithID(
<>
<Component
label="a"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
</>,
'root',
() => Scheduler.unstable_yieldValue('Sync effect'),
);
expect(Scheduler).toFlushAndYield(['a:two', 'Sync effect']);
ReactNoop.flushPassiveEffects();
expect(source.listenerCount).toBe(1);
// Umounting a root should remove the remaining event listeners
ReactNoop.unmountRootWithID('root');
expect(Scheduler).toFlushAndYield([]);
ReactNoop.flushPassiveEffects();
expect(source.listenerCount).toBe(0);
// Changes to source should not trigger an updates or warnings.
source.value = 'three';
expect(Scheduler).toFlushAndYield([]);
});
});
// @gate experimental
it('should restart work if a new source is mutated during render', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source);
act(() => {
ReactNoop.render(
<>
<Component
label="a"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
<Component
label="b"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
</>,
() => Scheduler.unstable_yieldValue('Sync effect'),
);
// Do enough work to read from one component
expect(Scheduler).toFlushAndYieldThrough(['a:one']);
// Mutate source before continuing work
source.value = 'two';
// Render work should restart and the updated value should be used
expect(Scheduler).toFlushAndYield(['a:two', 'b:two', 'Sync effect']);
});
});
// @gate experimental
it('should schedule an update if a new source is mutated between render and commit (subscription)', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source);
act(() => {
ReactNoop.render(
<>
<Component
label="a"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
<Component
label="b"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
</>,
() => Scheduler.unstable_yieldValue('Sync effect'),
);
// Finish rendering
expect(Scheduler).toFlushAndYieldThrough([
'a:one',
'b:one',
'Sync effect',
]);
// Mutate source before subscriptions are attached
expect(source.listenerCount).toBe(0);
source.value = 'two';
// Mutation should be detected, and a new render should be scheduled
expect(Scheduler).toFlushAndYield(['a:two', 'b:two']);
});
});
// @gate experimental
it('should unsubscribe and resubscribe if a new source is used', () => {
const sourceA = createSource('a-one');
const mutableSourceA = createMutableSource(sourceA);
const sourceB = createSource('b-one');
const mutableSourceB = createMutableSource(sourceB);
act(() => {
ReactNoop.render(
<Component
label="only"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSourceA}
subscribe={defaultSubscribe}
/>,
() => Scheduler.unstable_yieldValue('Sync effect'),
);
expect(Scheduler).toFlushAndYield(['only:a-one', 'Sync effect']);
ReactNoop.flushPassiveEffects();
expect(sourceA.listenerCount).toBe(1);
// Changing values should schedule an update with React
sourceA.value = 'a-two';
expect(Scheduler).toFlushAndYield(['only:a-two']);
// If we re-render with a new source, the old one should be unsubscribed.
ReactNoop.render(
<Component
label="only"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSourceB}
subscribe={defaultSubscribe}
/>,
() => Scheduler.unstable_yieldValue('Sync effect'),
);
expect(Scheduler).toFlushAndYield(['only:b-one', 'Sync effect']);
ReactNoop.flushPassiveEffects();
expect(sourceA.listenerCount).toBe(0);
expect(sourceB.listenerCount).toBe(1);
// Changing to original source should not schedule updates with React
sourceA.value = 'a-three';
expect(Scheduler).toFlushAndYield([]);
// Changing new source value should schedule an update with React
sourceB.value = 'b-two';
expect(Scheduler).toFlushAndYield(['only:b-two']);
});
});
// @gate experimental
it('should unsubscribe and resubscribe if a new subscribe function is provided', () => {
const source = createSource('a-one');
const mutableSource = createMutableSource(source);
const unsubscribeA = jest.fn();
const subscribeA = jest.fn(s => {
const unsubscribe = defaultSubscribe(s);
return () => {
unsubscribe();
unsubscribeA();
};
});
const unsubscribeB = jest.fn();
const subscribeB = jest.fn(s => {
const unsubscribe = defaultSubscribe(s);
return () => {
unsubscribe();
unsubscribeB();
};
});
act(() => {
ReactNoop.renderToRootWithID(
<Component
label="only"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={subscribeA}
/>,
'root',
() => Scheduler.unstable_yieldValue('Sync effect'),
);
expect(Scheduler).toFlushAndYield(['only:a-one', 'Sync effect']);
ReactNoop.flushPassiveEffects();
expect(source.listenerCount).toBe(1);
expect(subscribeA).toHaveBeenCalledTimes(1);
// If we re-render with a new subscription function,
// the old unsubscribe function should be called.
ReactNoop.renderToRootWithID(
<Component
label="only"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={subscribeB}
/>,
'root',
() => Scheduler.unstable_yieldValue('Sync effect'),
);
expect(Scheduler).toFlushAndYield(['only:a-one', 'Sync effect']);
ReactNoop.flushPassiveEffects();
expect(source.listenerCount).toBe(1);
expect(unsubscribeA).toHaveBeenCalledTimes(1);
expect(subscribeB).toHaveBeenCalledTimes(1);
// Unmounting should call the newer unsubscribe.
ReactNoop.unmountRootWithID('root');
expect(Scheduler).toFlushAndYield([]);
ReactNoop.flushPassiveEffects();
expect(source.listenerCount).toBe(0);
expect(unsubscribeB).toHaveBeenCalledTimes(1);
});
});
// @gate experimental
it('should re-use previously read snapshot value when reading is unsafe', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source);
act(() => {
ReactNoop.render(
<>
<Component
label="a"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
<Component
label="b"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
</>,
() => Scheduler.unstable_yieldValue('Sync effect'),
);
expect(Scheduler).toFlushAndYield(['a:one', 'b:one', 'Sync effect']);
// Changing values should schedule an update with React.
// Start working on this update but don't finish it.
source.value = 'two';
expect(Scheduler).toFlushAndYieldThrough(['a:two']);
// Re-renders that occur before the update is processed
// should reuse snapshot so long as the config has not changed
ReactNoop.flushSync(() => {
ReactNoop.render(
<>
<Component
label="a"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
<Component
label="b"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
</>,
() => Scheduler.unstable_yieldValue('Sync effect'),
);
});
expect(Scheduler).toHaveYielded(['a:one', 'b:one', 'Sync effect']);
expect(Scheduler).toFlushAndYield(['a:two', 'b:two']);
});
});
// @gate experimental
it('should read from source on newly mounted subtree if no pending updates are scheduled for source', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source);
act(() => {
ReactNoop.render(
<>
<Component
label="a"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
</>,
() => Scheduler.unstable_yieldValue('Sync effect'),
);
expect(Scheduler).toFlushAndYield(['a:one', 'Sync effect']);
ReactNoop.render(
<>
<Component
label="a"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
<Component
label="b"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
</>,
() => Scheduler.unstable_yieldValue('Sync effect'),
);
expect(Scheduler).toFlushAndYield(['a:one', 'b:one', 'Sync effect']);
});
});
// @gate experimental
it('should throw and restart render if source and snapshot are unavailable during an update', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source);
act(() => {
ReactNoop.render(
<>
<Component
label="a"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
<Component
label="b"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
</>,
() => Scheduler.unstable_yieldValue('Sync effect'),
);
expect(Scheduler).toFlushAndYield(['a:one', 'b:one', 'Sync effect']);
ReactNoop.flushPassiveEffects();
// Changing values should schedule an update with React.
// Start working on this update but don't finish it.
Scheduler.unstable_runWithPriority(Scheduler.unstable_LowPriority, () => {
source.value = 'two';
expect(Scheduler).toFlushAndYieldThrough(['a:two']);
});
const newGetSnapshot = s => 'new:' + defaultGetSnapshot(s);
// Force a higher priority render with a new config.
// This should signal that the snapshot is not safe and trigger a full re-render.
Scheduler.unstable_runWithPriority(
Scheduler.unstable_UserBlockingPriority,
() => {
ReactNoop.render(
<>
<Component
label="a"
getSnapshot={newGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
<Component
label="b"
getSnapshot={newGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
</>,
() => Scheduler.unstable_yieldValue('Sync effect'),
);
},
);
expect(Scheduler).toFlushAndYieldThrough([
'a:new:two',
'b:new:two',
'Sync effect',
]);
});
});
// @gate experimental
it('should throw and restart render if source and snapshot are unavailable during a sync update', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source);
act(() => {
ReactNoop.render(
<>
<Component
label="a"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
<Component
label="b"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
</>,
() => Scheduler.unstable_yieldValue('Sync effect'),
);
expect(Scheduler).toFlushAndYield(['a:one', 'b:one', 'Sync effect']);
ReactNoop.flushPassiveEffects();
// Changing values should schedule an update with React.
// Start working on this update but don't finish it.
Scheduler.unstable_runWithPriority(Scheduler.unstable_LowPriority, () => {
source.value = 'two';
expect(Scheduler).toFlushAndYieldThrough(['a:two']);
});
const newGetSnapshot = s => 'new:' + defaultGetSnapshot(s);
// Force a higher priority render with a new config.
// This should signal that the snapshot is not safe and trigger a full re-render.
ReactNoop.flushSync(() => {
ReactNoop.render(
<>
<Component
label="a"
getSnapshot={newGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
<Component
label="b"
getSnapshot={newGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
</>,
() => Scheduler.unstable_yieldValue('Sync effect'),
);
});
expect(Scheduler).toHaveYielded([
'a:new:two',
'b:new:two',
'Sync effect',
]);
});
});
// @gate experimental
it('should only update components whose subscriptions fire', () => {
const source = createComplexSource('a:one', 'b:one');
const mutableSource = createMutableSource(source);
// Subscribe to part of the store.
const getSnapshotA = s => s.valueA;
const subscribeA = (s, callback) => s.subscribeA(callback);
const getSnapshotB = s => s.valueB;
const subscribeB = (s, callback) => s.subscribeB(callback);
act(() => {
ReactNoop.render(
<>
<Component
label="a"
getSnapshot={getSnapshotA}
mutableSource={mutableSource}
subscribe={subscribeA}
/>
<Component
label="b"
getSnapshot={getSnapshotB}
mutableSource={mutableSource}
subscribe={subscribeB}
/>
</>,
() => Scheduler.unstable_yieldValue('Sync effect'),
);
expect(Scheduler).toFlushAndYield(['a:a:one', 'b:b:one', 'Sync effect']);
// Changes to part of the store (e.g. A) should not render other parts.
source.valueA = 'a:two';
expect(Scheduler).toFlushAndYield(['a:a:two']);
source.valueB = 'b:two';
expect(Scheduler).toFlushAndYield(['b:b:two']);
});
});
// @gate experimental
it('should detect tearing in part of the store not yet subscribed to', () => {
const source = createComplexSource('a:one', 'b:one');
const mutableSource = createMutableSource(source);
// Subscribe to part of the store.
const getSnapshotA = s => s.valueA;
const subscribeA = (s, callback) => s.subscribeA(callback);
const getSnapshotB = s => s.valueB;
const subscribeB = (s, callback) => s.subscribeB(callback);
act(() => {
ReactNoop.render(
<>
<Component
label="a"
getSnapshot={getSnapshotA}
mutableSource={mutableSource}
subscribe={subscribeA}
/>
</>,
() => Scheduler.unstable_yieldValue('Sync effect'),
);
expect(Scheduler).toFlushAndYield(['a:a:one', 'Sync effect']);
// Because the store has not changed yet, there are no pending updates,
// so it is considered safe to read from when we start this render.
ReactNoop.render(
<>
<Component
label="a"
getSnapshot={getSnapshotA}
mutableSource={mutableSource}
subscribe={subscribeA}
/>
<Component
label="b"
getSnapshot={getSnapshotB}
mutableSource={mutableSource}
subscribe={subscribeB}
/>
<Component
label="c"
getSnapshot={getSnapshotB}
mutableSource={mutableSource}
subscribe={subscribeB}
/>
</>,
() => Scheduler.unstable_yieldValue('Sync effect'),
);
expect(Scheduler).toFlushAndYieldThrough(['a:a:one', 'b:b:one']);
// Mutating the source should trigger a tear detection on the next read,
// which should throw and re-render the entire tree.
source.valueB = 'b:two';
expect(Scheduler).toFlushAndYield([
'a:a:one',
'b:b:two',
'c:b:two',
'Sync effect',
]);
});
});
// @gate experimental
it('does not schedule an update for subscriptions that fire with an unchanged snapshot', () => {
const MockComponent = jest.fn(Component);
const source = createSource('one');
const mutableSource = createMutableSource(source);
act(() => {
ReactNoop.render(
<MockComponent
label="only"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>,
() => Scheduler.unstable_yieldValue('Sync effect'),
);
expect(Scheduler).toFlushAndYieldThrough(['only:one', 'Sync effect']);
ReactNoop.flushPassiveEffects();
expect(source.listenerCount).toBe(1);
// Notify subscribe function but don't change the value
source.value = 'one';
expect(Scheduler).toFlushWithoutYielding();
});
});
// @gate experimental
it('should throw and restart if getSnapshot changes between scheduled update and re-render', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source);
const newGetSnapshot = s => 'new:' + defaultGetSnapshot(s);
let updateGetSnapshot;
function WrapperWithState() {
const tuple = React.useState(() => defaultGetSnapshot);
updateGetSnapshot = tuple[1];
return (
<Component
label="only"
getSnapshot={tuple[0]}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
);
}
act(() => {
ReactNoop.render(<WrapperWithState />, () =>
Scheduler.unstable_yieldValue('Sync effect'),
);
expect(Scheduler).toFlushAndYield(['only:one', 'Sync effect']);
ReactNoop.flushPassiveEffects();
// Change the source (and schedule an update).
Scheduler.unstable_runWithPriority(Scheduler.unstable_LowPriority, () => {
source.value = 'two';
});
// Schedule a higher priority update that changes getSnapshot.
Scheduler.unstable_runWithPriority(
Scheduler.unstable_UserBlockingPriority,
() => {
updateGetSnapshot(() => newGetSnapshot);
},
);
expect(Scheduler).toFlushAndYield(['only:new:two']);
});
});
// @gate experimental
it('should recover from a mutation during yield when other work is scheduled', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source);
act(() => {
// Start a render that uses the mutable source.
ReactNoop.render(
<>
<Component
label="a"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
<Component
label="b"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
</>,
);
expect(Scheduler).toFlushAndYieldThrough(['a:one']);
// Mutate source
source.value = 'two';
// Now render something different.
ReactNoop.render(<div />);
expect(Scheduler).toFlushAndYield([]);
});
});
// @gate experimental
it('should not throw if the new getSnapshot returns the same snapshot value', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source);
const onRenderA = jest.fn();
const onRenderB = jest.fn();
let updateGetSnapshot;
function WrapperWithState() {
const tuple = React.useState(() => defaultGetSnapshot);
updateGetSnapshot = tuple[1];
return (
<Component
label="b"
getSnapshot={tuple[0]}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
);
}
act(() => {
ReactNoop.render(
<>
<React.Profiler id="a" onRender={onRenderA}>
<Component
label="a"
getSnapshot={defaultGetSnapshot}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
</React.Profiler>
<React.Profiler id="b" onRender={onRenderB}>
<WrapperWithState />
</React.Profiler>
</>,
() => Scheduler.unstable_yieldValue('Sync effect'),
);
expect(Scheduler).toFlushAndYield(['a:one', 'b:one', 'Sync effect']);
ReactNoop.flushPassiveEffects();
expect(onRenderA).toHaveBeenCalledTimes(1);
expect(onRenderB).toHaveBeenCalledTimes(1);
// If B's getSnapshot function updates, but the snapshot it returns is the same,
// only B should re-render (to update its state).
updateGetSnapshot(() => s => defaultGetSnapshot(s));
expect(Scheduler).toFlushAndYield(['b:one']);
ReactNoop.flushPassiveEffects();
expect(onRenderA).toHaveBeenCalledTimes(1);
expect(onRenderB).toHaveBeenCalledTimes(2);
});
});
// @gate experimental
it('should not throw if getSnapshot changes but the source can be safely read from anyway', () => {
const source = createSource('one');
const mutableSource = createMutableSource(source);
const newGetSnapshot = s => 'new:' + defaultGetSnapshot(s);
let updateGetSnapshot;
function WrapperWithState() {
const tuple = React.useState(() => defaultGetSnapshot);
updateGetSnapshot = tuple[1];
return (
<Component
label="only"
getSnapshot={tuple[0]}
mutableSource={mutableSource}
subscribe={defaultSubscribe}
/>
);
}
act(() => {
ReactNoop.render(<WrapperWithState />, () =>
Scheduler.unstable_yieldValue('Sync effect'),
);
expect(Scheduler).toFlushAndYield(['only:one', 'Sync effect']);
ReactNoop.flushPassiveEffects();
// Change the source (and schedule an update)
// but also change the snapshot function too.
ReactNoop.batchedUpdates(() => {
source.value = 'two';
updateGetSnapshot(() => newGetSnapshot);
});
expect(Scheduler).toFlushAndYield(['only:new:two']);
});
});
// @gate experimental
it('should still schedule an update if an eager selector throws after a mutation', () => {
const source = createSource({
friends: [
{id: 1, name: 'Foo'},
{id: 2, name: 'Bar'},
],
});
const mutableSource = createMutableSource(source);
function FriendsList() {
const getSnapshot = React.useCallback(
({value}) => Array.from(value.friends),
[],
);
const friends = useMutableSource(
mutableSource,
getSnapshot,
defaultSubscribe,
);
return (
<ul>
{friends.map(friend => (
<Friend key={friend.id} id={friend.id} />
))}
</ul>
);
}
function Friend({id}) {
const getSnapshot = React.useCallback(
({value}) => {
// This selector is intentionally written in a way that will throw
// if no matching friend exists in the store.
return value.friends.find(friend => friend.id === id).name;
},
[id],
);
const name = useMutableSource(
mutableSource,
getSnapshot,
defaultSubscribe,
);
Scheduler.unstable_yieldValue(`${id}:${name}`);
return <li>{name}</li>;
}
act(() => {
ReactNoop.render(<FriendsList />, () =>
Scheduler.unstable_yieldValue('Sync effect'),
);
expect(Scheduler).toFlushAndYield(['1:Foo', '2:Bar', 'Sync effect']);
// This mutation will cause the "Bar" component to throw,
// since its value will no longer be a part of the store.
// Mutable source should still schedule an update though,
// which should unmount "Bar" and mount "Baz".
source.value = {
friends: [
{id: 1, name: 'Foo'},
{id: 3, name: 'Baz'},
],
};
expect(Scheduler).toFlushAndYield(['1:Foo', '3:Baz']);