-
Notifications
You must be signed in to change notification settings - Fork 51k
Expand file tree
/
Copy pathReactDOM-test.js
More file actions
1023 lines (915 loc) · 33.8 KB
/
ReactDOM-test.js
File metadata and controls
1023 lines (915 loc) · 33.8 KB
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) 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 ReactDOM;
let findDOMNode;
let ReactDOMClient;
let ReactDOMServer;
let assertConsoleErrorDev;
let act;
describe('ReactDOM', () => {
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
ReactDOMServer = require('react-dom/server');
findDOMNode =
ReactDOM.__DOM_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE
.findDOMNode;
({act, assertConsoleErrorDev} = require('internal-test-utils'));
});
it('should bubble onSubmit', async () => {
const container = document.createElement('div');
let count = 0;
let buttonRef;
function Parent() {
return (
<div
onSubmit={event => {
event.preventDefault();
count++;
}}>
<Child />
</div>
);
}
function Child() {
return (
<form>
<input type="submit" ref={button => (buttonRef = button)} />
</form>
);
}
document.body.appendChild(container);
const root = ReactDOMClient.createRoot(container);
try {
await act(() => {
root.render(<Parent />);
});
buttonRef.click();
expect(count).toBe(1);
} finally {
document.body.removeChild(container);
}
});
it('allows a DOM element to be used with a string', async () => {
const element = React.createElement('div', {className: 'foo'});
const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(element);
});
const node = container.firstChild;
expect(node.tagName).toBe('DIV');
});
it('should allow children to be passed as an argument', async () => {
const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(React.createElement('div', null, 'child'));
});
const argNode = container.firstChild;
expect(argNode.innerHTML).toBe('child');
});
it('should overwrite props.children with children argument', async () => {
const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(React.createElement('div', {children: 'fakechild'}, 'child'));
});
const conflictNode = container.firstChild;
expect(conflictNode.innerHTML).toBe('child');
});
/**
* We need to make sure that updates occur to the actual node that's in the
* DOM, instead of a stale cache.
*/
it('should purge the DOM cache when removing nodes', async () => {
let container = document.createElement('div');
let root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<div>
<div key="theDog" className="dog" />,
<div key="theBird" className="bird" />
</div>,
);
});
// Warm the cache with theDog
container = document.createElement('div');
root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<div>
<div key="theDog" className="dogbeforedelete" />,
<div key="theBird" className="bird" />,
</div>,
);
});
// Remove theDog - this should purge the cache
container = document.createElement('div');
root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<div>
<div key="theBird" className="bird" />,
</div>,
);
});
// Now, put theDog back. It's now a different DOM node.
container = document.createElement('div');
root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<div>
<div key="theDog" className="dog" />,
<div key="theBird" className="bird" />,
</div>,
);
});
// Change the className of theDog. It will use the same element
container = document.createElement('div');
root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<div>
<div key="theDog" className="bigdog" />,
<div key="theBird" className="bird" />,
</div>,
);
});
const myDiv = container.firstChild;
const dog = myDiv.childNodes[0];
expect(dog.className).toBe('bigdog');
});
// @gate !disableLegacyMode
it('throws in render() if the mount callback in legacy roots is not a function', async () => {
spyOnDev(console, 'warn');
spyOnDev(console, 'error');
function Foo() {
this.a = 1;
this.b = 2;
}
class A extends React.Component {
state = {};
render() {
return <div />;
}
}
const myDiv = document.createElement('div');
await expect(async () => {
await act(() => {
ReactDOM.render(<A />, myDiv, 'no');
});
}).rejects.toThrowError(
'Invalid argument passed as callback. Expected a function. Instead ' +
'received: no',
);
assertConsoleErrorDev([
'Expected the last optional `callback` argument to be a function. Instead received: no.',
'Expected the last optional `callback` argument to be a function. Instead received: no.',
]);
await expect(async () => {
await act(() => {
ReactDOM.render(<A />, myDiv, {foo: 'bar'});
});
}).rejects.toThrowError(
'Invalid argument passed as callback. Expected a function. Instead ' +
'received: [object Object]',
);
assertConsoleErrorDev([
"Expected the last optional `callback` argument to be a function. Instead received: { foo: 'bar' }",
"Expected the last optional `callback` argument to be a function. Instead received: { foo: 'bar' }.",
]);
await expect(async () => {
await act(() => {
ReactDOM.render(<A />, myDiv, new Foo());
});
}).rejects.toThrowError(
'Invalid argument passed as callback. Expected a function. Instead ' +
'received: [object Object]',
);
assertConsoleErrorDev([
'Expected the last optional `callback` argument to be a function. Instead received: Foo { a: 1, b: 2 }.',
'Expected the last optional `callback` argument to be a function. Instead received: Foo { a: 1, b: 2 }.',
]);
});
// @gate !disableLegacyMode
it('throws in render() if the update callback in legacy roots is not a function', async () => {
function Foo() {
this.a = 1;
this.b = 2;
}
class A extends React.Component {
state = {};
render() {
return <div />;
}
}
const myDiv = document.createElement('div');
ReactDOM.render(<A />, myDiv);
await expect(async () => {
await act(() => {
ReactDOM.render(<A />, myDiv, 'no');
});
}).rejects.toThrowError(
'Invalid argument passed as callback. Expected a function. Instead ' +
'received: no',
);
assertConsoleErrorDev([
'Expected the last optional `callback` argument to be a function. Instead received: no.',
'Expected the last optional `callback` argument to be a function. Instead received: no.',
]);
ReactDOM.render(<A />, myDiv); // Re-mount
await expect(async () => {
await act(() => {
ReactDOM.render(<A />, myDiv, {foo: 'bar'});
});
}).rejects.toThrowError(
'Invalid argument passed as callback. Expected a function. Instead ' +
'received: [object Object]',
);
assertConsoleErrorDev([
"Expected the last optional `callback` argument to be a function. Instead received: { foo: 'bar' }.",
"Expected the last optional `callback` argument to be a function. Instead received: { foo: 'bar' }.",
]);
ReactDOM.render(<A />, myDiv); // Re-mount
await expect(async () => {
await act(() => {
ReactDOM.render(<A />, myDiv, new Foo());
});
}).rejects.toThrowError(
'Invalid argument passed as callback. Expected a function. Instead ' +
'received: [object Object]',
);
assertConsoleErrorDev([
'Expected the last optional `callback` argument to be a function. Instead received: Foo { a: 1, b: 2 }.',
'Expected the last optional `callback` argument to be a function. Instead received: Foo { a: 1, b: 2 }.',
]);
});
it('preserves focus', async () => {
let input;
let input2;
class A extends React.Component {
render() {
return (
<div>
<input id="one" ref={r => (input = input || r)} />
{this.props.showTwo && (
<input id="two" ref={r => (input2 = input2 || r)} />
)}
</div>
);
}
componentDidUpdate() {
// Focus should have been restored to the original input
expect(document.activeElement.id).toBe('one');
input2.focus();
expect(document.activeElement.id).toBe('two');
log.push('input2 focused');
}
}
const log = [];
const container = document.createElement('div');
document.body.appendChild(container);
const root = ReactDOMClient.createRoot(container);
try {
await act(() => {
root.render(<A showTwo={false} />);
});
input.focus();
// When the second input is added, let's simulate losing focus, which is
// something that could happen when manipulating DOM nodes (but is hard to
// deterministically force without relying intensely on React DOM
// implementation details)
const div = container.firstChild;
['appendChild', 'insertBefore'].forEach(name => {
const mutator = div[name];
div[name] = function () {
if (input) {
input.blur();
expect(document.activeElement.tagName).toBe('BODY');
log.push('input2 inserted');
}
return mutator.apply(this, arguments);
};
});
expect(document.activeElement.id).toBe('one');
await act(() => {
root.render(<A showTwo={true} />);
});
// input2 gets added, which causes input to get blurred. Then
// componentDidUpdate focuses input2 and that should make it down to here,
// not get overwritten by focus restoration.
expect(document.activeElement.id).toBe('two');
expect(log).toEqual(['input2 inserted', 'input2 focused']);
} finally {
document.body.removeChild(container);
}
});
it('calls focus() on autoFocus elements after they have been mounted to the DOM', async () => {
const originalFocus = HTMLElement.prototype.focus;
try {
let focusedElement;
let inputFocusedAfterMount = false;
// This test needs to determine that focus is called after mount.
// Can't check document.activeElement because PhantomJS is too permissive;
// It doesn't require element to be in the DOM to be focused.
HTMLElement.prototype.focus = function () {
focusedElement = this;
inputFocusedAfterMount = !!this.parentNode;
};
const container = document.createElement('div');
document.body.appendChild(container);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<div>
<h1>Auto-focus Test</h1>
<input autoFocus={true} />
<p>The above input should be focused after mount.</p>
</div>,
);
});
expect(inputFocusedAfterMount).toBe(true);
expect(focusedElement.tagName).toBe('INPUT');
} finally {
HTMLElement.prototype.focus = originalFocus;
}
});
it('calls focus() on autoFocus anchor elements after they have been mounted to the DOM', async () => {
const originalFocus = HTMLElement.prototype.focus;
try {
let focusedElement;
let anchorFocusedAfterMount = false;
HTMLElement.prototype.focus = function () {
focusedElement = this;
anchorFocusedAfterMount = !!this.parentNode;
};
const container = document.createElement('div');
document.body.appendChild(container);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<div>
<h1>Auto-focus Test</h1>
<a href="https://react.dev" autoFocus={true}>
Link
</a>
<p>The above anchor should be focused after mount.</p>
</div>,
);
});
expect(anchorFocusedAfterMount).toBe(true);
expect(focusedElement.tagName).toBe('A');
} finally {
HTMLElement.prototype.focus = originalFocus;
}
});
it('calls focus() on autoFocus for any focusable element after mount', async () => {
const originalFocus = HTMLElement.prototype.focus;
try {
let focusedElement;
let elementFocusedAfterMount = false;
HTMLElement.prototype.focus = function () {
focusedElement = this;
elementFocusedAfterMount = !!this.parentNode;
};
const container = document.createElement('div');
document.body.appendChild(container);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<div>
<div tabIndex={0} autoFocus={true} id="focusable-div">
Focusable div
</div>
</div>,
);
});
expect(elementFocusedAfterMount).toBe(true);
expect(focusedElement.tagName).toBe('DIV');
expect(focusedElement.id).toBe('focusable-div');
} finally {
HTMLElement.prototype.focus = originalFocus;
}
});
it("shouldn't fire duplicate event handler while handling other nested dispatch", async () => {
const actual = [];
class Wrapper extends React.Component {
componentDidMount() {
this.ref1.click();
}
render() {
return (
<div>
<div
onClick={() => {
actual.push('1st node clicked');
this.ref2.click();
}}
ref={ref => (this.ref1 = ref)}
/>
<div
onClick={ref => {
actual.push("2nd node clicked imperatively from 1st's handler");
}}
ref={ref => (this.ref2 = ref)}
/>
</div>
);
}
}
const container = document.createElement('div');
document.body.appendChild(container);
const root = ReactDOMClient.createRoot(container);
try {
await act(() => {
root.render(<Wrapper />);
});
const expected = [
'1st node clicked',
"2nd node clicked imperatively from 1st's handler",
];
expect(actual).toEqual(expected);
} finally {
document.body.removeChild(container);
}
});
it('should not crash with devtools installed', async () => {
try {
global.__REACT_DEVTOOLS_GLOBAL_HOOK__ = {
inject: function () {},
onCommitFiberRoot: function () {},
onCommitFiberUnmount: function () {},
supportsFiber: true,
};
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
class Component extends React.Component {
render() {
return <div />;
}
}
const root = ReactDOMClient.createRoot(document.createElement('div'));
await act(() => {
root.render(<Component />);
});
} finally {
delete global.__REACT_DEVTOOLS_GLOBAL_HOOK__;
}
});
it('should not crash calling findDOMNode inside a function component', async () => {
class Component extends React.Component {
render() {
return <div />;
}
}
const container = document.createElement('div');
let root = ReactDOMClient.createRoot(container);
let instance;
await act(() => {
root.render(<Component ref={current => (instance = current)} />);
});
const App = () => {
findDOMNode(instance);
return <div />;
};
if (__DEV__) {
root = ReactDOMClient.createRoot(document.createElement('div'));
await act(() => {
root.render(<App />);
});
}
});
it('reports stacks with re-entrant renderToString() calls on the client', async () => {
function Child2(props) {
return <span ariaTypo3="no">{props.children}</span>;
}
function App2() {
return (
<Child2>
{ReactDOMServer.renderToString(<blink ariaTypo2="no" />)}
</Child2>
);
}
function Child() {
return (
<span ariaTypo4="no">{ReactDOMServer.renderToString(<App2 />)}</span>
);
}
function ServerEntry() {
return ReactDOMServer.renderToString(<Child />);
}
function App() {
return (
<div>
<span ariaTypo="no" />
<ServerEntry />
<font ariaTypo5="no" />
</div>
);
}
const root = ReactDOMClient.createRoot(document.createElement('div'));
await act(() => {
root.render(<App />);
});
assertConsoleErrorDev([
// ReactDOM(App > div > span)
'Invalid ARIA attribute `ariaTypo`. ARIA attributes follow the pattern aria-* and must be lowercase.\n' +
' in span (at **)\n' +
' in App (at **)',
// ReactDOM(App > div > ServerEntry) >>> ReactDOMServer(Child) >>> ReactDOMServer(App2) >>> ReactDOMServer(blink)
'Invalid ARIA attribute `ariaTypo2`. ARIA attributes follow the pattern aria-* and must be lowercase.\n' +
' in blink (at **)\n' +
' in App2 (at **)\n' +
' in Child (at **)\n' +
' in ServerEntry (at **)',
// ReactDOM(App > div > ServerEntry) >>> ReactDOMServer(Child) >>> ReactDOMServer(App2 > Child2 > span)
'Invalid ARIA attribute `ariaTypo3`. ARIA attributes follow the pattern aria-* and must be lowercase.\n' +
' in span (at **)\n' +
' in Child2 (at **)\n' +
' in App2 (at **)\n' +
' in Child (at **)\n' +
' in ServerEntry (at **)',
// ReactDOM(App > div > ServerEntry) >>> ReactDOMServer(Child > span)
'Invalid ARIA attribute `ariaTypo4`. ARIA attributes follow the pattern aria-* and must be lowercase.\n' +
' in span (at **)\n' +
' in Child (at **)\n' +
' in ServerEntry (at **)',
// ReactDOM(App > div > font)
'Invalid ARIA attribute `ariaTypo5`. ARIA attributes follow the pattern aria-* and must be lowercase.\n' +
' in font (at **)\n' +
' in App (at **)',
]);
});
it('should render root host components into body scope when the container is a Document', async () => {
function App({phase}) {
return (
<>
{phase < 1 ? null : <div>..before</div>}
{phase < 3 ? <div>before</div> : null}
{phase < 2 ? null : <div>before..</div>}
<html lang="en">
<head data-h="">
{phase < 1 ? null : <meta itemProp="" content="..head" />}
{phase < 3 ? <meta itemProp="" content="head" /> : null}
{phase < 2 ? null : <meta itemProp="" content="head.." />}
</head>
<body data-b="">
{phase < 1 ? null : <div>..inside</div>}
{phase < 3 ? <div>inside</div> : null}
{phase < 2 ? null : <div>inside..</div>}
</body>
</html>
{phase < 1 ? null : <div>..after</div>}
{phase < 3 ? <div>after</div> : null}
{phase < 2 ? null : <div>after..</div>}
</>
);
}
const root = ReactDOMClient.createRoot(document);
await act(() => {
root.render(<App phase={0} />);
});
expect(document.documentElement.outerHTML).toBe(
'<html lang="en"><head data-h=""><meta itemprop="" content="head"></head><body data-b=""><div>before</div><div>inside</div><div>after</div></body></html>',
);
await act(() => {
root.render(<App phase={1} />);
});
expect(document.documentElement.outerHTML).toBe(
'<html lang="en"><head data-h=""><meta itemprop="" content="..head"><meta itemprop="" content="head"></head><body data-b=""><div>..before</div><div>before</div><div>..inside</div><div>inside</div><div>..after</div><div>after</div></body></html>',
);
await act(() => {
root.render(<App phase={2} />);
});
expect(document.documentElement.outerHTML).toBe(
'<html lang="en"><head data-h=""><meta itemprop="" content="..head"><meta itemprop="" content="head"><meta itemprop="" content="head.."></head><body data-b=""><div>..before</div><div>before</div><div>before..</div><div>..inside</div><div>inside</div><div>inside..</div><div>..after</div><div>after</div><div>after..</div></body></html>',
);
await act(() => {
root.render(<App phase={3} />);
});
expect(document.documentElement.outerHTML).toBe(
'<html lang="en"><head data-h=""><meta itemprop="" content="..head"><meta itemprop="" content="head.."></head><body data-b=""><div>..before</div><div>before..</div><div>..inside</div><div>inside..</div><div>..after</div><div>after..</div></body></html>',
);
await act(() => {
root.unmount();
});
expect(document.documentElement.outerHTML).toBe(
'<html><head></head><body></body></html>',
);
});
it('should render root host components into body scope when the container is a the <html> tag', async () => {
function App({phase}) {
return (
<>
{phase < 1 ? null : <div>..before</div>}
{phase < 3 ? <div>before</div> : null}
{phase < 2 ? null : <div>before..</div>}
<head data-h="">
{phase < 1 ? null : <meta itemProp="" content="..head" />}
{phase < 3 ? <meta itemProp="" content="head" /> : null}
{phase < 2 ? null : <meta itemProp="" content="head.." />}
</head>
<body data-b="">
{phase < 1 ? null : <div>..inside</div>}
{phase < 3 ? <div>inside</div> : null}
{phase < 2 ? null : <div>inside..</div>}
</body>
{phase < 1 ? null : <div>..after</div>}
{phase < 3 ? <div>after</div> : null}
{phase < 2 ? null : <div>after..</div>}
</>
);
}
const root = ReactDOMClient.createRoot(document.documentElement);
await act(() => {
root.render(<App phase={0} />);
});
expect(document.documentElement.outerHTML).toBe(
'<html><head data-h=""><meta itemprop="" content="head"></head><body data-b=""><div>before</div><div>inside</div><div>after</div></body></html>',
);
await act(() => {
root.render(<App phase={1} />);
});
expect(document.documentElement.outerHTML).toBe(
'<html><head data-h=""><meta itemprop="" content="..head"><meta itemprop="" content="head"></head><body data-b=""><div>..before</div><div>before</div><div>..inside</div><div>inside</div><div>..after</div><div>after</div></body></html>',
);
await act(() => {
root.render(<App phase={2} />);
});
expect(document.documentElement.outerHTML).toBe(
'<html><head data-h=""><meta itemprop="" content="..head"><meta itemprop="" content="head"><meta itemprop="" content="head.."></head><body data-b=""><div>..before</div><div>before</div><div>before..</div><div>..inside</div><div>inside</div><div>inside..</div><div>..after</div><div>after</div><div>after..</div></body></html>',
);
await act(() => {
root.render(<App phase={3} />);
});
expect(document.documentElement.outerHTML).toBe(
'<html><head data-h=""><meta itemprop="" content="..head"><meta itemprop="" content="head.."></head><body data-b=""><div>..before</div><div>before..</div><div>..inside</div><div>inside..</div><div>..after</div><div>after..</div></body></html>',
);
await act(() => {
root.unmount();
});
expect(document.documentElement.outerHTML).toBe(
'<html><head></head><body></body></html>',
);
});
it('should render root host components into body scope when the container is a the <body> tag', async () => {
function App({phase}) {
return (
<>
{phase < 1 ? null : <div>..before</div>}
{phase < 3 ? <div>before</div> : null}
{phase < 2 ? null : <div>before..</div>}
<head data-h="">
{phase < 1 ? null : <meta itemProp="" content="..head" />}
{phase < 3 ? <meta itemProp="" content="head" /> : null}
{phase < 2 ? null : <meta itemProp="" content="head.." />}
</head>
{phase < 1 ? null : <div>..inside</div>}
{phase < 3 ? <div>inside</div> : null}
{phase < 2 ? null : <div>inside..</div>}
{phase < 1 ? null : <div>..after</div>}
{phase < 3 ? <div>after</div> : null}
{phase < 2 ? null : <div>after..</div>}
</>
);
}
const root = ReactDOMClient.createRoot(document.body);
await act(() => {
root.render(<App phase={0} />);
});
expect(document.documentElement.outerHTML).toBe(
'<html><head data-h=""><meta itemprop="" content="head"></head><body><div>before</div><div>inside</div><div>after</div></body></html>',
);
await act(() => {
root.render(<App phase={1} />);
});
expect(document.documentElement.outerHTML).toBe(
'<html><head data-h=""><meta itemprop="" content="..head"><meta itemprop="" content="head"></head><body><div>..before</div><div>before</div><div>..inside</div><div>inside</div><div>..after</div><div>after</div></body></html>',
);
await act(() => {
root.render(<App phase={2} />);
});
expect(document.documentElement.outerHTML).toBe(
'<html><head data-h=""><meta itemprop="" content="..head"><meta itemprop="" content="head"><meta itemprop="" content="head.."></head><body><div>..before</div><div>before</div><div>before..</div><div>..inside</div><div>inside</div><div>inside..</div><div>..after</div><div>after</div><div>after..</div></body></html>',
);
await act(() => {
root.render(<App phase={3} />);
});
expect(document.documentElement.outerHTML).toBe(
'<html><head data-h=""><meta itemprop="" content="..head"><meta itemprop="" content="head.."></head><body><div>..before</div><div>before..</div><div>..inside</div><div>inside..</div><div>..after</div><div>after..</div></body></html>',
);
await act(() => {
root.unmount();
});
expect(document.documentElement.outerHTML).toBe(
'<html><head></head><body></body></html>',
);
});
it('should render children of <head> into the document head even when the container is inside the document body', async () => {
function App({phase}) {
return (
<>
<div>before</div>
<head data-h="">
{phase < 1 ? null : <meta itemProp="" content="..head" />}
{phase < 3 ? <meta itemProp="" content="head" /> : null}
{phase < 2 ? null : <meta itemProp="" content="head.." />}
</head>
<div>after</div>
</>
);
}
const container = document.createElement('main');
document.body.append(container);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<App phase={0} />);
});
expect(document.documentElement.outerHTML).toBe(
'<html><head data-h=""><meta itemprop="" content="head"></head><body><main><div>before</div><div>after</div></main></body></html>',
);
// @TODO remove this warning check when we loosen the tag nesting restrictions to allow arbitrary tags at the
// root of the application
assertConsoleErrorDev([
'In HTML, <head> cannot be a child of <main>.\nThis will cause a hydration error.\n' +
' in head (at **)\n' +
' in App (at **)',
]);
await act(() => {
root.render(<App phase={1} />);
});
expect(document.documentElement.outerHTML).toBe(
'<html><head data-h=""><meta itemprop="" content="..head"><meta itemprop="" content="head"></head><body><main><div>before</div><div>after</div></main></body></html>',
);
await act(() => {
root.render(<App phase={2} />);
});
expect(document.documentElement.outerHTML).toBe(
'<html><head data-h=""><meta itemprop="" content="..head"><meta itemprop="" content="head"><meta itemprop="" content="head.."></head><body><main><div>before</div><div>after</div></main></body></html>',
);
await act(() => {
root.render(<App phase={3} />);
});
expect(document.documentElement.outerHTML).toBe(
'<html><head data-h=""><meta itemprop="" content="..head"><meta itemprop="" content="head.."></head><body><main><div>before</div><div>after</div></main></body></html>',
);
await act(() => {
root.unmount();
});
expect(document.documentElement.outerHTML).toBe(
'<html><head></head><body><main></main></body></html>',
);
});
it('can render a Suspense boundary above the <html> tag', async () => {
let suspendOnNewPromise;
let resolveCurrentPromise;
let currentPromise;
function createNewPromise() {
currentPromise = new Promise(r => {
resolveCurrentPromise = r;
});
return currentPromise;
}
createNewPromise();
function Comp() {
const [promise, setPromise] = React.useState(currentPromise);
suspendOnNewPromise = () => {
setPromise(createNewPromise());
};
React.use(promise);
return null;
}
const fallback = (
<html data-fallback="">
<body data-fallback="">
<div>fallback</div>
</body>
</html>
);
const main = (
<html lang="en">
<head>
<meta itemProp="" content="primary" />
</head>
<body>
<div>
<Message />
</div>
</body>
</html>
);
let suspendOnNewMessage;
let currentMessage;
let resolveCurrentMessage;
function createNewMessage() {
currentMessage = new Promise(r => {
resolveCurrentMessage = r;
});
return currentMessage;
}
createNewMessage();
resolveCurrentMessage('hello world');
function Message() {
const [pendingMessage, setPendingMessage] =
React.useState(currentMessage);
suspendOnNewMessage = () => {
setPendingMessage(createNewMessage());
};
return React.use(pendingMessage);
}
function App() {
return (
<React.Suspense fallback={fallback}>
<Comp />
{main}
</React.Suspense>
);
}
const root = ReactDOMClient.createRoot(document);
await act(() => {
root.render(<App />);
});
// The initial render is blocked by promiseA so we see the fallback Document
expect(document.documentElement.outerHTML).toBe(
'<html data-fallback=""><head></head><body data-fallback=""><div>fallback</div></body></html>',
);
await act(() => {
resolveCurrentPromise();
});
// When promiseA resolves we see the primary Document
expect(document.documentElement.outerHTML).toBe(
'<html lang="en"><head><meta itemprop="" content="primary"></head><body><div>hello world</div></body></html>',
);
await act(() => {
suspendOnNewPromise();
});
// When we switch to rendering ComponentB synchronously we have to put the Document back into fallback
// The primary content remains hidden until promiseB resolves
expect(document.documentElement.outerHTML).toBe(
'<html data-fallback=""><head><meta itemprop="" content="primary" style="display: none;"></head><body data-fallback=""><div style="display: none;">hello world</div><div>fallback</div></body></html>',
);
await act(() => {
resolveCurrentPromise();
});
// When promiseB resolves we see the new primary content inside the primary Document
// style attributes stick around after being unhidden by the Suspense boundary
expect(document.documentElement.outerHTML).toBe(
'<html lang="en"><head><meta itemprop="" content="primary" style=""></head><body><div style="">hello world</div></body></html>',
);
await act(() => {
React.startTransition(() => {
suspendOnNewPromise();
});
});
expect(document.documentElement.outerHTML).toBe(
'<html lang="en"><head><meta itemprop="" content="primary" style=""></head><body><div style="">hello world</div></body></html>',
);
await act(() => {
resolveCurrentPromise();
});
expect(document.documentElement.outerHTML).toBe(
'<html lang="en"><head><meta itemprop="" content="primary" style=""></head><body><div style="">hello world</div></body></html>',
);
await act(() => {
suspendOnNewMessage();
});
// When we update the message itself we will be causing updates on the primary content of the Suspense boundary.
// The reason we also test for this is to make sure we don't double acquire the document singletons while
// disappearing and reappearing layout effects
expect(document.documentElement.outerHTML).toBe(
'<html data-fallback=""><head><meta itemprop="" content="primary" style="display: none;"></head><body data-fallback=""><div style="display: none;">hello world</div><div>fallback</div></body></html>',
);
await act(() => {