Files
DongHyeonka 8bcecabc40 fix(a11y): 처리 중인 세션·상태 화면 버튼이 꺼지지 않고 포커스를 지킨다
셸 헤더의 세션 버튼과 로그인 안내(StatusPage)의 로그인·복구 버튼이 처리 중에
`disabled`가 된다. 템플릿 action API(`PageActionDefinition`)에는 `pending`이
없어 상태 화면은 끄는 길밖에 없었다. 꺼진 버튼은 키보드 포커스를 잃는다.
키보드로 "로그인 시작"을 누르고 sign-in이 실패하면 실패 알림이 뜰 때 포커스는
이미 body에 있다(결함 패턴 C11). 모든 파생 서비스의 셸이 복제한다. 같은
모양으로 Pagination의 「이전」을 2쪽에서 누르면 1쪽이 되며 그 버튼이 꺼져
포커스가 떨어진다.

고른 방법: `PageActionDefinition` 버튼에 `pending`·`pendingLabel`을 더해
`Button`으로 넘긴다(추가만). 라우터 상태 화면과 셸 세션 버튼은 `disabled`
대신 `pending`을 쓴다. 버튼은 켜진 채 `aria-busy="true"`이고, 처리 중에 그 버튼이나
다른 세션 버튼(셸과 상태 화면)을 누르면 `useSessionAction().run`이 무시한다.
`SessionProvider`가 세션 버튼 모두에 잠금 하나를 준다(중복 실행을
`disabled`에 기대지 않는다). Pagination 끝 버튼은 `aria-disabled`와 누름 무시로 바꾸고, 같은
모양(`.ui-button[aria-disabled="true"]`)으로 그린다. 버린 대안: `Button`의
`pending`이 클릭을 삼키게 하기(폼은 이미 자기 `pendingRef`로 막고, 공개
primitive의 동작이 바뀐다).

위험·한계: 다운스트림이 `PageActionDefinition`에 `disabled: pending`을 넘기고
있으면 그대로 동작하지만 포커스를 잃는다. `pending`으로 옮기면 된다. jsdom은
꺼진 버튼의 포커스를 떨어뜨리지 않아 component 시험은 `disabled`가 아님과
`aria-busy`, 실패 뒤 포커스를 본다.

검증: `tests/component/route-status-surfaces.test.tsx` 세 사례와
`tests/component/pagination.test.tsx` 끝 버튼 두 사례가 고치기 전 코드에서
실패하고 고친 뒤 통과한다(desktop, PR 본문).

Refs: 1112/05-03, 1112/04-05
2026-11-13 19:03:18 +09:00

175 lines
5.1 KiB
TypeScript

// @vitest-environment jsdom
import { fireEvent, render, screen, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { useState } from "react";
import { describe, expect, it, vi } from "vitest";
import { Pagination } from "../../src/presentation/design-system/index.ts";
import { CollectionPage } from "../../src/presentation/templates/index.ts";
function pagination(page: number, pageCount: number) {
return (
<Pagination
label="Pages"
nextLabel="Next"
onChange={() => {}}
page={page}
pageCount={pageCount}
pageLabel={(candidate) => `Page ${candidate}`}
previousLabel="Previous"
/>
);
}
function pageLinks(): string[] {
return screen
.getAllByRole("link")
.map((link) => link.textContent ?? "");
}
describe("Pagination windowing", () => {
it("links the first, last and neighbouring pages only", () => {
render(pagination(50, 5000));
expect(pageLinks()).toEqual([
"Page 1",
"Page 49",
"Page 50",
"Page 51",
"Page 5000",
]);
expect(screen.getByRole("link", { name: "Page 50" })).toHaveAttribute(
"aria-current",
"page",
);
});
it("links a single skipped page instead of eliding it", () => {
render(pagination(3, 5000));
expect(pageLinks()).toEqual([
"Page 1",
"Page 2",
"Page 3",
"Page 4",
"Page 5000",
]);
});
it("keeps every page of a short range", () => {
render(pagination(1, 3));
expect(pageLinks()).toEqual(["Page 1", "Page 2", "Page 3"]);
});
});
describe("Pagination landmarks", () => {
it("is a navigation landmark on its own", () => {
render(pagination(1, 2));
expect(screen.getByRole("navigation", { name: "Pages" })).toBeVisible();
});
it("does not nest a navigation landmark inside the collection page slot", () => {
render(
<CollectionPage heading={{ title: "Collection" }} pagination={pagination(1, 2)}>
Results
</CollectionPage>,
);
const slot = screen.getByRole("navigation", { name: "페이지 탐색" });
expect(within(slot).queryAllByRole("navigation")).toHaveLength(0);
expect(within(slot).getByRole("group", { name: "Pages" })).toBeVisible();
});
});
// 08-12. A modified click is the browser's (new tab/window), not a page change.
describe("Pagination modified clicks", () => {
function renderWith(onChange: (page: number) => void) {
render(
<Pagination
label="Pages"
nextLabel="Next"
onChange={onChange}
page={1}
pageCount={3}
pageLabel={(candidate) => `Page ${candidate}`}
previousLabel="Previous"
/>,
);
return screen.getByRole("link", { name: "Page 2" });
}
it.each([
["Ctrl", { ctrlKey: true }],
["Cmd", { metaKey: true }],
["Shift", { shiftKey: true }],
["Alt", { altKey: true }],
])("leaves a %s click to the browser", (_, modifier) => {
const onChange = vi.fn();
const link = renderWith(onChange);
const notPrevented = fireEvent.click(link, { button: 0, ...modifier });
expect(notPrevented).toBe(true);
expect(onChange).not.toHaveBeenCalled();
});
it("changes the page on a plain primary click", () => {
const onChange = vi.fn();
const link = renderWith(onChange);
expect(fireEvent.click(link, { button: 0 })).toBe(false);
expect(onChange).toHaveBeenCalledWith(2);
});
});
// 1112/05-03. Reaching the first or last page must not take keyboard focus
// away from the edge button the user just pressed.
describe("Pagination edge buttons", () => {
function Controlled(props: Readonly<{ onChange(page: number): void }>) {
const [page, setPage] = useState(2);
return (
<Pagination
label="Pages"
nextLabel="Next"
onChange={(next) => {
props.onChange(next);
setPage(next);
}}
page={page}
pageCount={3}
pageLabel={(candidate) => `Page ${candidate}`}
previousLabel="Previous"
/>
);
}
it("keeps focus on previous when it reaches the first page and ignores further presses", async () => {
const user = userEvent.setup();
const onChange = vi.fn();
render(<Controlled onChange={onChange} />);
const previous = screen.getByRole("button", { name: "Previous" });
await user.click(previous);
expect(onChange).toHaveBeenCalledWith(1);
expect(previous).not.toBeDisabled();
expect(previous).toHaveAttribute("aria-disabled", "true");
expect(previous).toHaveFocus();
await user.keyboard("{Enter}");
expect(onChange).toHaveBeenCalledOnce();
});
it("marks next at the last page the same way", async () => {
const user = userEvent.setup();
const onChange = vi.fn();
render(<Controlled onChange={onChange} />);
const next = screen.getByRole("button", { name: "Next" });
await user.click(next);
expect(next).toHaveAttribute("aria-disabled", "true");
expect(next).toHaveFocus();
await user.click(next);
expect(onChange).toHaveBeenCalledOnce();
expect(screen.getByRole("button", { name: "Previous" })).not.toHaveAttribute(
"aria-disabled",
);
});
});