// @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( , ); } 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(); }); });