Files
fn_registry/frontend/functions/browser/pw_wait_predicate.test.ts
T
egutierrez 626d06327c feat(registry): add playwright capability group (6 TS browser fns)
New domain `browser` under frontend/functions/ with 6 Playwright helpers:
- pw_launch_browser: chromium + context + page bootstrap with storageState
  support and baseUrl navigation.
- pw_kanban_login: authenticates a Page against /api/auth/login; sets the
  kanban_session cookie via shared storageState; verifies login page no
  longer visible after navigation.
- pw_drag_drop: human-like pointer drag (mousedown + activateOffset +
  stepped move + mouseup) compatible with @dnd-kit/core's 8px activation
  threshold; supports hoverMs for time-based dropzones.
- pw_keyboard_sequence: ordered focus/type/press/wait steps for scripting
  realistic input flows (typing then arrow-key navigating autocompletes).
- pw_wait_predicate: thin wrapper over page.waitForFunction with friendlier
  defaults and custom error messages.
- pw_assert_class: poll-based assertion that a Locator has/lacks a CSS
  class within a timeout; useful for visual-state checks.

Each function ships with vitest tests (5-8 cases each) covering both happy
and error paths, plus self-documenting .md (Ejemplo + Cuando usarla +
Gotchas + frontmatter with params/output schema).

Adds frontend/functions/package.json with `"type": "module"` so consumers
can ESM-import the .ts files from anywhere in the registry (Playwright's
tsx loader respects nearest package.json).

Capability page docs/capabilities/playwright.md documents the group with
a canonical end-to-end example, frontiers, prerequisites, and gotchas.
Index updated.

First consumer (issue 0088): apps/kanban requester-input.spec.ts.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-14 12:57:30 +02:00

89 lines
2.5 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import type { JSHandle } from "playwright";
// --- mocks ------------------------------------------------------------------
const mockJsonValue = vi.fn();
const mockHandle: Partial<JSHandle> = {
jsonValue: mockJsonValue,
};
const mockPage = {
waitForFunction: vi.fn(),
};
// ---------------------------------------------------------------------------
beforeEach(() => {
vi.clearAllMocks();
mockPage.waitForFunction.mockResolvedValue(mockHandle);
mockJsonValue.mockResolvedValue(true);
});
import { pw_wait_predicate } from "./pw_wait_predicate";
// ---------------------------------------------------------------------------
describe("pw_wait_predicate", () => {
it("defaults applied", async () => {
const predicate = () => document.querySelector(".ready") !== null;
await pw_wait_predicate(mockPage as never, predicate);
expect(mockPage.waitForFunction).toHaveBeenCalledWith(
predicate,
undefined, // arg default
{ timeout: 5000, polling: 100 },
);
});
it("arg passed through", async () => {
const predicate = (sel: unknown) =>
document.querySelectorAll(sel as string).length > 0;
const arg = ".border-red";
await pw_wait_predicate(mockPage as never, predicate, {
timeoutMs: 2000,
pollMs: 50,
arg,
});
expect(mockPage.waitForFunction).toHaveBeenCalledWith(
predicate,
arg,
{ timeout: 2000, polling: 50 },
);
});
it("jsonValue returned", async () => {
const expected = { count: 3 };
mockJsonValue.mockResolvedValue(expected);
const result = await pw_wait_predicate(mockPage as never, "() => true");
expect(result).toEqual(expected);
expect(mockJsonValue).toHaveBeenCalledOnce();
});
it("message wraps timeout", async () => {
const originalError = new Error("Timeout 5000ms exceeded");
mockPage.waitForFunction.mockRejectedValue(originalError);
await expect(
pw_wait_predicate(mockPage as never, "() => false", {
message: "no red-bordered card appeared",
}),
).rejects.toThrow("no red-bordered card appeared: Timeout 5000ms exceeded");
});
it("error passthrough without message", async () => {
const originalError = new Error("Something went wrong");
mockPage.waitForFunction.mockRejectedValue(originalError);
await expect(
pw_wait_predicate(mockPage as never, "() => false"),
).rejects.toThrow("Something went wrong");
});
});