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>
163 lines
5.3 KiB
TypeScript
163 lines
5.3 KiB
TypeScript
// @vitest-environment jsdom
|
|
|
|
import { render, screen } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { useState } from "react";
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
Button,
|
|
Drawer,
|
|
Pagination,
|
|
Tabs,
|
|
} from "../../src/presentation/design-system/index.ts";
|
|
import {
|
|
LocaleProvider,
|
|
useLocale,
|
|
type SupportedLocale,
|
|
} from "../../src/presentation/i18n/index.ts";
|
|
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";
|
|
|
|
function LocaleHarness() {
|
|
const { direction, locale, message, setLocale } = useLocale();
|
|
const [drawerOpen, setDrawerOpen] = useState(false);
|
|
return (
|
|
<>
|
|
<label htmlFor="test-locale">{message("shell.locale")}</label>
|
|
<select
|
|
id="test-locale"
|
|
onChange={(event) =>
|
|
setLocale(event.currentTarget.value as SupportedLocale)
|
|
}
|
|
value={locale}
|
|
>
|
|
<option value="ko-KR">한국어</option>
|
|
<option value="en-US">English</option>
|
|
<option value="ar-EG">RTL</option>
|
|
</select>
|
|
<output>{`${locale}:${direction}`}</output>
|
|
<Button onClick={() => setDrawerOpen(true)}>
|
|
{message("shell.menu")}
|
|
</Button>
|
|
<Drawer
|
|
closeLabel={message("shell.closeMenu")}
|
|
onClose={() => setDrawerOpen(false)}
|
|
open={drawerOpen}
|
|
title={message("shell.menu")}
|
|
>
|
|
content
|
|
</Drawer>
|
|
</>
|
|
);
|
|
}
|
|
|
|
describe("locale provider runtime", () => {
|
|
it("switches copy and synchronizes the document language and direction", async () => {
|
|
const user = userEvent.setup();
|
|
render(
|
|
<LocaleProvider>
|
|
<LocaleHarness />
|
|
</LocaleProvider>,
|
|
);
|
|
|
|
expect(document.documentElement).toHaveAttribute("lang", "ko-KR");
|
|
await user.selectOptions(screen.getByLabelText("언어"), "en-US");
|
|
expect(screen.getByLabelText("Language")).toHaveValue("en-US");
|
|
expect(document.documentElement).toHaveAttribute("lang", "en-US");
|
|
expect(document.documentElement).toHaveAttribute("dir", "ltr");
|
|
|
|
await user.selectOptions(screen.getByLabelText("Language"), "ar-EG");
|
|
expect(document.documentElement).toHaveAttribute("lang", "ar-EG");
|
|
expect(document.documentElement).toHaveAttribute("dir", "rtl");
|
|
await user.click(screen.getByRole("button", { name: "Menu" }));
|
|
expect(screen.getByRole("dialog", { name: "Menu" })).toHaveAttribute(
|
|
"open",
|
|
);
|
|
});
|
|
|
|
it("reverses horizontal tab focus semantics in RTL", async () => {
|
|
const user = userEvent.setup();
|
|
render(
|
|
<LocaleProvider initialLocale="ar-EG">
|
|
<Tabs
|
|
activation="manual"
|
|
label="sections"
|
|
tabs={[
|
|
{ id: "one", label: "One", panel: "One panel" },
|
|
{ id: "two", label: "Two", panel: "Two panel" },
|
|
{ id: "three", label: "Three", panel: "Three panel" },
|
|
]}
|
|
/>
|
|
</LocaleProvider>,
|
|
);
|
|
const first = screen.getByRole("tab", { name: "One" });
|
|
first.focus();
|
|
await user.keyboard("{ArrowRight}");
|
|
expect(screen.getByRole("tab", { name: "Three" })).toHaveFocus();
|
|
await user.keyboard("{ArrowLeft}");
|
|
expect(first).toHaveFocus();
|
|
});
|
|
|
|
it("keeps pagination semantics while direction-aware icons remain decorative", () => {
|
|
render(
|
|
<LocaleProvider initialLocale="ar-EG">
|
|
<Pagination
|
|
label="pages"
|
|
nextLabel="Next page"
|
|
onChange={() => {}}
|
|
page={2}
|
|
pageCount={3}
|
|
pageLabel={(page) => `Page ${page}`}
|
|
previousLabel="Previous page"
|
|
/>
|
|
</LocaleProvider>,
|
|
);
|
|
expect(
|
|
screen.getByRole("button", { name: "Previous page" }),
|
|
).toBeEnabled();
|
|
expect(screen.getByRole("button", { name: "Next page" })).toBeEnabled();
|
|
expect(screen.queryByRole("img")).not.toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe("persisted locale preference", () => {
|
|
it("stores the chosen locale and starts the next mount from it", async () => {
|
|
const user = userEvent.setup();
|
|
const stored = new Map<string, unknown>();
|
|
const application = createTestApplication({
|
|
preferences: {
|
|
read: (name) => ({ ok: true as const, value: stored.get(name) }),
|
|
write: (name, value) => {
|
|
stored.set(name, value);
|
|
return { ok: true as const };
|
|
},
|
|
remove: (name) => {
|
|
stored.delete(name);
|
|
return { ok: true as const };
|
|
},
|
|
},
|
|
});
|
|
const mount = () =>
|
|
render(
|
|
<ApplicationProvider application={application}>
|
|
<PreferenceProviders>
|
|
<AppRouter />
|
|
</PreferenceProviders>
|
|
</ApplicationProvider>,
|
|
);
|
|
window.history.pushState({}, "", "/missing");
|
|
|
|
const first = mount();
|
|
await user.selectOptions(await screen.findByLabelText("언어"), "en-US");
|
|
expect(stored.get("LOCALE")).toBe("en-US");
|
|
first.unmount();
|
|
|
|
mount();
|
|
expect(await screen.findByLabelText("Language")).toHaveValue("en-US");
|
|
expect(document.documentElement).toHaveAttribute("lang", "en-US");
|
|
});
|
|
});
|