Repository navigation
Expand file tree
/
Copy pathfixture.ts
More file actions
67 lines (60 loc) · 2.19 KB
/
Copy pathfixture.ts
File metadata and controls
67 lines (60 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import path from "node:path";
import { test as base, expect, type Page } from "@playwright/test";
const fixturesDirectory = path.resolve(import.meta.dirname, "fixtures");
export const test = base.extend({
page: async ({ page }, use, testInfo) => {
const fixture = path
.relative(fixturesDirectory, path.dirname(testInfo.file))
.split(path.sep)
.join("/");
const errors: string[] = [];
page.on("pageerror", (error) => errors.push(error.message));
const response = await page.goto(`/${fixture}/`);
expect(response).not.toBeNull();
expect(response?.ok(), `loading ${response?.url()}`).toBe(true);
await expect(
page.getByRole("heading", { name: "An error occurred" }),
"SQL fixture rendered without errors",
).toHaveCount(0);
await expect(
page.locator("[data-pre-init]"),
"component initialization",
).toHaveCount(0);
await use(page);
expect(errors, "uncaught browser errors").toEqual([]);
},
});
export type { ConsoleMessage, Page } from "@playwright/test";
export { expect };
/** Insert server-rendered SQL output, then announce it like a custom fragment consumer. */
export async function loadFragment(
page: Page,
url: string,
root: string | null = "main",
parent = "main",
) {
await page.evaluate(
async ({ url, root, parent }) => {
const request = new URL(url, location.href);
request.searchParams.set("_sqlpage_embed", "1");
const response = await fetch(request);
if (!response.ok)
throw new Error(`Fragment request failed: ${response.status}`);
const fragment = document.createElement("template");
fragment.innerHTML = await response.text();
const destination = document.querySelector(parent);
if (!destination)
throw new Error(`Missing fragment destination: ${parent}`);
destination.append(fragment.content);
if (root) {
const target =
root === "document" ? document : document.querySelector(root);
if (!target) throw new Error(`Missing fragment root: ${root}`);
target.dispatchEvent(
new CustomEvent("fragment-loaded", { bubbles: true }),
);
}
},
{ url, root, parent },
);
}