test(browser): add regression coverage
This commit is contained in:
53
src/browser/pw-session.test.ts
Normal file
53
src/browser/pw-session.test.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user