Files
clean-architecture-frontend…/tests/component/design-system-platform.test.tsx
T
DongHyeonka ba98048b29 test(design-system): Popover 포커스 반환 테스트가 전제를 먼저 만든다 (10-02)
문제: "Escape 뒤 포커스가 trigger로 돌아온다" 단언은 trigger를 클릭한
채로 Escape를 누른다. Popover는 열 때 포커스를 내용으로 옮기지 않으므로
포커스는 처음부터 끝까지 trigger에 있다. overlays.tsx의 Escape 처리에서
`triggerRef.current?.focus()`를 지워도(M15) 관련 테스트가 모두 통과한다.
내용 안의 버튼으로 Tab한 뒤 Escape를 누르는 키보드 사용자는 포커스를
잃어도 아무 테스트가 모른다(WCAG 2.4.3).

고른 방법: 열고 나서 Tab으로 내용 안의 "내부 작업" 버튼에 포커스를
옮기고, 그 버튼에 포커스가 있는지 먼저 단언한다. 그 뒤 Escape와 trigger
포커스 단언은 그대로 둔다. 리뷰 최소 개선안 그대로다.

위험·한계: 바깥 클릭으로 닫을 때의 포커스 반환(closeOutside)은 이
테스트가 보지 않는다. 이번 발견 범위 밖이다.

검증:
- RED(M15 적용): 변경 전 테스트 파일은 통과, 새 테스트는
  "expect(element).toHaveFocus()" 실패(design-system-platform.test.tsx
  trigger 단언).
- GREEN: 원본 overlays.tsx로 되돌리고 related overlays.tsx --run —
  23파일 152 passed.
- eslint(바꾼 파일), tsc -p tsconfig.test.json 통과.

Refs: docs/reviews/2026-11-01-maturity-review/10-tests-fast-layers.md 10-02
2026-11-01 23:18:45 +09:00

