Files
clean-architecture-frontend…/tests/component/app-shell.test.tsx
DongHyeonkaandClaude Opus 5.5 ddf74ed6e9 fix(presentation): keep preferences above the scope fence, dispose routers (R-8.3)
LocaleProvider and ThemeProvider lived inside AppRouter, below the
server-state scope fence (and below QueryClientProvider keyed by
generation). A session change (FENCED -> READY) unmounted and remounted
them, so their live state was thrown away and re-read from storage. The
browser router was created in useMemo and never disposed, so every
remount (and StrictMode's double useMemo) left a popstate listener behind.

- New presentation/providers/preference-providers.tsx: persisted
  LocaleProvider + ThemeProvider (the wiring T45 added to AppRouter).
- bootstrap/runtime-application.tsx: ApplicationProvider >
  PreferenceProviders > ServerStateGenerationProvider > AppRouter, so the
  fence only remounts session-scoped UI.
- AppRouter no longer mounts Locale/Theme. It creates the router in the
  effect that disposes it: a disposed router never listens to history
  again, so useMemo + cleanup would break back/forward under StrictMode's
  replayed mount.
- Tests that mount AppRouter directly now wrap it in PreferenceProviders
  (router, chunk-recovery-boundary, app-shell, product-feature-switch,
  locale-platform, reference-feature reference-page).

Verified: RED "keeps locale state across a session scope fence and
disposes routers" (RuntimeApplication, demo sign-in, storage cleared after
choosing en-US) came back in Korean before and passes after; with the
providers hoisted but no dispose it fails on popstate listeners (4 added,
0 removed). check:architecture, check:types:app, check:types:test, lint,
test:component, test:reference-feature 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

118 lines
3.8 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 },
{ 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());
});
});