Skip to content

Commit

Permalink
Move rollupCache to test helper/utils file
Browse files Browse the repository at this point in the history
  • Loading branch information
mofeiZ committed Nov 1, 2022
1 parent a4662cb commit 5f4d560
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 29 deletions.
10 changes: 2 additions & 8 deletions packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ let buffer = '';
let hasErrored = false;
let fatalError = undefined;
const renderOptions = {};
const rollupCache: Map<string, string | null> = new Map();

describe('ReactDOMFizzServer', () => {
beforeEach(() => {
Expand Down Expand Up @@ -137,7 +136,7 @@ describe('ReactDOMFizzServer', () => {
container.nodeName === '#document' ? container.body : container;
while (fakeBody.firstChild) {
const node = fakeBody.firstChild;
await replaceScriptsAndMove(window, rollupCache, CSPnonce, node, parent);
await replaceScriptsAndMove(window, CSPnonce, node, parent);
}
}

Expand All @@ -163,12 +162,7 @@ describe('ReactDOMFizzServer', () => {
document = jsdom.window.document;
container = document;
buffer = '';
await replaceScriptsAndMove(
window,
rollupCache,
CSPnonce,
document.documentElement,
);
await replaceScriptsAndMove(window, CSPnonce, document.documentElement);
}

function getVisibleChildren(element) {
Expand Down
16 changes: 2 additions & 14 deletions packages/react-dom/src/__tests__/ReactDOMFloat-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ let buffer = '';
let hasErrored = false;
let fatalError = undefined;
const renderOptions = {};
const rollupCache: Map<string, string | null> = new Map();

describe('ReactDOMFloat', () => {
beforeEach(() => {
Expand Down Expand Up @@ -103,13 +102,7 @@ describe('ReactDOMFloat', () => {
container.nodeName === '#document' ? container.body : container;
while (fakeBody.firstChild) {
const node = fakeBody.firstChild;
await replaceScriptsAndMove(
document.defaultView,
rollupCache,
CSPnonce,
node,
parent,
);
await replaceScriptsAndMove(document.defaultView, CSPnonce, node, parent);
}
}

Expand All @@ -134,12 +127,7 @@ describe('ReactDOMFloat', () => {
document = jsdom.window.document;
container = document;
buffer = '';
await replaceScriptsAndMove(
jsdom.window,
rollupCache,
null,
document.documentElement,
);
await replaceScriptsAndMove(jsdom.window, null, document.documentElement);
}

function getMeaningfulChildren(element) {
Expand Down
12 changes: 5 additions & 7 deletions packages/react-dom/src/test-utils/FizzTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import * as fs from 'fs';
import replace from 'rollup-plugin-replace';
import {rollup} from 'rollup';

const rollupCache: Map<string, string | null> = new Map();

// Utility function to read and bundle a standalone browser script
async function getRollupResult(
scriptSrc: string,
rollupCache: Map<string, string | null>,
): Promise<string | null> {
async function getRollupResult(scriptSrc: string): Promise<string | null> {
const cachedResult = rollupCache.get(scriptSrc);
if (cachedResult !== undefined) {
return cachedResult;
Expand Down Expand Up @@ -71,7 +70,6 @@ async function getRollupResult(
// 2. Resolving scripts with sources
async function replaceScriptsAndMove(
window: any,
rollupCache: Map<string, string | null>,
CSPnonce: string | null,
node: Node,
parent: Node | null,
Expand All @@ -85,7 +83,7 @@ async function replaceScriptsAndMove(
const script = window.document.createElement('SCRIPT');
const scriptSrc = element.getAttribute('src');
if (scriptSrc) {
const rollupOutput = await getRollupResult(scriptSrc, rollupCache);
const rollupOutput = await getRollupResult(scriptSrc);
if (rollupOutput) {
// Manually call eval(...) here, since changing the HTML text content
// may interfere with hydration
Expand All @@ -107,7 +105,7 @@ async function replaceScriptsAndMove(
} else {
for (let i = 0; i < node.childNodes.length; i++) {
const inner = node.childNodes[i];
await replaceScriptsAndMove(window, rollupCache, CSPnonce, inner, null);
await replaceScriptsAndMove(window, CSPnonce, inner, null);
}
if (parent != null) {
parent.appendChild(node);
Expand Down

0 comments on commit 5f4d560

Please sign in to comment.