Files
clean-architecture-frontend…/tests/component/product-feature-switch.test.tsx
T
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

87 lines
3.1 KiB
TypeScript

// @vitest-environment jsdom
import { render, screen } from "@testing-library/react";
import { describe, expect, it } from "vitest";
import { createAnonymousSessionAdapter } from "../../src/adapters/auth/external-session-adapter.ts";
import { ROUTE_REGISTRY } from "../../src/features/installed-feature-contracts.ts";
import { INSTALLED_PRODUCT_FEATURE_IDS } from "../../src/features/installed-product-manifest.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";
import { createProductFeaturesStub } from "../helpers/runtime-capabilities-stub.ts";
/**
* §3.5. The runtime kill switch, exercised through the running app rather than
* through the resolver that computes it.
*
* Withdrawing a feature from navigation is not the same as taking it out of
* service: a typed deep link would still mount it. Both halves are asserted
* here, on the same render, so the switch cannot be half-wired.
*/
const FEATURE_ID = INSTALLED_PRODUCT_FEATURE_IDS[0]!;
const FEATURE_ROUTE = ROUTE_REGISTRY.REFERENCE_RESOURCE_LIST;
function renderAt(path: string, disabled: boolean) {
window.history.pushState({}, "", path);
return render(
<ApplicationProvider
application={createTestApplication({
session: createAnonymousSessionAdapter(),
...(disabled
? {
productFeatures: createProductFeaturesStub({
[FEATURE_ID]: "DISABLED_BY_CONFIG",
}),
}
: {}),
})}
>
<PreferenceProviders>
<AppRouter />
</PreferenceProviders>
</ApplicationProvider>,
);
}
describe("runtime product feature switch", () => {
it("advertises the feature's route while the feature is active", async () => {
renderAt("/", false);
expect(
await screen.findByRole("link", { name: FEATURE_ROUTE.navigationLabel! }),
).toBeTruthy();
});
it("withdraws the feature's route from navigation when it is disabled", async () => {
renderAt("/", true);
// The shell itself still renders: disabling a feature is not an outage.
expect(await screen.findByRole("navigation")).toBeTruthy();
expect(
screen.queryByRole("link", { name: FEATURE_ROUTE.navigationLabel! }),
).toBeNull();
});
it("takes the feature out of service for a direct deep link", async () => {
renderAt(FEATURE_ROUTE.path, true);
const surface = await screen.findByText(
(_, element) =>
element?.getAttribute("data-disabled-feature") === FEATURE_ID,
{},
{ timeout: 5000 },
);
expect(surface).toBeTruthy();
});
it("serves the same deep link while the feature is active", async () => {
renderAt(FEATURE_ROUTE.path, false);
expect(
screen.queryByText(
(_, element) =>
element?.getAttribute("data-disabled-feature") === FEATURE_ID,
),
).toBeNull();
});
});