343 lines
10 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// @vitest-environment jsdom
import {
act,
fireEvent,
render,
screen,
waitFor,
within,
} from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { useState } from "react";
import { describe, expect, it, vi } from "vitest";
import {
Button,
Checkbox,
DataTable,
Drawer,
IconButton,
Menu,
MenuIcon,
Pagination,
Popover,
RadioGroup,
SearchField,
Switch,
Tabs,
ToastProvider,
useToast,
} from "../../src/presentation/design-system/index.ts";
describe("SearchField", () => {
it("returns focus to the input when the clear button removes itself", async () => {
const user = userEvent.setup();
function ControlledSearch() {
const [value, setValue] = useState("query");
return (
<SearchField
clearLabel="Clear search"
label="Search"
onChange={(event) => setValue(event.currentTarget.value)}
onClear={() => setValue("")}
value={value}
/>
);
}
render(<ControlledSearch />);
await user.click(screen.getByRole("button", { name: "Clear search" }));
const input = screen.getByRole("searchbox", { name: "Search" });
expect(input).toHaveValue("");
expect(input).toHaveFocus();
expect(
screen.queryByRole("button", { name: "Clear search" }),
).not.toBeInTheDocument();
});
});
describe("design-system platform interactions", () => {
it("keeps decorative icons out of the accessibility tree and names icon actions", () => {
render(
<>
<MenuIcon />
<IconButton accessibleName="탐색 열기">
<MenuIcon />
</IconButton>
</>,
);
expect(screen.getByRole("button", { name: "탐색 열기" })).toBeVisible();
expect(screen.queryByRole("img")).not.toBeInTheDocument();
});
it("supports native choices, indeterminate state, radio arrows and switches", async () => {
const user = userEvent.setup();
const onRadio = vi.fn();
const onSwitch = vi.fn();
render(
<>
<Checkbox indeterminate label="일부 선택" />
<RadioGroup
label="밀도"
name="density"
onChange={onRadio}
options={[
{ value: "normal", label: "보통" },
{ value: "compact", label: "조밀" },
]}
value="normal"
/>
<Switch
checked={false}
label="알림"
onChange={onSwitch}
/>
</>,
);
const checkbox = screen.getByRole("checkbox", { name: "일부 선택" });
expect(checkbox).toBePartiallyChecked();
const normal = screen.getByRole("radio", { name: "보통" });
normal.focus();
await user.keyboard("{ArrowRight}");
expect(onRadio).toHaveBeenCalledWith("compact");
await user.click(screen.getByRole("switch", { name: "알림" }));
expect(onSwitch).toHaveBeenCalledWith(true);
});
it("moves through a menu, executes once, dismisses and restores focus", async () => {
const user = userEvent.setup();
const inspect = vi.fn();
render(
<Menu
items={[
{ id: "open", label: "열기", onSelect: vi.fn() },
{ id: "inspect", label: "검사", onSelect: inspect },
]}
triggerLabel="작업"
/>,
);
const trigger = screen.getByRole("button", { name: "작업" });
trigger.focus();
await user.keyboard("{ArrowDown}");
expect(screen.getByRole("menuitem", { name: "열기" })).toHaveFocus();
await user.keyboard("{ArrowDown}{Enter}");
expect(inspect).toHaveBeenCalledOnce();
await waitFor(() => expect(trigger).toHaveFocus());
expect(screen.queryByRole("menu")).not.toBeInTheDocument();
});
it("supports manual tab activation with arrow-key roving focus", async () => {
const user = userEvent.setup();
render(
<Tabs
activation="manual"
label="계층"
tabs={[
{ id: "tokens", label: "토큰", panel: "토큰 내용" },
{ id: "patterns", label: "패턴", panel: "패턴 내용" },
]}
/>,
);
const tokenTab = screen.getByRole("tab", { name: "토큰" });
tokenTab.focus();
await user.keyboard("{ArrowRight}");
const patternTab = screen.getByRole("tab", { name: "패턴" });
expect(patternTab).toHaveFocus();
expect(patternTab).toHaveAttribute("aria-selected", "false");
await user.keyboard("{Enter}");
expect(patternTab).toHaveAttribute("aria-selected", "true");
expect(screen.getByRole("tabpanel", { name: "패턴" })).toHaveTextContent(
"패턴 내용",
);
});
it("uses a modal drawer and restores focus after Escape", async () => {
const user = userEvent.setup();
function Harness() {
const [open, setOpen] = useState(false);
return (
<>
<Button onClick={() => setOpen(true)}>탐색 열기</Button>
<Drawer onClose={() => setOpen(false)} open={open} title="탐색">
<a href="/target">대상</a>
</Drawer>
</>
);
}
render(<Harness />);
const trigger = screen.getByRole("button", { name: "탐색 열기" });
await user.click(trigger);
expect(screen.getByRole("dialog", { name: "탐색" })).toHaveAttribute("open");
await user.keyboard("{Escape}");
await waitFor(() => expect(trigger).toHaveFocus());
});
it("does not pull back focus that moved out of a closed drawer", async () => {
// A route change focuses the new page in the same tick the drawer closes;
// the drawer's deferred restore must not take that focus back.
const user = userEvent.setup();
function Harness() {
const [open, setOpen] = useState(false);
return (
<>
<Button onClick={() => setOpen(true)}>탐색 열기</Button>
<h1 tabIndex={-1}>새 페이지</h1>
<Drawer onClose={() => setOpen(false)} open={open} title="탐색">
<Button
onClick={() => {
setOpen(false);
queueMicrotask(() =>
screen.getByRole("heading", { name: "새 페이지" }).focus(),
);
}}
>
이동
</Button>
</Drawer>
</>
);
}
render(<Harness />);
await user.click(screen.getByRole("button", { name: "탐색 열기" }));
await user.click(screen.getByRole("button", { name: "이동" }));
await act(() => new Promise((resolve) => setTimeout(resolve, 20)));
expect(screen.getByRole("heading", { name: "새 페이지" })).toHaveFocus();
});
it("bounds the toast queue and collapses duplicate IDs", async () => {
const user = userEvent.setup();
function Harness() {
const toast = useToast();
return (
<>
<Button
onClick={() =>
toast.push({ id: "same", title: "저장됨", durationMs: 60_000 })
}
>
중복
</Button>
<Button
onClick={() => {
for (let index = 0; index < 5; index += 1) {
toast.push({
id: `toast-${index}`,
title: `알림 ${index}`,
durationMs: 60_000,
});
}
}}
>
다섯 개
</Button>
</>
);
}
render(
<ToastProvider limit={3}>
<Harness />
</ToastProvider>,
);
await user.click(screen.getByRole("button", { name: "중복" }));
await user.click(screen.getByRole("button", { name: "중복" }));
expect(screen.getByText("×2")).toBeVisible();
await user.click(screen.getByRole("button", { name: "다섯 개" }));
const region = screen.getByRole("region", { name: "알림" });
expect(within(region).getAllByRole("article")).toHaveLength(3);
expect(within(region).queryByText("알림 0")).not.toBeInTheDocument();
expect(within(region).getByText("알림 4")).toBeVisible();
});
it("pauses toast timeout while the user is interacting", () => {
vi.useFakeTimers();
function Harness() {
const toast = useToast();
return (
<Button
onClick={() =>
toast.push({ id: "timed", title: "시간 제한", durationMs: 1000 })
}
>
표시
</Button>
);
}
render(
<ToastProvider>
<Harness />
</ToastProvider>,
);
fireEvent.click(screen.getByRole("button", { name: "표시" }));
const item = screen.getByRole("article");
fireEvent.mouseEnter(item);
act(() => vi.advanceTimersByTime(2000));
expect(item).toBeVisible();
fireEvent.mouseLeave(item);
act(() => vi.advanceTimersByTime(1000));
expect(screen.queryByText("시간 제한")).not.toBeInTheDocument();
vi.useRealTimers();
});
it("provides dismissible popover and data/navigation patterns", async () => {
const user = userEvent.setup();
const onPage = vi.fn();
render(
<>
<Popover triggerLabel="설명 열기">
<Button>내부 작업</Button>
</Popover>
<DataTable
caption="결과"
columns={[
{ id: "name", header: "이름", cell: (row) => row.name },
]}
empty="비어 있음"
rowKey={(row) => row.id}
rows={[{ id: "one", name: "첫 항목" }]}
/>
<Pagination
label="페이지"
nextLabel="다음"
onChange={onPage}
page={1}
pageCount={2}
pageLabel={(page) => `${page}페이지`}
previousLabel="이전"
/>
</>,
);
const popoverTrigger = screen.getByRole("button", { name: "설명 열기" });
await user.click(popoverTrigger);
expect(screen.getByRole("dialog")).toHaveTextContent("내부 작업");
// 10-02. Opening leaves focus on the trigger, so move it into the content
// first; otherwise the check below holds whatever Escape does.
await user.tab();
expect(screen.getByRole("button", { name: "내부 작업" })).toHaveFocus();
await user.keyboard("{Escape}");
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
expect(popoverTrigger).toHaveFocus();
expect(screen.getByRole("table", { name: "결과" })).toHaveTextContent(
"첫 항목",
);
// UI-08: the keyboard-scrollable wrapper is a named region, not an
// anonymous focus stop.
expect(screen.getByRole("region", { name: "결과" })).toHaveAttribute(
"tabindex",
"0",
);
await user.click(screen.getByRole("link", { name: "2페이지" }));
expect(onPage).toHaveBeenCalledWith(2);
});
});