mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-24 23:21:30 +00:00
54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
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();
|
|
});
|
|
});
|