Skip to content

Commit

Permalink
Merge pull request #23422 from storybookjs/fix/nextjs-inconsistent-sn…
Browse files Browse the repository at this point in the history
…apshots

Build: Fix inconsistent stories in nextjs example
  • Loading branch information
ndelangen authored Jul 12, 2023
2 parents 204eb01 + 4e38ccc commit f76b060
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
41 changes: 41 additions & 0 deletions code/frameworks/nextjs/template/stories/Image.stories.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import Image from 'next/image';
import { waitFor } from '@storybook/testing-library';

import Accessibility from '../../assets/accessibility.svg';

Expand Down Expand Up @@ -28,6 +29,11 @@ export const BlurredAbsolutePlaceholder = {
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAABP5JREFUWEeNlwtz2zgMhEGKsv/k9XFp82xe11763yOJVGcXC4m2czM3GYa0E2s/LACSTi+vv1czM/7CvPpqxY/ejPeS3khmFiPHOiVLHKaZi4ux4j18GpMlS6cALupEQBCKQM4BdnGzjIcJgs//QBxAPQAem55f3yL4PWJIdyCyhlMfPdYZot0cwj3Ayg/5JwHA13paen7pADphxr/n5VI8JQsHCCGQ3gVGLLsxQ3h/LYSn5383B05rwNOws3Z576LOTOduvwfrOd5FtVat4akx0uPTrw8BNuUz23vLsY7hmg7i4ipqM2saiAdruNuirh4ff0bNdb3Qy3vkvfAQwrkHoDxTTZtDCOKrC1bMEkdnsQh/PLyetOGHkXeRAgQAQ84efQcBdUhvFofoulpdm9WGGTA+AJEe7l+i37a2c371tCFKs5zzJjxQNBMi1im7OCudP2aNghJuzZaGdSMEZjpwf/t0UgNdg9DOyLGLnY0BUHlzwVtNDkgEQhBeKkb1tUDgQrq7frwAiIJi5BKAeIFgHk5mOpPzvgltOfcoK0Rrs7lWHwsgqtXarK3N0u23h5Ne8+3Cqxn5RYSMfHCAMgDAx4CBWlA9RAGw0GA/ol0gvFB4WjAvBAFUa83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/QODjTHXhU7hMQAAAABJRU5ErkJggg==',
placeholder: 'blur',
},
parameters: {
// ignoring in Chromatic to avoid inconsistent snapshots
// given that the switch from blur to image is quite fast
chromatic: { disableSnapshot: true },
},
};

export const FilledParent = {
Expand Down Expand Up @@ -63,6 +69,25 @@ export const Lazy = {
</>
),
],
loaders: [
async () => {
// make sure we start at the top to test the scrolling into view functionality every time the story renders
// eslint-disable-next-line no-undef
window.scrollTo({ top: 0 });
await new Promise((res) => {
setTimeout(res, 16);
});
return {};
},
],
play: async ({ canvasElement, step }) => {
await step('scroll image into view', () => {
canvasElement.scrollIntoView(false);
});
await step('wait for images to load', async () => {
await waitFor(waitForImagesToLoad);
});
},
};

export const Eager = {
Expand All @@ -75,3 +100,19 @@ export const Eager = {
},
},
};

async function waitForImagesToLoad() {
// eslint-disable-next-line no-undef
const images = Array.from(document.getElementsByTagName('img'));

await Promise.all(
images.map((image) => {
if (image.complete) {
return Promise.resolve();
}
return new Promise((resolve) => {
image.addEventListener('load', resolve);
});
})
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export const BlurredAbsolutePlaceholder = {
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAABP5JREFUWEeNlwtz2zgMhEGKsv/k9XFp82xe11763yOJVGcXC4m2czM3GYa0E2s/LACSTi+vv1czM/7CvPpqxY/ejPeS3khmFiPHOiVLHKaZi4ux4j18GpMlS6cALupEQBCKQM4BdnGzjIcJgs//QBxAPQAem55f3yL4PWJIdyCyhlMfPdYZot0cwj3Ayg/5JwHA13paen7pADphxr/n5VI8JQsHCCGQ3gVGLLsxQ3h/LYSn5383B05rwNOws3Z576LOTOduvwfrOd5FtVat4akx0uPTrw8BNuUz23vLsY7hmg7i4ipqM2saiAdruNuirh4ff0bNdb3Qy3vkvfAQwrkHoDxTTZtDCOKrC1bMEkdnsQh/PLyetOGHkXeRAgQAQ84efQcBdUhvFofoulpdm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swBhv+6CBGjeBSxEOUAI7HyKHkD4O9xKb3/feQouAI4uLBciHRRHmgbfA7h/xFc9AngNBADthvii1sMOiPwDAFeyt6s7FSFS4PmnA1v0vQvqDqQKAAPE/weAUuEgsj8c+H11Twdw/AKANXA82EDr5cJBEEzB3oI4Mb0AdR3nNw8vQnegWuvqAABwJFJEBwDgNdA7IOs3gL0LhuJdwBY8c4BfNnDdVgooHiOqn/b7JoSW/QODjTHXhU7hMQAAAABJRU5ErkJggg==',
placeholder: 'blur',
},
parameters: {
// ignoring in Chromatic to avoid inconsistent snapshots
// given that the switch from blur to image is quite fast
chromatic: { disableSnapshot: true },
},
};

export const FilledParent = {
Expand Down

0 comments on commit f76b060

Please sign in to comment.