Files
clean-architecture-frontend…/tests/component/app-shell.test.tsx
T
DongHyeonka b9a8a1a6c2 test(presentation): 건너뛰기 링크·로딩 발표·사이드바 이름을 테스트로 지킨다 (08-09)
문제: 접근성 기본 장치 일부를 지키는 테스트가 없었다. 리뷰 변이 중
M3(건너뛰기 링크 대상을 #content로), M4(LoadingSurface의 aria-live
제거), M5(상태 화면 제목을 <p>로), M6(사이드바 aria-label 제거)가
component 211개를 모두 통과했다. e2e의 axe는 WCAG 태그만 돌려
best-practice인 skip-link 규칙이 빠지고, 건너뛰기 링크를 누르는 e2e도
없었다.

고른 방법: 리뷰의 최소 개선 방향 그대로 단언을 더했다.
- app-shell.test.tsx: 건너뛰기 링크 href가 main의 id와 같고, 이름 있는
  complementary 안에 주요 탐색이 있다.
- async-surface.test.tsx: LoadingSurface가 aria-busy와 aria-live="polite"를
  가진다.
- accessibility.spec.ts: 건너뛰기 링크 Enter 뒤 포커스가 main 안에 있다.
- M5는 08-05 커밋의 제목 수준 테스트가 이미 잡는다.
버린 대안: axe에 best-practice 태그를 켜기 — 이번 범위 밖의 위반까지
한꺼번에 게이트가 된다.

위험·한계: 사이드바 이름 "사이드바"는 위치를 말하는 이름이라 리뷰가
관찰로 남겼다. 테스트는 지금 이름을 고정하므로 이름을 바꾸면 테스트도
같이 바꾼다. M3 변이 빌드에서 e2e는 공용 실행 잠금이 길게 잡혀
돌리지 못했다(component 테스트가 M3를 잡는다).

검증(테스트만 추가라 RED는 변이로 확인):
- M3: app-shell landmarks 테스트 실패(1 failed | 221 passed)
- M4: loading surface 테스트 실패(1 failed | 221 passed)
- M5(state-surfaces.tsx 제목을 <p>로): 08-05 테스트 2건 실패
- M6: app-shell landmarks 테스트 실패(1 failed | 221 passed)
- 변이 없음: tests/component 222/222, 새 e2e chromium·firefox·webkit 3/3.
  tsc test 통과, eslint 통과.

Refs: docs/reviews/2026-11-01-maturity-review/08-presentation.md 08-09
2026-11-01 23:20:31 +09:00

133 lines
4.4 KiB
TypeScript

// @vitest-environment jsdom
import { render, screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { ApplicationProvider } from "../../src/presentation/providers/application-provider.tsx";
import { PreferenceProviders } from "../../src/presentation/providers/preference-providers.tsx";
import { AppRouter } from "../../src/presentation/routes/app-router.tsx";
import { createTestApplication } from "../helpers/create-test-application.ts";
/**
* jsdom has no showModal/close and no modal inertness. Browsers ignore
* focus() outside an open modal dialog, which is what made the route focus
* fail while the drawer was still open; this reproduces just that.
*/
function emulateModalDialogs() {
const dialogProto = HTMLDialogElement.prototype;
// user-event redefines focus as a getter, so patch and restore descriptors.
const focusDescriptor = Object.getOwnPropertyDescriptor(
HTMLElement.prototype,
"focus",
)!;
const nativeFocus = HTMLElement.prototype.focus;
let modal: HTMLDialogElement | null = null;
Object.defineProperties(dialogProto, {
open: {
configurable: true,
get(this: HTMLDialogElement) {
return this.hasAttribute("open");
},
},
showModal: {
configurable: true,
value(this: HTMLDialogElement) {
this.setAttribute("open", "");
modal = this;
},
},
close: {
configurable: true,
value(this: HTMLDialogElement) {
this.removeAttribute("open");
if (modal === this) modal = null;
},
},
});
Object.defineProperty(HTMLElement.prototype, "focus", {
configurable: true,
value(this: HTMLElement, options?: FocusOptions) {
if (modal && !modal.contains(this)) return;
nativeFocus.call(this, options);
},
});
return () => {
for (const key of ["open", "showModal", "close"]) {
Reflect.deleteProperty(dialogProto, key);
}
Object.defineProperty(HTMLElement.prototype, "focus", focusDescriptor);
};
}
let restoreDialogs: () => void = () => {};
beforeEach(() => {
restoreDialogs = emulateModalDialogs();
});
afterEach(() => restoreDialogs());
function renderShell() {
window.history.pushState({}, "", "/");
return render(
<ApplicationProvider application={createTestApplication()}>
<PreferenceProviders>
<AppRouter />
</PreferenceProviders>
</ApplicationProvider>,
);
}
describe("app shell mobile navigation drawer", () => {
it("hands focus to the new page when a drawer link navigates", async () => {
const user = userEvent.setup();
renderShell();
const menuButton = await screen.findByRole("button", { name: "메뉴" });
await user.click(menuButton);
const drawer = screen.getByRole("dialog", { name: "메뉴" });
await user.click(within(drawer).getByRole("link", { name: "UI 구성요소" }));
const heading = await screen.findByRole(
"heading",
{ name: "UI 구성요소", level: 1 },
);
await waitFor(() => expect(heading).toHaveFocus());
expect(menuButton).toHaveAttribute("aria-expanded", "false");
// The drawer's deferred focus restore must not pull focus back to the
// menu button after the page took it.
await new Promise((resolve) => setTimeout(resolve, 20));
expect(heading).toHaveFocus();
});
it("still returns focus to the menu button when Escape closes the drawer", async () => {
const user = userEvent.setup();
renderShell();
const menuButton = await screen.findByRole("button", { name: "메뉴" });
await user.click(menuButton);
expect(screen.getByRole("dialog", { name: "메뉴" })).toHaveAttribute(
"open",
);
await user.keyboard("{Escape}");
await waitFor(() => expect(menuButton).toHaveFocus());
});
});
describe("app shell landmarks (08-09)", () => {
it("points the skip link at the main region and names the sidebar", async () => {
renderShell();
const main = await screen.findByRole("main");
expect(main.id).not.toBe("");
expect(
screen.getByRole("link", { name: "본문으로 건너뛰기" }),
).toHaveAttribute("href", `#${main.id}`);
const sidebar = screen.getByRole("complementary", { name: "사이드바" });
expect(
within(sidebar).getByRole("navigation", { name: "주요 탐색" }),
).toBeVisible();
});
});