test(browser): add regression coverage

This commit is contained in:
Peter Steinberger
2026-01-12 17:31:53 +00:00
parent eeca541dde
commit cf78d28d74
3 changed files with 132 additions and 1 deletions

View File

@@ -0,0 +1,53 @@
import type { Page } from "playwright-core";
import { describe, expect, it, vi } from "vitest";
import { ensurePageState, refLocator } from "./pw-session.js";
function fakePage(): Page {
const handlers = new Map<string, Array<(...args: unknown[]) => void>>();
const on = vi.fn((event: string, cb: (...args: unknown[]) => void) => {
const list = handlers.get(event) ?? [];
list.push(cb);
handlers.set(event, list);
return undefined as unknown;
});
const getByRole = vi.fn(() => ({ nth: vi.fn(() => ({ ok: true })) }));
const frameLocator = vi.fn(() => ({
getByRole: vi.fn(() => ({ nth: vi.fn(() => ({ ok: true })) })),
}));
const locator = vi.fn(() => ({ nth: vi.fn(() => ({ ok: true })) }));
return {
on,
getByRole,
frameLocator,
locator,
} as unknown as Page;
}
describe("pw-session refLocator", () => {
it("uses frameLocator for role refs when snapshot was scoped to a frame", () => {
const page = fakePage();
const state = ensurePageState(page);
state.roleRefs = { e1: { role: "button", name: "OK" } };
state.roleRefsFrameSelector = "iframe#main";
refLocator(page, "e1");
expect(
page.frameLocator as unknown as ReturnType<typeof vi.fn>,
).toHaveBeenCalledWith("iframe#main");
});
it("uses page getByRole for role refs by default", () => {
const page = fakePage();
const state = ensurePageState(page);
state.roleRefs = { e1: { role: "button", name: "OK" } };
refLocator(page, "e1");
expect(
page.getByRole as unknown as ReturnType<typeof vi.fn>,
).toHaveBeenCalled();
});
});