forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReactInternalTestUtilsDOM-test.js
566 lines (495 loc) · 16 KB
/
ReactInternalTestUtilsDOM-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
/**
* Copyright (c) Meta Platforms, Inc. and 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 act;
let Scheduler;
let ReactDOMClient;
let simulateEventDispatch;
let assertLog;
describe('ReactInternalTestUtilsDOM', () => {
beforeEach(() => {
jest.resetModules();
act = require('internal-test-utils').act;
simulateEventDispatch =
require('internal-test-utils').simulateEventDispatch;
Scheduler = require('scheduler/unstable_mock');
ReactDOMClient = require('react-dom/client');
React = require('react');
assertLog = require('internal-test-utils').assertLog;
});
describe('simulateEventDispatch', () => {
it('should batch discrete capture events', async () => {
let childRef;
function Component() {
const [state, setState] = React.useState(0);
Scheduler.log(`Render ${state}`);
return (
<div
onClickCapture={() => {
queueMicrotask(() => {
Scheduler.log('Parent microtask');
});
setState(1);
Scheduler.log('onClickCapture parent');
}}>
<button
ref={ref => (childRef = ref)}
onClickCapture={() => {
queueMicrotask(() => {
Scheduler.log('Child microtask');
});
setState(2);
Scheduler.log('onClickCapture child');
}}
/>
</div>
);
}
const container = document.createElement('div');
document.body.appendChild(container);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<Component />);
});
assertLog(['Render 0']);
await act(async () => {
await simulateEventDispatch(childRef, 'click');
});
// Capture runs on every event we dispatch,
// which means we get two for the parent, and one for the child.
assertLog([
'onClickCapture parent',
'onClickCapture child',
'Parent microtask',
'Render 2',
'Child microtask',
]);
document.body.removeChild(container);
});
it('should batch continuous capture events', async () => {
let childRef;
function Component() {
const [state, setState] = React.useState(0);
Scheduler.log(`Render ${state}`);
return (
<div
onMouseOutCapture={() => {
queueMicrotask(() => {
Scheduler.log('Parent microtask');
});
setState(1);
Scheduler.log('onMouseOutCapture parent');
}}>
<button
ref={ref => (childRef = ref)}
onMouseOutCapture={() => {
queueMicrotask(() => {
Scheduler.log('Child microtask');
});
setState(2);
Scheduler.log('onMouseOutCapture child');
}}
/>
</div>
);
}
const container = document.createElement('div');
document.body.appendChild(container);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<Component />);
});
assertLog(['Render 0']);
await act(async () => {
await simulateEventDispatch(childRef, 'mouseout');
});
assertLog([
'onMouseOutCapture parent',
'onMouseOutCapture child',
'Parent microtask',
'Child microtask',
'Render 2',
]);
});
it('should batch bubbling discrete events', async () => {
let childRef;
function Component() {
const [state, setState] = React.useState(0);
Scheduler.log(`Render ${state}`);
return (
<div
onClick={() => {
queueMicrotask(() => {
Scheduler.log('Parent microtask');
});
setState(1);
Scheduler.log('onClick parent');
}}>
<button
ref={ref => (childRef = ref)}
onClick={() => {
queueMicrotask(() => {
Scheduler.log('Child microtask');
});
setState(2);
Scheduler.log('onClick child');
}}
/>
</div>
);
}
const container = document.createElement('div');
document.body.appendChild(container);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<Component />);
});
assertLog(['Render 0']);
await act(async () => {
await simulateEventDispatch(childRef, 'click');
});
assertLog([
'onClick child',
'onClick parent',
'Child microtask',
'Render 1',
'Parent microtask',
]);
});
it('should batch bubbling continuous events', async () => {
let childRef;
function Component() {
const [state, setState] = React.useState(0);
Scheduler.log(`Render ${state}`);
return (
<div
onMouseOut={() => {
queueMicrotask(() => {
Scheduler.log('Parent microtask');
});
setState(1);
Scheduler.log('onMouseOut parent');
}}>
<button
ref={ref => (childRef = ref)}
onMouseOut={() => {
queueMicrotask(() => {
Scheduler.log('Child microtask');
});
setState(2);
Scheduler.log('onMouseOut child');
}}
/>
</div>
);
}
const container = document.createElement('div');
document.body.appendChild(container);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<Component />);
});
assertLog(['Render 0']);
await act(async () => {
await simulateEventDispatch(childRef, 'mouseout');
});
assertLog([
'onMouseOut child',
'onMouseOut parent',
'Child microtask',
'Parent microtask',
'Render 1',
]);
});
it('does not batch discrete events between handlers', async () => {
let childRef = React.createRef();
function Component() {
const [state, setState] = React.useState(0);
const parentRef = React.useRef();
React.useEffect(() => {
function handleParentEvent() {
queueMicrotask(() => {
Scheduler.log('Parent microtask');
});
setState(2);
Scheduler.log(`Click parent`);
}
function handleChildEvent() {
queueMicrotask(() => {
Scheduler.log('Child microtask');
});
setState(1);
Scheduler.log(`Click child`);
}
parentRef.current.addEventListener('click', handleParentEvent);
childRef.current.addEventListener('click', handleChildEvent);
return () => {
parentRef.current.removeEventListener('click', handleParentEvent);
childRef.current.removeEventListener('click', handleChildEvent);
};
});
Scheduler.log(`Render ${state}`);
return (
<div ref={parentRef}>
<button ref={childRef} />
</div>
);
}
const container = document.createElement('div');
document.body.appendChild(container);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<Component />);
});
assertLog(['Render 0']);
await act(async () => {
await simulateEventDispatch(childRef.current, 'click');
});
assertLog([
'Click child',
'Child microtask',
'Render 1',
'Click parent',
'Parent microtask',
'Render 2',
]);
});
it('should batch continuous events between handlers', async () => {
let childRef = React.createRef();
function Component() {
const [state, setState] = React.useState(0);
const parentRef = React.useRef();
React.useEffect(() => {
function handleChildEvent() {
queueMicrotask(() => {
Scheduler.log('Child microtask');
});
setState(1);
Scheduler.log(`Mouseout child`);
}
function handleParentEvent() {
queueMicrotask(() => {
Scheduler.log('Parent microtask');
});
setState(2);
Scheduler.log(`Mouseout parent`);
}
parentRef.current.addEventListener('mouseout', handleParentEvent);
childRef.current.addEventListener('mouseout', handleChildEvent);
return () => {
parentRef.current.removeEventListener(
'mouseout',
handleParentEvent
);
childRef.current.removeEventListener('mouseout', handleChildEvent);
};
});
Scheduler.log(`Render ${state}`);
return (
<div ref={parentRef}>
<button ref={childRef} />
</div>
);
}
const container = document.createElement('div');
document.body.appendChild(container);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<Component />);
});
assertLog(['Render 0']);
await act(async () => {
await simulateEventDispatch(childRef.current, 'mouseout');
});
assertLog([
'Mouseout child',
'Child microtask',
'Mouseout parent',
'Parent microtask',
'Render 2',
]);
});
it('should flush discrete events between handlers from different roots', async () => {
const childContainer = document.createElement('div');
const parentContainer = document.createElement('main');
const childRoot = ReactDOMClient.createRoot(childContainer);
const parentRoot = ReactDOMClient.createRoot(parentContainer);
let childSetState;
function Parent() {
// eslint-disable-next-line no-unused-vars
const [state, _] = React.useState('Parent');
const handleClick = () => {
Promise.resolve().then(() => Scheduler.log('Flush Parent microtask'));
childSetState(2);
Scheduler.log('Parent click');
};
return <section onClick={handleClick}>{state}</section>;
}
function Child() {
const [state, setState] = React.useState('Child');
childSetState = setState;
const handleClick = () => {
Promise.resolve().then(() => Scheduler.log('Flush Child microtask'));
setState(1);
Scheduler.log('Child click');
};
Scheduler.log('Render ' + state);
return <span onClick={handleClick}>{state}</span>;
}
await act(() => {
childRoot.render(<Child />);
parentRoot.render(<Parent />);
});
const childNode = childContainer.firstChild;
const parentNode = parentContainer.firstChild;
parentNode.appendChild(childContainer);
document.body.appendChild(parentContainer);
assertLog(['Render Child']);
try {
await act(async () => {
await simulateEventDispatch(childNode, 'click');
});
// Since discrete events flush in a microtasks, they flush before
// the handler for the other root is called, after the microtask
// scheduled in the event fires.
assertLog([
'Child click',
'Flush Child microtask',
'Render 1',
'Parent click',
'Flush Parent microtask',
'Render 2',
]);
} finally {
document.body.removeChild(parentContainer);
}
});
it('should batch continuous events between handlers from different roots', async () => {
const childContainer = document.createElement('div');
const parentContainer = document.createElement('main');
const childRoot = ReactDOMClient.createRoot(childContainer);
const parentRoot = ReactDOMClient.createRoot(parentContainer);
let childSetState;
function Parent() {
// eslint-disable-next-line no-unused-vars
const [state, _] = React.useState('Parent');
const handleMouseOut = () => {
Promise.resolve().then(() => Scheduler.log('Flush Parent microtask'));
childSetState(2);
Scheduler.log('Parent mouseout');
};
return <section onMouseOut={handleMouseOut}>{state}</section>;
}
function Child() {
const [state, setState] = React.useState('Child');
childSetState = setState;
const handleMouseOut = () => {
Promise.resolve().then(() => Scheduler.log('Flush Child microtask'));
setState(1);
Scheduler.log('Child mouseout');
};
Scheduler.log('Render ' + state);
return <span onMouseOut={handleMouseOut}>{state}</span>;
}
await act(() => {
childRoot.render(<Child />);
parentRoot.render(<Parent />);
});
const childNode = childContainer.firstChild;
const parentNode = parentContainer.firstChild;
parentNode.appendChild(childContainer);
document.body.appendChild(parentContainer);
assertLog(['Render Child']);
try {
await act(async () => {
await simulateEventDispatch(childNode, 'mouseout');
});
// Since continuous events flush in a macrotask, they are batched after
// with the handler for the other root, but the microtasks scheduled
// in the event handlers still fire in between.
assertLog([
'Child mouseout',
'Flush Child microtask',
'Parent mouseout',
'Flush Parent microtask',
'Render 2',
]);
} finally {
document.body.removeChild(parentContainer);
}
});
it('should fire on nodes removed while dispatching', async () => {
let childRef;
function Component() {
const parentRef = React.useRef();
const middleRef = React.useRef();
Scheduler.log(`Render`);
return (
<div
ref={parentRef}
onClick={() => {
Scheduler.log('onMouseOut parent');
}}>
<div ref={middleRef}>
<button
ref={ref => (childRef = ref)}
onClick={() => {
Scheduler.log('onMouseOut child');
childRef.parentNode.remove();
}}
/>
</div>
</div>
);
}
const container = document.createElement('div');
document.body.appendChild(container);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<Component />);
});
assertLog(['Render']);
await act(async () => {
await simulateEventDispatch(childRef, 'click');
});
assertLog(['onMouseOut child', 'onMouseOut parent']);
});
it('should not fire if node is not in the document', async () => {
let childRef;
function Component() {
Scheduler.log(`Render`);
return (
<div
onMouseOut={() => {
Scheduler.log('onMouseOut parent');
}}>
<button
ref={ref => (childRef = ref)}
onMouseOut={() => {
Scheduler.log('onMouseOut child');
}}
/>
</div>
);
}
// Do not attach root to document.
const root = ReactDOMClient.createRoot(document.createElement('div'));
await act(() => {
root.render(<Component />);
});
assertLog(['Render']);
await act(async () => {
await simulateEventDispatch(childRef, 'mouseout');
});
// No events flushed, root not in document.
assertLog([]);
});
});
});