Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: stabilize image loading for visual regression tests #3023

Merged
merged 20 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/elements/core/testing/wait-for-image-ready.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
import type { SbbImageElement } from '../../image.js';
import { isSafari } from '../dom.js';

export async function waitForImageReady(
element: HTMLImageElement | SbbImageElement,
timeoutInMilliseconds = 2 * 1000,
): Promise<void> {
const imgElement =
element.localName === 'sbb-image'
? (element.shadowRoot?.querySelector<HTMLImageElement>('.sbb-image__img') ?? null)
: (element as HTMLImageElement);

Check warning on line 11 in src/elements/core/testing/wait-for-image-ready.ts

View check run for this annotation

Codecov / codecov/patch

src/elements/core/testing/wait-for-image-ready.ts#L11

Added line #L11 was not covered by tests

if (!imgElement) {
throw new Error('img tag not found');
}

Check warning on line 15 in src/elements/core/testing/wait-for-image-ready.ts

View check run for this annotation

Codecov / codecov/patch

src/elements/core/testing/wait-for-image-ready.ts#L14-L15

Added lines #L14 - L15 were not covered by tests

if (!element.complete) {
await new Promise<void>((resolve, reject) => {
const timeout = setTimeout(() => reject('image loading timeout'), timeoutInMilliseconds);
element.addEventListener('load', () => {
clearTimeout(timeout);
resolve();

imgElement.decode().then(() => {
if (isSafari && element.localName === 'sbb-image') {
// On a test page this is only happening once (first time an image is loaded). Therefore, the impact is very small.
setTimeout(resolve, 100);

Check warning on line 26 in src/elements/core/testing/wait-for-image-ready.ts

View check run for this annotation

Codecov / codecov/patch

src/elements/core/testing/wait-for-image-ready.ts#L25-L26

Added lines #L25 - L26 were not covered by tests
} else {
resolve();
}
});
});

element.addEventListener('error', () => {
clearTimeout(timeout);
reject('image error');
});
});
} else {
await imgElement.decode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import '../../title.js';
import '../../image.js';

const imageUrl = import.meta.resolve('../../core/testing/assets/placeholder-image.png');
const imageBase64 = await loadAssetAsBase64(
import.meta.resolve('../../core/testing/assets/lucerne.png'),
);
const imageBase64 = await loadAssetAsBase64(imageUrl);

const images = [
{
Expand Down
4 changes: 1 addition & 3 deletions src/elements/lead-container/lead-container.visual.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ import '../title.js';
import './lead-container.js';

const leadImageUrl = import.meta.resolve('../core/testing/assets/placeholder-image.png');
const leadImageBase64 = await loadAssetAsBase64(
import.meta.resolve('../core/testing/assets/lucerne.png'),
);
const leadImageBase64 = await loadAssetAsBase64(leadImageUrl);

describe(`sbb-lead-container`, () => {
const wrapperStyles = { backgroundColor: `var(--sbb-color-milk)`, padding: '0' };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const template = ({
<sbb-teaser-product ?negative=${negative} image-alignment=${imageAlignment || nothing} href="#">
${slottedImg
? html`<img slot="image" src=${imageBase64} alt="" />`
: html`<sbb-image slot="image" image-src=${imageUrl}></sbb-image>`}
: html`<sbb-image slot="image" image-src=${imageUrl} skip-lqip></sbb-image>`}
${content(longContent)} ${showFooter ? footer() : nothing}
</sbb-teaser-product>
`;
Expand All @@ -82,21 +82,17 @@ describe('sbb-teaser-product', () => {
}
});
}

it(
`imageAlignment=before`,
visualDiffDefault.with(async (setup) => {
await setup.withFixture(
template({ imageAlignment: 'before', showFooter: true, slottedImg }),
);
await waitForImageReady(
setup.snapshotElement.querySelector(slottedImg ? 'img' : 'sbb-image')!,
);
}),
);
});
}

it(
`imageAlignment=before`,
visualDiffDefault.with(async (setup) => {
await setup.withFixture(template({ imageAlignment: 'before', showFooter: true }));
await waitForImageReady(setup.snapshotElement.querySelector('sbb-image')!);
}),
);

it(
'no footer',
visualDiffDefault.with(async (setup) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ export const startPlaywrightServerCommand = [
`${port}:${port}`,
'--rm',
'--init',
'--add-host=host.containers.internal=host-gateway',
containerRuntime === 'docker' ? '--add-host=host.containers.internal=host-gateway' : undefined,
'--workdir=/home/pwuser',
'--entrypoint=/bin/sh',
containerImage,
`-c`,
`npx -y playwright@${playwrightVersion} run-server --port ${port} --host 0.0.0.0`,
];
].filter(Boolean);

// Reference: https://github.com/remcovaes/web-test-runner-vite-plugin
export function containerPlaywrightBrowserPlugin(): TestRunnerPlugin {
Expand Down
Loading