mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-08 12:32:43 +00:00
A spec that arms `page.route(...)` in a `beforeEach` and never drops the handler races its own teardown. The context closes, an async handler is still awaiting, its next call rejects with "Test ended.", and Playwright attributes that to the test that had just passed. With `retries: 2` plus `failOnFlakyTests`, which spec bites is a matter of timing. `e2e/setup/test.ts` re-exports Playwright's `test` extended with an auto fixture that unroutes every page and the context after each test, and clears the page event listeners a spec subscribes to; `expect` rides along so a spec needs one import. Auto means a spec author cannot forget it and does not have to know it exists. Every spec now takes `test` from there, and `e2e-route-cleanup-guard.test.ts` keeps the import boundary closed with an allowlist that carries its reason. The per-file `afterEach` on the cycle spec goes away — leaving both would read as not trusting the fixture. Not covered: a page inside a context a spec builds itself. Only `share-documents.spec.ts` does that, and it owns those contexts.
63 lines
2.0 KiB
TypeScript
63 lines
2.0 KiB
TypeScript
import { expect, test } from "./setup/test";
|
|
|
|
import { STORAGE_STATE_PATH } from "./setup/global-setup";
|
|
|
|
test.describe("mood timestamp capture", () => {
|
|
test.use({ storageState: STORAGE_STATE_PATH });
|
|
|
|
test("keeps Time visible while details are collapsed and submits the selected instant", async ({
|
|
page,
|
|
}) => {
|
|
let submitted: Record<string, unknown> | null = null;
|
|
|
|
await page.route("**/api/mood-entries*", async (route) => {
|
|
if (route.request().method() === "POST") {
|
|
submitted = route.request().postDataJSON() as Record<string, unknown>;
|
|
await route.fulfill({
|
|
status: 201,
|
|
contentType: "application/json",
|
|
body: JSON.stringify({ data: { id: "created-mood" }, error: null }),
|
|
});
|
|
return;
|
|
}
|
|
|
|
await route.fulfill({
|
|
status: 200,
|
|
contentType: "application/json",
|
|
body: JSON.stringify({
|
|
data: { entries: [], meta: { total: 0, limit: 50, offset: 0 } },
|
|
error: null,
|
|
}),
|
|
});
|
|
});
|
|
|
|
await page.goto("/mood", { waitUntil: "domcontentloaded" });
|
|
await page.getByRole("button", { name: "Add", exact: true }).click();
|
|
await page.getByRole("radio", { name: "Amazing" }).click();
|
|
|
|
const details = page.getByRole("button", { name: "Note & details" });
|
|
await expect(details).toHaveAttribute("aria-expanded", "false");
|
|
|
|
const timestamp = page.locator('[data-slot="date-time-field"]');
|
|
await expect(timestamp).toBeVisible();
|
|
const dateInput = timestamp.locator(
|
|
'[data-slot="date-field"] input[type="text"]',
|
|
);
|
|
const timeInput = timestamp.locator(
|
|
'[data-slot="time-field"] input[type="text"]',
|
|
);
|
|
await dateInput.fill("2026-01-15");
|
|
await dateInput.blur();
|
|
await timeInput.fill("09:45");
|
|
await timeInput.blur();
|
|
|
|
await page.getByRole("button", { name: "Save", exact: true }).click();
|
|
|
|
await expect.poll(() => submitted).not.toBeNull();
|
|
expect(submitted).toMatchObject({
|
|
mood: "SUPER_GUT",
|
|
moodLoggedAt: "2026-01-15T08:45:00.000Z",
|
|
});
|
|
});
|
|
});
|