Skip to content

Commit 93d777f

Browse files
committed
test: serve custom block images from local fixtures
1 parent 8a5d6ad commit 93d777f

5 files changed

Lines changed: 51 additions & 3 deletions

File tree

‎tests/src/end-to-end/customblocks/customblocks.test.tsx‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import ReactCustomBlocksApp from "@examples/06-custom-schema/react-custom-blocks/src/App";
22
import VanillaCustomBlocksApp from "@examples/vanilla-js/react-vanilla-custom-blocks/src/App";
3-
import { describe, test } from "vite-plus/test";
3+
import { afterAll, beforeAll, describe, expect, test } from "vite-plus/test";
44
import { render } from "vitest-browser-react";
5-
import { userEvent } from "../../utils/context.js";
5+
import { commands, userEvent } from "../../utils/context.js";
66
import { EDITOR_SELECTOR } from "../../utils/const.js";
77
import {
88
compareDocToSnapshot,
@@ -12,6 +12,22 @@ import {
1212
} from "../../utils/editor.js";
1313

1414
describe("Check custom block functionality", () => {
15+
const imageCommands = commands as unknown as {
16+
mockCustomBlockImages(action: "install" | "remove"): Promise<void>;
17+
};
18+
beforeAll(() => imageCommands.mockCustomBlockImages("install"));
19+
afterAll(() => imageCommands.mockCustomBlockImages("remove"));
20+
21+
async function waitForImage() {
22+
await expect
23+
.poll(
24+
() =>
25+
document.querySelector<HTMLImageElement>("img.simple-image")
26+
?.naturalWidth ?? 0,
27+
)
28+
.toBeGreaterThan(0);
29+
}
30+
1531
test("Should be able to interactively update vanilla custom blocks", async () => {
1632
await render(<VanillaCustomBlocksApp />);
1733
await waitForSelector(EDITOR_SELECTOR);
@@ -20,6 +36,7 @@ describe("Check custom block functionality", () => {
2036
await sleep(500);
2137

2238
await compareDocToSnapshot("vanillaInteractivity");
39+
await waitForImage();
2340
await expectElement(document.body).toMatchScreenshot(
2441
"vanilla-interactivity",
2542
);
@@ -33,6 +50,7 @@ describe("Check custom block functionality", () => {
3350
await sleep(500);
3451

3552
await compareDocToSnapshot("reactInteractivity");
53+
await waitForImage();
3654
await expectElement(document.body).toMatchScreenshot("react-interactivity");
3755
});
3856
});
2.75 KB
Loading
10.5 KB
Loading
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { fileURLToPath } from "node:url";
2+
import type { BrowserCommandContext } from "vite-plus/test/node";
3+
4+
// Preserve the examples' URLs/document snapshots, but serve the exact image
5+
// bytes locally: a screenshot must not depend on placehold.co being available.
6+
export async function mockCustomBlockImages(
7+
{ context }: BrowserCommandContext,
8+
action: "install" | "remove",
9+
) {
10+
for (const filename of ["800x540.png", "332x322.jpg"]) {
11+
const url = `https://placehold.co/${filename}`;
12+
if (action === "remove") {
13+
await context.unroute(url);
14+
} else {
15+
const path = fileURLToPath(
16+
new URL(
17+
`../end-to-end/customblocks/fixtures/${filename}`,
18+
import.meta.url,
19+
),
20+
);
21+
await context.route(url, (route) => route.fulfill({ path }));
22+
}
23+
}
24+
}

‎tests/vite.config.browser.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { configDefaults, defineConfig, type UserConfig } from "vite-plus";
55
import { playwright } from "vite-plus/test/browser/providers/playwright";
66
import { positionalMouse } from "./src/utils/positionalMouse.js";
77
import { imeComposition } from "./src/utils/imeComposition.js";
8+
import { mockCustomBlockImages } from "./src/utils/mockCustomBlockImages.js";
89

910
// For the desktop instances: end-to-end/mobile runs only in the "android"
1011
// instance. An instance-level `exclude` replaces the resolved base exclude
@@ -165,7 +166,12 @@ export default defineConfig(
165166
// still show in the HTML report (errors + stack traces don't depend
166167
// on these shots), so disable them. See `e2e:report` to view.
167168
screenshotFailures: false,
168-
commands: { positionalMouse, imeComposition, restoreTouchEmulation },
169+
commands: {
170+
positionalMouse,
171+
imeComposition,
172+
restoreTouchEmulation,
173+
mockCustomBlockImages,
174+
},
169175
instances: [
170176
{
171177
browser: "chromium",

0 commit comments

Comments
 (0)