Skip to content

Commit 56c2c3d

Browse files
committed
Update float test with server assertions
1 parent 6bf603b commit 56c2c3d

File tree

1 file changed

+46
-39
lines changed

1 file changed

+46
-39
lines changed

packages/react-dom/src/__tests__/ReactDOMFloat-test.js

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
* @emails react-core
8+
* @jest-environment node
89
*/
910

1011
'use strict';
1112

1213
let JSDOM;
14+
let JSDOMVirtualConsole;
1315
let Stream;
1416
let Scheduler;
1517
let React;
@@ -18,36 +20,52 @@ let ReactDOMClient;
1820
let ReactDOMFizzServer;
1921
let Suspense;
2022
let textCache;
21-
let document;
2223
let writable;
2324
const CSPnonce = null;
2425
let container;
2526
let buffer = '';
2627
let hasErrored = false;
2728
let fatalError = undefined;
2829

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+
2961
describe('ReactDOMFloat', () => {
3062
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+
);
4066

4167
textCache = new Map();
4268

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;
5169
container = document.getElementById('container');
5270

5371
buffer = '';
@@ -129,10 +147,8 @@ describe('ReactDOMFloat', () => {
129147
// We assume that we have now received a proper fragment of HTML.
130148
const bufferedContent = buffer;
131149
// Test Environment
132-
const jsdom = new JSDOM(bufferedContent, {
133-
runScripts: 'dangerously',
134-
});
135-
document = jsdom.window.document;
150+
resetModules(bufferedContent);
151+
136152
container = document;
137153
buffer = '';
138154
}
@@ -394,16 +410,14 @@ describe('ReactDOMFloat', () => {
394410
ReactDOM.preload('foo', {as: 'style'});
395411
ReactDOMClient.createRoot(container);
396412
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(
402414
<html>
403415
<head>
404416
<link rel="preload" as="style" href="bar" />
405417
</head>
406-
<body />
418+
<body>
419+
<div id="container" />
420+
</body>
407421
</html>,
408422
);
409423
});
@@ -470,7 +484,6 @@ describe('ReactDOMFloat', () => {
470484
<head />
471485
<body>
472486
foo
473-
<link rel="preload" as="style" href="foo" />
474487
</body>
475488
</html>,
476489
);
@@ -537,16 +550,14 @@ describe('ReactDOMFloat', () => {
537550
ReactDOM.preinit('foo', {as: 'style'});
538551
ReactDOMClient.hydrateRoot(container, null);
539552
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(
545554
<html>
546555
<head>
547556
<link rel="preload" as="style" href="bar" />
548557
</head>
549-
<body />
558+
<body>
559+
<div id="container" />
560+
</body>
550561
</html>,
551562
);
552563
});
@@ -1695,8 +1706,6 @@ describe('ReactDOMFloat', () => {
16951706
<body>
16961707
<div>Hello</div>
16971708
<div>loading...</div>
1698-
<link rel="preload" href="one" as="style" />
1699-
<link rel="preload" href="two" as="style" />
17001709
</body>
17011710
</html>,
17021711
);
@@ -1713,8 +1722,6 @@ describe('ReactDOMFloat', () => {
17131722
<body>
17141723
<div>Hello</div>
17151724
<div>bar</div>
1716-
<link rel="preload" href="one" as="style" />
1717-
<link rel="preload" href="two" as="style" />
17181725
</body>
17191726
</html>,
17201727
);

0 commit comments

Comments
 (0)