Skip to content

Commit 6facdf9

Browse files
committed
Float tests
1 parent 8b1ad04 commit 6facdf9

File tree

3 files changed

+94
-27
lines changed

3 files changed

+94
-27
lines changed

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

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
*/
99

1010
'use strict';
11-
import {replaceScriptsAndMove, mergeOptions} from '../test-utils/FizzTestUtils';
11+
import {
12+
replaceScriptsAndMove,
13+
mergeOptions,
14+
stripExternalRuntimeInString,
15+
stripExternalRuntimeInNodes,
16+
} from '../test-utils/FizzTestUtils';
1217

1318
let JSDOM;
1419
let Stream;
@@ -577,7 +582,12 @@ describe('ReactDOMFizzServer', () => {
577582
// Because there is no content inside the Suspense boundary that could've
578583
// been written, we expect to not see any additional partial data flushed
579584
// yet.
580-
expect(container.childNodes.length).toBe(1);
585+
expect(
586+
stripExternalRuntimeInNodes(
587+
container.childNodes,
588+
renderOptions.unstable_externalRuntimeSrc,
589+
).length,
590+
).toBe(1);
581591
await act(async () => {
582592
resolveElement({default: <Text text="Hello" />});
583593
});
@@ -3471,21 +3481,19 @@ describe('ReactDOMFizzServer', () => {
34713481
</body>
34723482
</html>,
34733483
);
3474-
const expectedScripts = [
3484+
expect(
3485+
stripExternalRuntimeInNodes(
3486+
document.getElementsByTagName('script'),
3487+
renderOptions.unstable_externalRuntimeSrc,
3488+
).map(n => n.outerHTML),
3489+
).toEqual([
34753490
'<script src="foo" async=""></script>',
34763491
'<script src="bar" async=""></script>',
34773492
'<script src="baz" integrity="qux" async=""></script>',
34783493
'<script type="module" src="quux" async=""></script>',
34793494
'<script type="module" src="corge" async=""></script>',
34803495
'<script type="module" src="grault" integrity="garply" async=""></script>',
3481-
];
3482-
let actualScripts = Array.from(document.getElementsByTagName('script')).map(
3483-
n => n.outerHTML,
3484-
);
3485-
if (gate(flags => flags.enableFizzExternalRuntime)) {
3486-
actualScripts = actualScripts.slice(1);
3487-
}
3488-
expect(actualScripts).toEqual(expectedScripts);
3496+
]);
34893497
});
34903498

34913499
describe('bootstrapScriptContent escaping', () => {
@@ -4494,10 +4502,12 @@ describe('ReactDOMFizzServer', () => {
44944502
const {pipe} = renderToPipeableStream(<App name="Foo" />);
44954503
pipe(writable);
44964504
});
4497-
4498-
expect(container.innerHTML).toEqual(
4499-
'<div>hello<b>world, <!-- -->Foo</b>!</div>',
4500-
);
4505+
expect(
4506+
stripExternalRuntimeInString(
4507+
container.innerHTML,
4508+
renderOptions.unstable_externalRuntimeSrc,
4509+
),
4510+
).toEqual('<div>hello<b>world, <!-- -->Foo</b>!</div>');
45014511
const errors = [];
45024512
ReactDOMClient.hydrateRoot(container, <App name="Foo" />, {
45034513
onRecoverableError(error) {
@@ -4542,13 +4552,14 @@ describe('ReactDOMFizzServer', () => {
45424552
expect(container.firstElementChild.outerHTML).toEqual(
45434553
'<div id="app-div">hello<b>world, Foo</b>!</div>',
45444554
);
4545-
// there are extra script / data nodes at the end of container
4546-
if (gate(flags => flags.enableFizzExternalRuntime)) {
4547-
// extra script node inserted for the external runtime
4548-
expect(container.childNodes.length).toBe(6);
4549-
} else {
4550-
expect(container.childNodes.length).toBe(5);
4551-
}
4555+
// there are extra script nodes at the end of container
4556+
expect(
4557+
stripExternalRuntimeInNodes(
4558+
container.childNodes,
4559+
renderOptions.unstable_externalRuntimeSrc,
4560+
).length,
4561+
).toBe(5);
4562+
45524563
const div = container.childNodes[1];
45534564
expect(div.childNodes.length).toBe(3);
45544565
const b = div.childNodes[1];
@@ -4851,8 +4862,12 @@ describe('ReactDOMFizzServer', () => {
48514862
pipe(writable);
48524863
});
48534864

4854-
// strip inserted external runtime
4855-
expect(container.innerHTML).toEqual(
4865+
expect(
4866+
stripExternalRuntimeInString(
4867+
container.innerHTML,
4868+
renderOptions.unstable_externalRuntimeSrc,
4869+
),
4870+
).toEqual(
48564871
'<div><!--$-->hello<!-- -->world<!-- --><!--/$--><!--$-->world<!-- --><!--/$--><!--$-->hello<!-- -->world<!-- --><br><!--/$--><!--$-->world<!-- --><br><!--/$--></div>',
48574872
);
48584873

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
*/
99

1010
'use strict';
11-
import {replaceScriptsAndMove, mergeOptions} from '../test-utils/FizzTestUtils';
11+
import {
12+
replaceScriptsAndMove,
13+
mergeOptions,
14+
stripExternalRuntimeInString,
15+
} from '../test-utils/FizzTestUtils';
1216

1317
let JSDOM;
1418
let Stream;
@@ -26,7 +30,7 @@ let container;
2630
let buffer = '';
2731
let hasErrored = false;
2832
let fatalError = undefined;
29-
const renderOptions = {};
33+
let renderOptions;
3034
const rollupCache: Map<string, string | null> = new Map();
3135

3236
describe('ReactDOMFloat', () => {
@@ -65,6 +69,12 @@ describe('ReactDOMFloat', () => {
6569
hasErrored = true;
6670
fatalError = error;
6771
});
72+
73+
renderOptions = {};
74+
if (gate(flags => flags.enableFizzExternalRuntime)) {
75+
renderOptions.unstable_externalRuntimeSrc =
76+
'../server/ReactDOMServerExternalRuntime.js';
77+
}
6878
});
6979

7080
function normalizeCodeLocInfo(str) {
@@ -529,6 +539,12 @@ describe('ReactDOMFloat', () => {
529539
);
530540
pipe(writable);
531541
});
542+
if (gate(flags => flags.enableFizzExternalRuntime)) {
543+
chunks[0] = stripExternalRuntimeInString(
544+
chunks[0],
545+
renderOptions.unstable_externalRuntimeSrc,
546+
);
547+
}
532548
expect(chunks).toEqual([
533549
'<!DOCTYPE html><html><script async="" src="foo"></script><title>foo</title><body>bar',
534550
'</body></html>',

packages/react-dom/src/test-utils/FizzTestUtils.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,40 @@ function mergeOptions(options: Object, defaultOptions: Object): Object {
122122
};
123123
}
124124

125-
export {replaceScriptsAndMove, mergeOptions};
125+
function stripExternalRuntimeInString(
126+
source: string,
127+
externalRuntimeSrc: string | null,
128+
): string {
129+
if (externalRuntimeSrc == null) {
130+
return source;
131+
}
132+
const matchExternalRuntimeTag = new RegExp(
133+
'<script src="' + externalRuntimeSrc + '"[\\s\\S]*/script>',
134+
'm',
135+
);
136+
return source.replace(matchExternalRuntimeTag, '');
137+
}
138+
139+
function stripExternalRuntimeInNodes(
140+
nodes: HTMLElement[] | HTMLCollection<HTMLElement>,
141+
externalRuntimeSrc: string | null,
142+
): HTMLElement[] {
143+
if (!Array.isArray(nodes)) {
144+
nodes = Array.from(nodes);
145+
}
146+
if (externalRuntimeSrc == null) {
147+
return nodes;
148+
}
149+
return nodes.filter(
150+
n =>
151+
(n.tagName !== 'SCRIPT' && n.tagName !== 'script') ||
152+
n.getAttribute('src') !== externalRuntimeSrc,
153+
);
154+
}
155+
156+
export {
157+
replaceScriptsAndMove,
158+
mergeOptions,
159+
stripExternalRuntimeInString,
160+
stripExternalRuntimeInNodes,
161+
};

0 commit comments

Comments
 (0)