Files
clean-architecture-frontend…/tests/component/app-shell.test.tsx
T
DongHyeonkaandClaude Opus 5.5 b1a8269d65 fix(presentation): land focus on the new page after drawer navigation (R-8.6)
Navigating from the mobile drawer left focus on the menu button. The
route's main.focus() ran while the modal drawer was still open (the page
is inert then, so the call is ignored), the shell closed the drawer one
commit later, and the Dialog's deferred restore moved focus to the menu
button.

- app-shell.tsx: drawer links close the drawer on click, so the modal is
  closed before the route commits and the route can take focus.
- core.tsx (Dialog): the deferred restore only runs while focus is still
  unclaimed (body/null or inside the dialog). React usually commits the
  navigation before setTimeout(0) fires in a browser; without this the
  restore would take focus back from the new page. Escape still returns
  focus to the trigger, because focus is inside the dialog then.

Verified: new tests/component/app-shell.test.tsx emulates modal
inertness (jsdom has no showModal/inert). RED "hands focus to the new
page when a drawer link navigates" ended on the menu button before and
passes after; "still returns focus to the menu button when Escape closes
the drawer" passes. New Dialog test "does not pull back focus that moved
out of a closed drawer" fails without the core.tsx guard. check:architecture,
check:types:app, check:types:test, lint, test:component pass; test:unit
passes except the pre-existing removal-fixture test (needs generated
artifacts/ files absent from a fresh worktree).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 12:39:51 +09:00

115 lines
3.7 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 { 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()}>
<AppRouter />
</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 },
{ timeout: 5000 },
);
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());
});
});