forked from facebook/react
/
ReactDOMTracing-test.internal.js
715 lines (617 loc) · 23.1 KB
/
ReactDOMTracing-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
/**
* 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 React;
let ReactDOM;
let ReactDOMServer;
let ReactFeatureFlags;
let Scheduler;
let SchedulerTracing;
let TestUtils;
let onInteractionScheduledWorkCompleted;
let onInteractionTraced;
let onWorkCanceled;
let onWorkScheduled;
let onWorkStarted;
let onWorkStopped;
function loadModules() {
ReactFeatureFlags = require('shared/ReactFeatureFlags');
ReactFeatureFlags.debugRenderPhaseSideEffects = false;
ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false;
ReactFeatureFlags.enableSuspenseServerRenderer = true;
ReactFeatureFlags.enableProfilerTimer = true;
ReactFeatureFlags.enableSchedulerTracing = true;
ReactFeatureFlags.replayFailedUnitOfWorkWithInvokeGuardedCallback = false;
React = require('react');
ReactDOM = require('react-dom');
ReactDOMServer = require('react-dom/server');
Scheduler = require('scheduler');
SchedulerTracing = require('scheduler/tracing');
TestUtils = require('react-dom/test-utils');
onInteractionScheduledWorkCompleted = jest.fn();
onInteractionTraced = jest.fn();
onWorkCanceled = jest.fn();
onWorkScheduled = jest.fn();
onWorkStarted = jest.fn();
onWorkStopped = jest.fn();
// Verify interaction subscriber methods are called as expected.
SchedulerTracing.unstable_subscribe({
onInteractionScheduledWorkCompleted,
onInteractionTraced,
onWorkCanceled,
onWorkScheduled,
onWorkStarted,
onWorkStopped,
});
}
describe('ReactDOMTracing', () => {
beforeEach(() => {
jest.resetModules();
loadModules();
});
describe('interaction tracing', () => {
describe('hidden', () => {
it('traces interaction through hidden subtree', () => {
const Child = () => {
const [didMount, setDidMount] = React.useState(false);
Scheduler.yieldValue('Child');
React.useEffect(
() => {
if (didMount) {
Scheduler.yieldValue('Child:update');
} else {
Scheduler.yieldValue('Child:mount');
setDidMount(true);
}
},
[didMount],
);
return <div />;
};
const App = () => {
Scheduler.yieldValue('App');
React.useEffect(() => {
Scheduler.yieldValue('App:mount');
}, []);
return (
<div hidden={true}>
<Child />
</div>
);
};
let interaction;
const onRender = jest.fn();
const container = document.createElement('div');
const root = ReactDOM.unstable_createRoot(container);
SchedulerTracing.unstable_trace('initialization', 0, () => {
interaction = Array.from(SchedulerTracing.unstable_getCurrent())[0];
TestUtils.act(() => {
root.render(
<React.Profiler id="test" onRender={onRender}>
<App />
</React.Profiler>,
);
expect(onInteractionTraced).toHaveBeenCalledTimes(1);
expect(onInteractionTraced).toHaveBeenLastNotifiedOfInteraction(
interaction,
);
expect(Scheduler).toFlushAndYieldThrough(['App', 'App:mount']);
expect(onInteractionScheduledWorkCompleted).not.toHaveBeenCalled();
expect(onRender).toHaveBeenCalledTimes(1);
expect(onRender).toHaveLastRenderedWithInteractions(
new Set([interaction]),
);
expect(Scheduler).toFlushAndYieldThrough(['Child', 'Child:mount']);
expect(onInteractionScheduledWorkCompleted).not.toHaveBeenCalled();
expect(onRender).toHaveBeenCalledTimes(2);
expect(onRender).toHaveLastRenderedWithInteractions(
new Set([interaction]),
);
expect(Scheduler).toFlushAndYield(['Child', 'Child:update']);
});
});
expect(onInteractionScheduledWorkCompleted).toHaveBeenCalledTimes(1);
expect(
onInteractionScheduledWorkCompleted,
).toHaveBeenLastNotifiedOfInteraction(interaction);
expect(onRender).toHaveBeenCalledTimes(3);
expect(onRender).toHaveLastRenderedWithInteractions(
new Set([interaction]),
);
});
it('traces interaction through hidden subtree when there is other pending traced work', () => {
const Child = () => {
Scheduler.yieldValue('Child');
return <div />;
};
let wrapped = null;
const App = () => {
Scheduler.yieldValue('App');
React.useEffect(() => {
wrapped = SchedulerTracing.unstable_wrap(() => {});
Scheduler.yieldValue('App:mount');
}, []);
return (
<div hidden={true}>
<Child />
</div>
);
};
let interaction;
const onRender = jest.fn();
const container = document.createElement('div');
const root = ReactDOM.unstable_createRoot(container);
SchedulerTracing.unstable_trace('initialization', 0, () => {
interaction = Array.from(SchedulerTracing.unstable_getCurrent())[0];
TestUtils.act(() => {
root.render(
<React.Profiler id="test" onRender={onRender}>
<App />
</React.Profiler>,
);
expect(onInteractionTraced).toHaveBeenCalledTimes(1);
expect(onInteractionTraced).toHaveBeenLastNotifiedOfInteraction(
interaction,
);
expect(Scheduler).toFlushAndYieldThrough(['App', 'App:mount']);
expect(onInteractionScheduledWorkCompleted).not.toHaveBeenCalled();
expect(onRender).toHaveBeenCalledTimes(1);
expect(onRender).toHaveLastRenderedWithInteractions(
new Set([interaction]),
);
expect(wrapped).not.toBeNull();
expect(Scheduler).toFlushAndYield(['Child']);
expect(onInteractionScheduledWorkCompleted).not.toHaveBeenCalled();
expect(onRender).toHaveBeenCalledTimes(2);
expect(onRender).toHaveLastRenderedWithInteractions(
new Set([interaction]),
);
});
});
wrapped();
expect(onInteractionTraced).toHaveBeenCalledTimes(1);
expect(onInteractionScheduledWorkCompleted).toHaveBeenCalledTimes(1);
expect(
onInteractionScheduledWorkCompleted,
).toHaveBeenLastNotifiedOfInteraction(interaction);
});
it('traces interaction through hidden subtree that schedules more idle/never work', () => {
const Child = () => {
const [didMount, setDidMount] = React.useState(false);
Scheduler.yieldValue('Child');
React.useLayoutEffect(
() => {
if (didMount) {
Scheduler.yieldValue('Child:update');
} else {
Scheduler.yieldValue('Child:mount');
Scheduler.unstable_runWithPriority(
Scheduler.unstable_IdlePriority,
() => setDidMount(true),
);
}
},
[didMount],
);
return <div />;
};
const App = () => {
Scheduler.yieldValue('App');
React.useEffect(() => {
Scheduler.yieldValue('App:mount');
}, []);
return (
<div hidden={true}>
<Child />
</div>
);
};
let interaction;
const onRender = jest.fn();
const container = document.createElement('div');
const root = ReactDOM.unstable_createRoot(container);
SchedulerTracing.unstable_trace('initialization', 0, () => {
interaction = Array.from(SchedulerTracing.unstable_getCurrent())[0];
TestUtils.act(() => {
root.render(
<React.Profiler id="test" onRender={onRender}>
<App />
</React.Profiler>,
);
expect(onInteractionTraced).toHaveBeenCalledTimes(1);
expect(onInteractionTraced).toHaveBeenLastNotifiedOfInteraction(
interaction,
);
expect(Scheduler).toFlushAndYieldThrough(['App', 'App:mount']);
expect(onInteractionScheduledWorkCompleted).not.toHaveBeenCalled();
expect(onRender).toHaveBeenCalledTimes(1);
expect(onRender).toHaveLastRenderedWithInteractions(
new Set([interaction]),
);
expect(Scheduler).toFlushAndYieldThrough(['Child', 'Child:mount']);
expect(onInteractionScheduledWorkCompleted).not.toHaveBeenCalled();
expect(onRender).toHaveBeenCalledTimes(2);
expect(onRender).toHaveLastRenderedWithInteractions(
new Set([interaction]),
);
expect(Scheduler).toFlushAndYield(['Child', 'Child:update']);
});
});
expect(onInteractionScheduledWorkCompleted).toHaveBeenCalledTimes(1);
expect(
onInteractionScheduledWorkCompleted,
).toHaveBeenLastNotifiedOfInteraction(interaction);
expect(onRender).toHaveBeenCalledTimes(3);
expect(onRender).toHaveLastRenderedWithInteractions(
new Set([interaction]),
);
});
it('does not continue interactions across pre-existing idle work', () => {
const Child = () => {
Scheduler.yieldValue('Child');
return <div />;
};
let update = null;
const WithHiddenWork = () => {
Scheduler.yieldValue('WithHiddenWork');
return (
<div hidden={true}>
<Child />
</div>
);
};
const Updater = () => {
Scheduler.yieldValue('Updater');
React.useEffect(() => {
Scheduler.yieldValue('Updater:effect');
});
const setCount = React.useState(0)[1];
update = () => {
setCount(current => current + 1);
};
return <div />;
};
const App = () => {
Scheduler.yieldValue('App');
React.useEffect(() => {
Scheduler.yieldValue('App:effect');
});
return (
<React.Fragment>
<WithHiddenWork />
<Updater />
</React.Fragment>
);
};
const onRender = jest.fn();
const container = document.createElement('div');
const root = ReactDOM.unstable_createRoot(container);
// Schedule some idle work without any interactions.
TestUtils.act(() => {
root.render(
<React.Profiler id="test" onRender={onRender}>
<App />
</React.Profiler>,
);
expect(Scheduler).toFlushAndYieldThrough([
'App',
'WithHiddenWork',
'Updater',
'Updater:effect',
'App:effect',
]);
expect(update).not.toBeNull();
// Trace a higher-priority update.
let interaction = null;
SchedulerTracing.unstable_trace('update', 0, () => {
interaction = Array.from(SchedulerTracing.unstable_getCurrent())[0];
update();
});
expect(interaction).not.toBeNull();
expect(onRender).toHaveBeenCalledTimes(1);
expect(onInteractionTraced).toHaveBeenCalledTimes(1);
expect(onInteractionTraced).toHaveBeenLastNotifiedOfInteraction(
interaction,
);
// Ensure the traced interaction completes without being attributed to the pre-existing idle work.
expect(Scheduler).toFlushAndYieldThrough([
'Updater',
'Updater:effect',
]);
expect(onInteractionScheduledWorkCompleted).toHaveBeenCalledTimes(1);
expect(
onInteractionScheduledWorkCompleted,
).toHaveBeenLastNotifiedOfInteraction(interaction);
expect(onRender).toHaveBeenCalledTimes(2);
expect(onRender).toHaveLastRenderedWithInteractions(
new Set([interaction]),
);
// Complete low-priority work and ensure no lingering interaction.
expect(Scheduler).toFlushAndYield(['Child']);
expect(onInteractionScheduledWorkCompleted).toHaveBeenCalledTimes(1);
expect(onRender).toHaveBeenCalledTimes(3);
expect(onRender).toHaveLastRenderedWithInteractions(new Set([]));
});
});
it('should properly trace interactions when there is work of interleaved priorities', () => {
const Child = () => {
Scheduler.yieldValue('Child');
return <div />;
};
let scheduleUpdate = null;
let scheduleUpdateWithHidden = null;
const MaybeHiddenWork = () => {
const [flag, setFlag] = React.useState(false);
scheduleUpdateWithHidden = () => setFlag(true);
Scheduler.yieldValue('MaybeHiddenWork');
React.useEffect(() => {
Scheduler.yieldValue('MaybeHiddenWork:effect');
});
return flag ? (
<div hidden={true}>
<Child />
</div>
) : null;
};
const Updater = () => {
Scheduler.yieldValue('Updater');
React.useEffect(() => {
Scheduler.yieldValue('Updater:effect');
});
const setCount = React.useState(0)[1];
scheduleUpdate = () => setCount(current => current + 1);
return <div />;
};
const App = () => {
Scheduler.yieldValue('App');
React.useEffect(() => {
Scheduler.yieldValue('App:effect');
});
return (
<React.Fragment>
<MaybeHiddenWork />
<Updater />
</React.Fragment>
);
};
const onRender = jest.fn();
const container = document.createElement('div');
const root = ReactDOM.unstable_createRoot(container);
TestUtils.act(() => {
root.render(
<React.Profiler id="test" onRender={onRender}>
<App />
</React.Profiler>,
);
expect(Scheduler).toFlushAndYield([
'App',
'MaybeHiddenWork',
'Updater',
'MaybeHiddenWork:effect',
'Updater:effect',
'App:effect',
]);
expect(scheduleUpdate).not.toBeNull();
expect(scheduleUpdateWithHidden).not.toBeNull();
expect(onRender).toHaveBeenCalledTimes(1);
// schedule traced high-pri update and a (non-traced) low-pri update.
let interaction = null;
SchedulerTracing.unstable_trace('update', 0, () => {
interaction = Array.from(SchedulerTracing.unstable_getCurrent())[0];
Scheduler.unstable_runWithPriority(
Scheduler.unstable_UserBlockingPriority,
() => scheduleUpdateWithHidden(),
);
});
scheduleUpdate();
expect(interaction).not.toBeNull();
expect(onRender).toHaveBeenCalledTimes(1);
expect(onInteractionTraced).toHaveBeenCalledTimes(1);
expect(onInteractionTraced).toHaveBeenLastNotifiedOfInteraction(
interaction,
);
// high-pri update should leave behind idle work and should not complete the interaction
expect(Scheduler).toFlushAndYieldThrough([
'MaybeHiddenWork',
'MaybeHiddenWork:effect',
]);
expect(onInteractionScheduledWorkCompleted).not.toHaveBeenCalled();
expect(onRender).toHaveBeenCalledTimes(2);
expect(onRender).toHaveLastRenderedWithInteractions(
new Set([interaction]),
);
// low-pri update should not have the interaction
expect(Scheduler).toFlushAndYieldThrough([
'Updater',
'Updater:effect',
]);
expect(onInteractionScheduledWorkCompleted).not.toHaveBeenCalled();
expect(onRender).toHaveBeenCalledTimes(3);
expect(onRender).toHaveLastRenderedWithInteractions(new Set([]));
// idle work should complete the interaction
expect(Scheduler).toFlushAndYield(['Child']);
expect(onInteractionScheduledWorkCompleted).toHaveBeenCalledTimes(1);
expect(
onInteractionScheduledWorkCompleted,
).toHaveBeenLastNotifiedOfInteraction(interaction);
expect(onRender).toHaveBeenCalledTimes(4);
expect(onRender).toHaveLastRenderedWithInteractions(
new Set([interaction]),
);
});
});
it('should properly trace interactions through a multi-pass SuspenseList render', () => {
const SuspenseList = React.unstable_SuspenseList;
const Suspense = React.Suspense;
function Text({text}) {
Scheduler.yieldValue(text);
React.useEffect(() => {
Scheduler.yieldValue('Commit ' + text);
});
return <span>{text}</span>;
}
function App() {
return (
<SuspenseList revealOrder="forwards">
<Suspense fallback={<Text text="Loading A" />}>
<Text text="A" />
</Suspense>
<Suspense fallback={<Text text="Loading B" />}>
<Text text="B" />
</Suspense>
<Suspense fallback={<Text text="Loading C" />}>
<Text text="C" />
</Suspense>
</SuspenseList>
);
}
const container = document.createElement('div');
const root = ReactDOM.unstable_createRoot(container);
let interaction;
TestUtils.act(() => {
SchedulerTracing.unstable_trace('initialization', 0, () => {
interaction = Array.from(SchedulerTracing.unstable_getCurrent())[0];
// This render is only CPU bound. Nothing suspends.
root.render(<App />);
});
expect(Scheduler).toFlushAndYieldThrough(['A']);
Scheduler.advanceTime(300);
jest.advanceTimersByTime(300);
expect(Scheduler).toFlushAndYieldThrough(['B']);
Scheduler.advanceTime(300);
jest.advanceTimersByTime(300);
// Time has now elapsed for so long that we're just going to give up
// rendering the rest of the content. So that we can at least show
// something.
expect(Scheduler).toFlushAndYieldThrough([
'Loading C',
'Commit A',
'Commit B',
'Commit Loading C',
]);
// Schedule an unrelated low priority update that shouldn't be included
// in the previous interaction. This is meant to ensure that we don't
// rely on the whole tree completing to cover up bugs.
Scheduler.unstable_runWithPriority(
Scheduler.unstable_IdlePriority,
() => root.render(<App />),
);
expect(onInteractionTraced).toHaveBeenCalledTimes(1);
expect(onInteractionTraced).toHaveBeenLastNotifiedOfInteraction(
interaction,
);
expect(onInteractionScheduledWorkCompleted).not.toHaveBeenCalled();
// Then we do a second pass to commit the last item.
expect(Scheduler).toFlushAndYieldThrough(['C', 'Commit C']);
expect(onInteractionScheduledWorkCompleted).toHaveBeenCalledTimes(1);
expect(
onInteractionScheduledWorkCompleted,
).toHaveBeenLastNotifiedOfInteraction(interaction);
});
});
});
describe('hydration', () => {
it('traces interaction across hydration', async done => {
let ref = React.createRef();
function Child() {
return 'Hello';
}
function App() {
return (
<div>
<span ref={ref}>
<Child />
</span>
</div>
);
}
// Render the final HTML.
const finalHTML = ReactDOMServer.renderToString(<App />);
const container = document.createElement('div');
container.innerHTML = finalHTML;
let interaction;
const root = ReactDOM.unstable_createRoot(container, {hydrate: true});
// Hydrate it.
SchedulerTracing.unstable_trace('initialization', 0, () => {
interaction = Array.from(SchedulerTracing.unstable_getCurrent())[0];
root.render(<App />);
});
Scheduler.flushAll();
jest.runAllTimers();
expect(ref.current).not.toBe(null);
expect(onInteractionTraced).toHaveBeenCalledTimes(1);
expect(onInteractionTraced).toHaveBeenLastNotifiedOfInteraction(
interaction,
);
expect(onInteractionScheduledWorkCompleted).toHaveBeenCalledTimes(1);
expect(
onInteractionScheduledWorkCompleted,
).toHaveBeenLastNotifiedOfInteraction(interaction);
done();
});
it('traces interaction across suspended hydration', async done => {
let suspend = false;
let resolve;
let promise = new Promise(resolvePromise => (resolve = resolvePromise));
let ref = React.createRef();
function Child() {
if (suspend) {
throw promise;
} else {
return 'Hello';
}
}
function App() {
return (
<div>
<React.Suspense fallback="Loading...">
<span ref={ref}>
<Child />
</span>
</React.Suspense>
</div>
);
}
// Render the final HTML.
// Don't suspend on the server.
const finalHTML = ReactDOMServer.renderToString(<App />);
const container = document.createElement('div');
container.innerHTML = finalHTML;
let interaction;
const root = ReactDOM.unstable_createRoot(container, {hydrate: true});
// Start hydrating but simulate blocking for suspense data.
suspend = true;
SchedulerTracing.unstable_trace('initialization', 0, () => {
interaction = Array.from(SchedulerTracing.unstable_getCurrent())[0];
root.render(<App />);
});
Scheduler.flushAll();
jest.runAllTimers();
expect(ref.current).toBe(null);
expect(onInteractionTraced).toHaveBeenCalledTimes(1);
expect(onInteractionTraced).toHaveBeenLastNotifiedOfInteraction(
interaction,
);
expect(onInteractionScheduledWorkCompleted).not.toHaveBeenCalled();
// Resolving the promise should continue hydration
suspend = false;
resolve();
await promise;
Scheduler.flushAll();
jest.runAllTimers();
expect(ref.current).not.toBe(null);
expect(onInteractionScheduledWorkCompleted).toHaveBeenCalledTimes(1);
expect(
onInteractionScheduledWorkCompleted,
).toHaveBeenLastNotifiedOfInteraction(interaction);
done();
});
});
});
});