Skip to content

Commit 8d0a41d

Browse files
committed
Update float test with server assertions
1 parent 6bf603b commit 8d0a41d

File tree

1 file changed

+47
-42
lines changed

1 file changed

+47
-42
lines changed

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

Lines changed: 47 additions & 42 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
});
@@ -468,10 +482,7 @@ describe('ReactDOMFloat', () => {
468482
expect(getVisibleChildren(document)).toEqual(
469483
<html>
470484
<head />
471-
<body>
472-
foo
473-
<link rel="preload" as="style" href="foo" />
474-
</body>
485+
<body>foo</body>
475486
</html>,
476487
);
477488
});
@@ -537,16 +548,14 @@ describe('ReactDOMFloat', () => {
537548
ReactDOM.preinit('foo', {as: 'style'});
538549
ReactDOMClient.hydrateRoot(container, null);
539550
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(
551+
expect(getVisibleChildren(document)).toEqual(
545552
<html>
546553
<head>
547554
<link rel="preload" as="style" href="bar" />
548555
</head>
549-
<body />
556+
<body>
557+
<div id="container" />
558+
</body>
550559
</html>,
551560
);
552561
});
@@ -1695,8 +1704,6 @@ describe('ReactDOMFloat', () => {
16951704
<body>
16961705
<div>Hello</div>
16971706
<div>loading...</div>
1698-
<link rel="preload" href="one" as="style" />
1699-
<link rel="preload" href="two" as="style" />
17001707
</body>
17011708
</html>,
17021709
);
@@ -1713,8 +1720,6 @@ describe('ReactDOMFloat', () => {
17131720
<body>
17141721
<div>Hello</div>
17151722
<div>bar</div>
1716-
<link rel="preload" href="one" as="style" />
1717-
<link rel="preload" href="two" as="style" />
17181723
</body>
17191724
</html>,
17201725
);

0 commit comments

Comments
 (0)