Skip to content

Commit

Permalink
Add test for image preload media attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
damnsamn committed Jan 25, 2024
1 parent 72411c4 commit 734d239
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFloat-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3935,6 +3935,91 @@ body {
);
});

it('should handle media on image preload', async () => {
function App({isClient}) {
ReactDOM.preload('/server', {
as: 'image',
imageSrcSet: '/server',
imageSizes: '100vw',
media: 'print and (min-width: 768px)',
});

if (isClient) {
ReactDOM.preload('/client', {
as: 'image',
imageSrcSet: '/client',
imageSizes: '100vw',
media: 'screen and (max-width: 480px)',
});
}

return (
<html>
<body>hello</body>
</html>
);
}

await act(() => {
renderToPipeableStream(<App />).pipe(writable);
});
expect(getMeaningfulChildren(document)).toEqual(
<html>
<head>
<link
rel="preload"
as="image"
imagesrcset="/server"
imagesizes="100vw"
media="print and (min-width: 768px)"
/>
</head>
<body>hello</body>
</html>,
);

const root = ReactDOMClient.hydrateRoot(document, <App />);
await waitForAll([]);
expect(getMeaningfulChildren(document)).toEqual(
<html>
<head>
<link
rel="preload"
as="image"
imagesrcset="/server"
imagesizes="100vw"
media="print and (min-width: 768px)"
/>
</head>
<body>hello</body>
</html>,
);

root.render(<App isClient={true} />);
await waitForAll([]);
expect(getMeaningfulChildren(document)).toEqual(
<html>
<head>
<link
rel="preload"
as="image"
imagesrcset="/server"
imagesizes="100vw"
media="print and (min-width: 768px)"
/>
<link
rel="preload"
as="image"
imagesrcset="/client"
imagesizes="100vw"
media="screen and (max-width: 480px)"
/>
</head>
<body>hello</body>
</html>,
);
});

it('can emit preloads for non-lazy images that are rendered', async () => {
function App() {
ReactDOM.preload('script', {as: 'script'});
Expand Down

0 comments on commit 734d239

Please sign in to comment.