5
5
* LICENSE file in the root directory of this source tree.
6
6
*
7
7
* @emails react-core
8
+ * @jest -environment node
8
9
*/
9
10
10
11
'use strict' ;
11
12
12
13
let JSDOM ;
14
+ let JSDOMVirtualConsole ;
13
15
let Stream ;
14
16
let Scheduler ;
15
17
let React ;
@@ -18,36 +20,52 @@ let ReactDOMClient;
18
20
let ReactDOMFizzServer ;
19
21
let Suspense ;
20
22
let textCache ;
21
- let document ;
22
23
let writable ;
23
24
const CSPnonce = null ;
24
25
let container ;
25
26
let buffer = '' ;
26
27
let hasErrored = false ;
27
28
let fatalError = undefined ;
28
29
30
+ function resetModules ( markup ) {
31
+ jest . resetModules ( ) ;
32
+
33
+ // Test Environment
34
+ ( { JSDOM , VirtualConsole : JSDOMVirtualConsole } = require ( 'jsdom' ) ) ;
35
+ const virtualConsole = new JSDOMVirtualConsole ( ) ;
36
+ virtualConsole . sendTo ( console , {
37
+ omitJSDOMErrors : true ,
38
+ } ) ;
39
+ virtualConsole . on ( 'jsdomError' , error => {
40
+ console . error ( error ) ;
41
+ } ) ;
42
+ const jsdom = new JSDOM ( markup , {
43
+ runScripts : 'dangerously' ,
44
+ virtualConsole,
45
+ } ) ;
46
+
47
+ global . window = jsdom . window ;
48
+ global . document = jsdom . window . document ;
49
+ global . navigator = jsdom . window . navigator ;
50
+ global . Node = jsdom . window . Node ;
51
+
52
+ Scheduler = require ( 'scheduler' ) ;
53
+ React = require ( 'react' ) ;
54
+ ReactDOM = require ( 'react-dom' ) ;
55
+ ReactDOMClient = require ( 'react-dom/client' ) ;
56
+ ReactDOMFizzServer = require ( 'react-dom/server' ) ;
57
+ Stream = require ( 'stream' ) ;
58
+ Suspense = React . Suspense ;
59
+ }
60
+
29
61
describe ( 'ReactDOMFloat' , ( ) => {
30
62
beforeEach ( ( ) => {
31
- jest . resetModules ( ) ;
32
- JSDOM = require ( 'jsdom' ) . JSDOM ;
33
- Scheduler = require ( 'scheduler' ) ;
34
- React = require ( 'react' ) ;
35
- ReactDOM = require ( 'react-dom' ) ;
36
- ReactDOMClient = require ( 'react-dom/client' ) ;
37
- ReactDOMFizzServer = require ( 'react-dom/server' ) ;
38
- Stream = require ( 'stream' ) ;
39
- Suspense = React . Suspense ;
63
+ resetModules (
64
+ '<!DOCTYPE html><html><head></head><body><div id="container">' ,
65
+ ) ;
40
66
41
67
textCache = new Map ( ) ;
42
68
43
- // Test Environment
44
- const jsdom = new JSDOM (
45
- '<!DOCTYPE html><html><head></head><body><div id="container">' ,
46
- {
47
- runScripts : 'dangerously' ,
48
- } ,
49
- ) ;
50
- document = jsdom . window . document ;
51
69
container = document . getElementById ( 'container' ) ;
52
70
53
71
buffer = '' ;
@@ -129,10 +147,8 @@ describe('ReactDOMFloat', () => {
129
147
// We assume that we have now received a proper fragment of HTML.
130
148
const bufferedContent = buffer ;
131
149
// Test Environment
132
- const jsdom = new JSDOM ( bufferedContent , {
133
- runScripts : 'dangerously' ,
134
- } ) ;
135
- document = jsdom . window . document ;
150
+ resetModules ( bufferedContent ) ;
151
+
136
152
container = document ;
137
153
buffer = '' ;
138
154
}
@@ -394,16 +410,14 @@ describe('ReactDOMFloat', () => {
394
410
ReactDOM . preload ( 'foo' , { as : 'style' } ) ;
395
411
ReactDOMClient . createRoot ( container ) ;
396
412
ReactDOM . preload ( 'bar' , { as : 'style' } ) ;
397
- // We need to use global.document because preload falls back
398
- // to the window.document global when no other documents have been used
399
- // The way the JSDOM runtim is created for these tests the local document
400
- // global does not point to the global.document
401
- expect ( getVisibleChildren ( global . document ) ) . toEqual (
413
+ expect ( getVisibleChildren ( document ) ) . toEqual (
402
414
< html >
403
415
< head >
404
416
< link rel = "preload" as = "style" href = "bar" />
405
417
</ head >
406
- < body />
418
+ < body >
419
+ < div id = "container" />
420
+ </ body >
407
421
</ html > ,
408
422
) ;
409
423
} ) ;
@@ -470,7 +484,6 @@ describe('ReactDOMFloat', () => {
470
484
< head />
471
485
< body >
472
486
foo
473
- < link rel = "preload" as = "style" href = "foo" />
474
487
</ body >
475
488
</ html > ,
476
489
) ;
@@ -537,16 +550,14 @@ describe('ReactDOMFloat', () => {
537
550
ReactDOM . preinit ( 'foo' , { as : 'style' } ) ;
538
551
ReactDOMClient . hydrateRoot ( container , null ) ;
539
552
ReactDOM . preinit ( 'bar' , { as : 'style' } ) ;
540
- // We need to use global.document because preload falls back
541
- // to the window.document global when no other documents have been used
542
- // The way the JSDOM runtim is created for these tests the local document
543
- // global does not point to the global.document
544
- expect ( getVisibleChildren ( global . document ) ) . toEqual (
553
+ expect ( getVisibleChildren ( document ) ) . toEqual (
545
554
< html >
546
555
< head >
547
556
< link rel = "preload" as = "style" href = "bar" />
548
557
</ head >
549
- < body />
558
+ < body >
559
+ < div id = "container" />
560
+ </ body >
550
561
</ html > ,
551
562
) ;
552
563
} ) ;
@@ -1695,8 +1706,6 @@ describe('ReactDOMFloat', () => {
1695
1706
< body >
1696
1707
< div > Hello</ div >
1697
1708
< div > loading...</ div >
1698
- < link rel = "preload" href = "one" as = "style" />
1699
- < link rel = "preload" href = "two" as = "style" />
1700
1709
</ body >
1701
1710
</ html > ,
1702
1711
) ;
@@ -1713,8 +1722,6 @@ describe('ReactDOMFloat', () => {
1713
1722
< body >
1714
1723
< div > Hello</ div >
1715
1724
< div > bar</ div >
1716
- < link rel = "preload" href = "one" as = "style" />
1717
- < link rel = "preload" href = "two" as = "style" />
1718
1725
</ body >
1719
1726
</ html > ,
1720
1727
) ;
0 commit comments