Files
clean-architecture-frontend…/tests/e2e/reference-save.spec.ts
donghyeon-kaandClaude Opus 5.5 48b84951b8 test(e2e): assert reference data and cover save success and retry (11-06)
The route spec only checked that the protected screen opened, so a schema
violating 200 or a 503 still passed it. It now asserts the mocked row, and
a new spec saves once and retries after a 429 in a real browser.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 13:00:35 +09:00

101 lines
3.4 KiB
TypeScript

import type { Page, Route } from "@playwright/test";
import { expect, test } from "../support/browser/strict-browser-test.ts";
const CREATE_URL = "http://localhost:8080/api/reference-resources";
type CreateCall = Readonly<{ name: unknown; idempotencyKey: string | null }>;
async function openForm(page: Page) {
await page.route(`${CREATE_URL}?*`, (route) => route.fulfill({ json: [] }));
await page.goto("/examples/reference-resources/new");
await page.getByRole("button", { name: "로그인 시작" }).click();
await expect(
page.getByRole("heading", { name: "Reference resource 만들기" }),
).toBeVisible();
}
/** Answers each POST with the next responder and records what was sent. */
async function routeCreate(
page: Page,
responders: readonly ((route: Route) => Promise<void>)[],
): Promise<CreateCall[]> {
const calls: CreateCall[] = [];
await page.route(CREATE_URL, async (route) => {
const request = route.request();
if (request.method() !== "POST") return route.fallback();
calls.push({
name: (request.postDataJSON() as { name?: unknown }).name,
idempotencyKey: await request.headerValue("idempotency-key"),
});
const respond = responders[calls.length - 1];
if (!respond) throw new Error(`unexpected create call ${calls.length}`);
await respond(route);
});
return calls;
}
const created = (route: Route) =>
route.fulfill({
status: 201,
json: {
id: "browser-created",
name: "Saved in the browser",
createdAt: "2026-07-26T00:00:00.000Z",
},
});
test("saves a new reference resource through the production command path", async ({
page,
}) => {
await openForm(page);
const calls = await routeCreate(page, [created]);
await page
.getByRole("textbox", { name: /새 항목 이름/ })
.fill("Saved in the browser");
await page.getByRole("button", { name: "저장" }).click();
await expect(page.getByRole("status")).toHaveText("저장했습니다.");
expect(calls).toHaveLength(1);
expect(calls[0]?.name).toBe("Saved in the browser");
expect(calls[0]?.idempotencyKey).toBeTruthy();
});
test.describe("after a rate-limited save", () => {
// Chromium and WebKit log a 4xx fetch response to the console, and the
// client cancels the 429 body unread, which Chromium reports as aborted.
test.use({
allowedBrowserFailure:
/^console:error:.*status of 429|^requestfailed:POST:\/api\/reference-resources:net::ERR_ABORTED$/u,
});
test("keeps the input and saves on retry", async ({ page }) => {
await openForm(page);
const calls = await routeCreate(page, [
(route) =>
route.fulfill({
status: 429,
headers: { "retry-after": "1" },
json: { title: "Too many requests" },
}),
created,
]);
const name = page.getByRole("textbox", { name: /새 항목 이름/ });
await name.fill("Saved in the browser");
await page.getByRole("button", { name: "저장" }).click();
await expect(
page.getByText("요청이 많습니다. 잠시 후 다시 시도해 주세요."),
).toBeVisible();
await expect(name).toHaveValue("Saved in the browser");
await page.getByRole("button", { name: "다시 시도" }).click();
await expect(page.getByRole("status")).toHaveText("저장했습니다.");
expect(calls.map((call) => call.name)).toEqual([
"Saved in the browser",
"Saved in the browser",
]);
});
});