Skip to content

Commit 4a40cad

Browse files
committed
Add test
1 parent 75695bc commit 4a40cad

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

packages/react-server-dom-webpack/src/__tests__/ReactFlightDOM-test.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,6 +1998,99 @@ describe('ReactFlightDOM', () => {
19981998
expect(hintRows.length).toEqual(6);
19991999
});
20002000

2001+
it('preloads resources without needing to render them', async () => {
2002+
function NoScriptComponent() {
2003+
return (
2004+
<p>
2005+
<img src="image-do-not-load" />
2006+
<link rel="stylesheet" href="css-do-not-load" />
2007+
</p>
2008+
);
2009+
}
2010+
2011+
function Component() {
2012+
return (
2013+
<div>
2014+
<img src="image-resource" />
2015+
<img
2016+
src="image-do-not-load"
2017+
srcSet="image-preload-src-set"
2018+
sizes="image-sizes"
2019+
/>
2020+
<img src="image-do-not-load" loading="lazy" />
2021+
<link
2022+
rel="preload"
2023+
href="video-resource"
2024+
as="video"
2025+
media="(orientation: landscape)"
2026+
/>
2027+
<link rel="modulepreload" href="module-resource" />
2028+
<picture>
2029+
<source
2030+
srcSet="image-not-yet-preloaded"
2031+
media="(orientation: portrait)"
2032+
/>
2033+
<img src="image-do-not-load" />
2034+
</picture>
2035+
<noscript>
2036+
<NoScriptComponent />
2037+
</noscript>
2038+
<link rel="stylesheet" href="css-resource" />
2039+
</div>
2040+
);
2041+
}
2042+
2043+
const {writable, readable} = getTestStream();
2044+
const {pipe} = await serverAct(() =>
2045+
ReactServerDOMServer.renderToPipeableStream(<Component />, webpackMap),
2046+
);
2047+
pipe(writable);
2048+
2049+
let response = null;
2050+
function getResponse() {
2051+
if (response === null) {
2052+
response = ReactServerDOMClient.createFromReadableStream(readable);
2053+
}
2054+
return response;
2055+
}
2056+
2057+
function App() {
2058+
// Not rendered but use for its side-effects.
2059+
getResponse();
2060+
return <p>hello world</p>;
2061+
}
2062+
2063+
const container = document.createElement('div');
2064+
const root = ReactDOMClient.createRoot(container);
2065+
await act(() => {
2066+
root.render(<App />);
2067+
});
2068+
2069+
expect(getMeaningfulChildren(document)).toEqual(
2070+
<html>
2071+
<head>
2072+
<link rel="preload" as="image" href="image-resource" />
2073+
<link
2074+
rel="preload"
2075+
as="image"
2076+
imagesrcset="image-preload-src-set"
2077+
imagesizes="image-sizes"
2078+
/>
2079+
<link
2080+
rel="preload"
2081+
as="video"
2082+
href="video-resource"
2083+
media="(orientation: landscape)"
2084+
/>
2085+
<link rel="modulepreload" href="module-resource" />
2086+
<link rel="preload" as="stylesheet" href="css-resource" />
2087+
</head>
2088+
<body />
2089+
</html>,
2090+
);
2091+
expect(getMeaningfulChildren(container)).toEqual(<p>hello world</p>);
2092+
});
2093+
20012094
it('should be able to include a client reference in printed errors', async () => {
20022095
const reportedErrors = [];
20032096

0 commit comments

Comments
 (0)