-
Notifications
You must be signed in to change notification settings - Fork 48.1k
/
Copy pathuseSyncExternalStoreShared-test.js
647 lines (558 loc) · 18.4 KB
/
useSyncExternalStoreShared-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
/**
* 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
*/
'use strict';
let useSyncExternalStore;
let useSyncExternalStoreExtra;
let React;
let ReactNoop;
let Scheduler;
let act;
let useState;
let useEffect;
let useLayoutEffect;
// This tests shared behavior between the built-in and shim implementations of
// of useSyncExternalStore.
describe('Shared useSyncExternalStore behavior (shim and built-in)', () => {
beforeEach(() => {
jest.resetModules();
// Remove the built-in API from the React exports to force the package to
// use the shim.
// TODO: Don't do this during a variant test run. That way these tests run
// against both the shim and the built-in implementation.
jest.mock('react', () => {
// eslint-disable-next-line no-unused-vars
const {startTransition, ...otherExports} = jest.requireActual('react');
return otherExports;
});
React = require('react');
ReactNoop = require('react-noop-renderer');
Scheduler = require('scheduler');
useState = React.useState;
useEffect = React.useEffect;
useLayoutEffect = React.useLayoutEffect;
const internalAct = require('jest-react').act;
// The internal act implementation doesn't batch updates by default, since
// it's mostly used to test concurrent mode. But since these tests run
// in both concurrent and legacy mode, I'm adding batching here.
act = cb => internalAct(() => ReactNoop.batchedUpdates(cb));
useSyncExternalStore = require('use-sync-external-store')
.useSyncExternalStore;
useSyncExternalStoreExtra = require('use-sync-external-store/extra')
.useSyncExternalStoreExtra;
});
function Text({text}) {
Scheduler.unstable_yieldValue(text);
return text;
}
function createRoot(element) {
// This wrapper function exists so we can test both legacy roots and
// concurrent roots.
//
// TODO: Once the built-in API exists, conditionally test the concurrent
// root API, too.
const root = ReactNoop.createLegacyRoot();
act(() => {
root.render(element);
});
return root;
}
function createExternalStore(initialState) {
const listeners = new Set();
let currentState = initialState;
return {
set(text) {
currentState = text;
ReactNoop.batchedUpdates(() => {
listeners.forEach(listener => listener());
});
},
subscribe(listener) {
listeners.add(listener);
return () => listeners.delete(listener);
},
getState() {
return currentState;
},
getSubscriberCount() {
return listeners.size;
},
};
}
test('basic usage', () => {
const store = createExternalStore('Initial');
function App() {
const text = useSyncExternalStore(store.subscribe, store.getState);
return <Text text={text} />;
}
const root = createRoot(<App />);
expect(Scheduler).toHaveYielded(['Initial']);
expect(root).toMatchRenderedOutput('Initial');
act(() => {
store.set('Updated');
});
expect(Scheduler).toHaveYielded(['Updated']);
expect(root).toMatchRenderedOutput('Updated');
});
test('skips re-rendering if nothing changes', () => {
const store = createExternalStore('Initial');
function App() {
const text = useSyncExternalStore(store.subscribe, store.getState);
return <Text text={text} />;
}
const root = createRoot(<App />);
expect(Scheduler).toHaveYielded(['Initial']);
expect(root).toMatchRenderedOutput('Initial');
// Update to the same value
act(() => {
store.set('Initial');
});
// Should not re-render
expect(Scheduler).toHaveYielded([]);
expect(root).toMatchRenderedOutput('Initial');
});
test('switch to a different store', () => {
const storeA = createExternalStore(0);
const storeB = createExternalStore(0);
let setStore;
function App() {
const [store, _setStore] = useState(storeA);
setStore = _setStore;
const value = useSyncExternalStore(store.subscribe, store.getState);
return <Text text={value} />;
}
const root = createRoot(<App />);
expect(Scheduler).toHaveYielded([0]);
expect(root).toMatchRenderedOutput('0');
act(() => {
storeA.set(1);
});
expect(Scheduler).toHaveYielded([1]);
expect(root).toMatchRenderedOutput('1');
// Switch stores
act(() => {
// This update will be disregarded
storeA.set(2);
setStore(storeB);
});
// Now reading from B instead of A
expect(Scheduler).toHaveYielded([0]);
expect(root).toMatchRenderedOutput('0');
// Update A
act(() => {
storeA.set(3);
});
// Nothing happened, because we're no longer subscribed to A
expect(Scheduler).toHaveYielded([]);
expect(root).toMatchRenderedOutput('0');
// Update B
act(() => {
storeB.set(1);
});
expect(Scheduler).toHaveYielded([1]);
expect(root).toMatchRenderedOutput('1');
});
test('selecting a specific value inside getSnapshot', () => {
const store = createExternalStore({a: 0, b: 0});
function A() {
const a = useSyncExternalStore(store.subscribe, () => store.getState().a);
return <Text text={'A' + a} />;
}
function B() {
const b = useSyncExternalStore(store.subscribe, () => store.getState().b);
return <Text text={'B' + b} />;
}
function App() {
return (
<>
<A />
<B />
</>
);
}
const root = createRoot(<App />);
expect(Scheduler).toHaveYielded(['A0', 'B0']);
expect(root).toMatchRenderedOutput('A0B0');
// Update b but not a
act(() => {
store.set({a: 0, b: 1});
});
// Only b re-renders
expect(Scheduler).toHaveYielded(['B1']);
expect(root).toMatchRenderedOutput('A0B1');
// Update a but not b
act(() => {
store.set({a: 1, b: 1});
});
// Only a re-renders
expect(Scheduler).toHaveYielded(['A1']);
expect(root).toMatchRenderedOutput('A1B1');
});
test(
"compares to current state before bailing out, even when there's a " +
'mutation in between the sync and passive effects',
() => {
const store = createExternalStore(0);
function App() {
const value = useSyncExternalStore(store.subscribe, store.getState);
useEffect(() => {
Scheduler.unstable_yieldValue('Passive effect: ' + value);
}, [value]);
return <Text text={value} />;
}
const root = createRoot(<App />);
expect(Scheduler).toHaveYielded([0, 'Passive effect: 0']);
// Schedule an update. We'll intentionally not use `act` so that we can
// insert a mutation before React subscribes to the store in a
// passive effect.
store.set(1);
expect(Scheduler).toHaveYielded([
1,
// Passive effect hasn't fired yet
]);
expect(root).toMatchRenderedOutput('1');
// Flip the store state back to the previous value.
store.set(0);
expect(Scheduler).toHaveYielded([
'Passive effect: 1',
// Re-render. If the current state were tracked by updating a ref in a
// passive effect, then this would break because the previous render's
// passive effect hasn't fired yet, so we'd incorrectly think that
// the state hasn't changed.
0,
]);
// Should flip back to 0
expect(root).toMatchRenderedOutput('0');
},
);
test('mutating the store in between render and commit when getSnapshot has changed', () => {
const store = createExternalStore({a: 1, b: 1});
const getSnapshotA = () => store.getState().a;
const getSnapshotB = () => store.getState().b;
function Child1({step}) {
const value = useSyncExternalStore(store.subscribe, store.getState);
useLayoutEffect(() => {
if (step === 1) {
// Update B in a layout effect. This happens in the same commit
// that changed the getSnapshot in Child2. Child2's effects haven't
// fired yet, so it doesn't have access to the latest getSnapshot. So
// it can't use the getSnapshot to bail out.
Scheduler.unstable_yieldValue('Update B in commit phase');
store.set({a: value.a, b: 2});
}
}, [step]);
return null;
}
function Child2({step}) {
const label = step === 0 ? 'A' : 'B';
const getSnapshot = step === 0 ? getSnapshotA : getSnapshotB;
const value = useSyncExternalStore(store.subscribe, getSnapshot);
return <Text text={label + value} />;
}
let setStep;
function App() {
const [step, _setStep] = useState(0);
setStep = _setStep;
return (
<>
<Child1 step={step} />
<Child2 step={step} />
</>
);
}
const root = createRoot(<App />);
expect(Scheduler).toHaveYielded(['A1']);
expect(root).toMatchRenderedOutput('A1');
act(() => {
// Change getSnapshot and update the store in the same batch
setStep(1);
});
expect(Scheduler).toHaveYielded([
'B1',
'Update B in commit phase',
// If Child2 had used the old getSnapshot to bail out, then it would have
// incorrectly bailed out here instead of re-rendering.
'B2',
]);
expect(root).toMatchRenderedOutput('B2');
});
test('mutating the store in between render and commit when getSnapshot has _not_ changed', () => {
// Same as previous test, but `getSnapshot` does not change
const store = createExternalStore({a: 1, b: 1});
const getSnapshotA = () => store.getState().a;
function Child1({step}) {
const value = useSyncExternalStore(store.subscribe, store.getState);
useLayoutEffect(() => {
if (step === 1) {
// Update B in a layout effect. This happens in the same commit
// that changed the getSnapshot in Child2. Child2's effects haven't
// fired yet, so it doesn't have access to the latest getSnapshot. So
// it can't use the getSnapshot to bail out.
Scheduler.unstable_yieldValue('Update B in commit phase');
store.set({a: value.a, b: 2});
}
}, [step]);
return null;
}
function Child2({step}) {
const value = useSyncExternalStore(store.subscribe, getSnapshotA);
return <Text text={'A' + value} />;
}
let setStep;
function App() {
const [step, _setStep] = useState(0);
setStep = _setStep;
return (
<>
<Child1 step={step} />
<Child2 step={step} />
</>
);
}
const root = createRoot(<App />);
expect(Scheduler).toHaveYielded(['A1']);
expect(root).toMatchRenderedOutput('A1');
// This will cause a layout effect, and in the layout effect we'll update
// the store
act(() => {
setStep(1);
});
expect(Scheduler).toHaveYielded([
'A1',
// This updates B, but since Child2 doesn't subscribe to B, it doesn't
// need to re-render.
'Update B in commit phase',
// No re-render
]);
expect(root).toMatchRenderedOutput('A1');
});
test("does not bail out if the previous update hasn't finished yet", () => {
const store = createExternalStore(0);
function Child1() {
const value = useSyncExternalStore(store.subscribe, store.getState);
useLayoutEffect(() => {
if (value === 1) {
Scheduler.unstable_yieldValue('Reset back to 0');
store.set(0);
}
}, [value]);
return <Text text={value} />;
}
function Child2() {
const value = useSyncExternalStore(store.subscribe, store.getState);
return <Text text={value} />;
}
const root = createRoot(
<>
<Child1 />
<Child2 />
</>,
);
expect(Scheduler).toHaveYielded([0, 0]);
expect(root).toMatchRenderedOutput('00');
act(() => {
store.set(1);
});
expect(Scheduler).toHaveYielded([1, 1, 'Reset back to 0', 0, 0]);
expect(root).toMatchRenderedOutput('00');
});
test('uses the latest getSnapshot, even if it changed in the same batch as a store update', () => {
const store = createExternalStore({a: 0, b: 0});
const getSnapshotA = () => store.getState().a;
const getSnapshotB = () => store.getState().b;
let setGetSnapshot;
function App() {
const [getSnapshot, _setGetSnapshot] = useState(() => getSnapshotA);
setGetSnapshot = _setGetSnapshot;
const text = useSyncExternalStore(store.subscribe, getSnapshot);
return <Text text={text} />;
}
const root = createRoot(<App />);
expect(Scheduler).toHaveYielded([0]);
// Update the store and getSnapshot at the same time
act(() => {
setGetSnapshot(() => getSnapshotB);
store.set({a: 1, b: 2});
});
// It should read from B instead of A
expect(Scheduler).toHaveYielded([2]);
expect(root).toMatchRenderedOutput('2');
});
test('handles errors thrown by getSnapshot or isEqual', () => {
class ErrorBoundary extends React.Component {
state = {error: null};
static getDerivedStateFromError(error) {
return {error};
}
render() {
if (this.state.error) {
return <Text text={this.state.error.message} />;
}
return this.props.children;
}
}
const store = createExternalStore({
value: 0,
throwInGetSnapshot: false,
throwInIsEqual: false,
});
function App() {
const {value} = useSyncExternalStore(
store.subscribe,
() => {
const state = store.getState();
if (state.throwInGetSnapshot) {
throw new Error('Error in getSnapshot');
}
return state;
},
{
isEqual: (a, b) => {
if (a.throwInIsEqual || b.throwInIsEqual) {
throw new Error('Error in isEqual');
}
return a.value === b.value;
},
},
);
return <Text text={value} />;
}
const errorBoundary = React.createRef(null);
const root = createRoot(
<ErrorBoundary ref={errorBoundary}>
<App />
</ErrorBoundary>,
);
expect(Scheduler).toHaveYielded([0]);
expect(root).toMatchRenderedOutput('0');
// Update that throws in a getSnapshot. We can catch it with an error boundary.
act(() => {
store.set({value: 1, throwInGetSnapshot: true, throwInIsEqual: false});
});
expect(Scheduler).toHaveYielded(['Error in getSnapshot']);
expect(root).toMatchRenderedOutput('Error in getSnapshot');
// Clear the error.
act(() => {
store.set({value: 1, throwInGetSnapshot: false, throwInIsEqual: false});
errorBoundary.current.setState({error: null});
});
expect(Scheduler).toHaveYielded([1]);
expect(root).toMatchRenderedOutput('1');
// Update that throws in isEqual. Since isEqual only prevents a bail out,
// we don't need to surface an error. But we do have to re-render.
act(() => {
store.set({value: 1, throwInGetSnapshot: false, throwInIsEqual: true});
});
expect(Scheduler).toHaveYielded([1]);
expect(root).toMatchRenderedOutput('1');
});
describe('extra features implemented in user-space', () => {
test('memoized selectors are only called once per update', () => {
const store = createExternalStore({a: 0, b: 0});
function selector(state) {
Scheduler.unstable_yieldValue('Selector');
return state.a;
}
function App() {
Scheduler.unstable_yieldValue('App');
const a = useSyncExternalStoreExtra(
store.subscribe,
store.getState,
selector,
);
return <Text text={'A' + a} />;
}
const root = createRoot(<App />);
expect(Scheduler).toHaveYielded(['App', 'Selector', 'A0']);
expect(root).toMatchRenderedOutput('A0');
// Update the store
act(() => {
store.set({a: 1, b: 0});
});
expect(Scheduler).toHaveYielded([
// The selector runs before React starts rendering
'Selector',
'App',
// And because the selector didn't change during render, we can reuse
// the previous result without running the selector again
'A1',
]);
expect(root).toMatchRenderedOutput('A1');
});
test('Using isEqual to bailout', () => {
const store = createExternalStore({a: 0, b: 0});
function A() {
const {a} = useSyncExternalStoreExtra(
store.subscribe,
store.getState,
state => ({a: state.a}),
(state1, state2) => state1.a === state2.a,
);
return <Text text={'A' + a} />;
}
function B() {
const {b} = useSyncExternalStoreExtra(
store.subscribe,
store.getState,
state => {
return {b: state.b};
},
(state1, state2) => state1.b === state2.b,
);
return <Text text={'B' + b} />;
}
function App() {
return (
<>
<A />
<B />
</>
);
}
const root = createRoot(<App />);
expect(Scheduler).toHaveYielded(['A0', 'B0']);
expect(root).toMatchRenderedOutput('A0B0');
// Update b but not a
act(() => {
store.set({a: 0, b: 1});
});
// Only b re-renders
expect(Scheduler).toHaveYielded(['B1']);
expect(root).toMatchRenderedOutput('A0B1');
// Update a but not b
act(() => {
store.set({a: 1, b: 1});
});
// Only a re-renders
expect(Scheduler).toHaveYielded(['A1']);
expect(root).toMatchRenderedOutput('A1B1');
});
});
test('Infinite loop if getSnapshot keeps returning new reference', () => {
const store = createExternalStore({});
function App() {
const text = useSyncExternalStore(store.subscribe, () => ({}));
return <Text text={JSON.stringify(text)} />;
}
spyOnDev(console, 'error');
expect(() => {
act(() => {
createRoot(<App />);
});
}).toThrow(
'Maximum update depth exceeded. This can happen when a component repeatedly ' +
'calls setState inside componentWillUpdate or componentDidUpdate. React limits ' +
'the number of nested updates to prevent infinite loops.',
);
if (__DEV__) {
expect(console.error.calls.argsFor(0)[0]).toMatch(
'The result of getSnapshot should be cached to avoid an infinite loop',
);
}
});
});