Files
clean-architecture-frontend…/tests/component/context-route-gateway.test.tsx
T
DongHyeonkaandClaude Opus 5.5 7022a1efd0 fix(presentation): key the context gateway and recover its chunk failures (R-8.7)
(a) ContextRouteGateway had no key, so React Router reused one instance
across sibling contexts. On a context A -> B move the first render still
held A's routes, matched B's URL against them and mounted NOT_FOUND, which
reported route.changed(NOT_FOUND) and moved title/focus once. The gateway
element is now keyed by the context id.

(b) A rejected loadRoutes() always showed RouteFailureSurface, so a
routes module missing after a deploy never reached recoverChunk. A chunk
load failure is now rethrown inside a ChunkRecoveryBoundary keyed on the
chunk of the registered route the URL names (the routes module has no
manifest entry of its own; recoverChunk needs one to decide
reload-once). Other load errors still show RouteFailureSurface.

Verified: new tests/component/context-route-gateway.test.tsx mocks the
app with contexts cut from the real reference routes. RED "never matches
a new context's URL against the previous context's routes" (NOT_FOUND
reported) and "sends a failed routes-module download through chunk
recovery" (navigation.reload never called after a release change) failed
before and pass after. 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

130 lines
4.2 KiB
TypeScript

// @vitest-environment jsdom
import { act, render, screen, waitFor } from "@testing-library/react";
import { describe, expect, it, vi } 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";
/**
* Three bounded contexts cut from the real reference routes, so canonical
* redirects and access rules stay valid: A owns the list, B the status page,
* and the third one's routes module fails to download.
*/
vi.mock("../../src/apps/template-app.ts", async () => {
const { FEATURE_CONTEXT_ROUTES } = await import(
"../../src/features/reference-feature/context-routes.ts"
);
const only = (id: string) => async () =>
FEATURE_CONTEXT_ROUTES.filter((route) => route.id === id);
return {
TEMPLATE_APP_MODULE: {
id: "two-context-app",
budget: { maxContexts: 3 },
contexts: [
{
id: "context-a",
route: {
path: "examples/reference-resources/*",
loadRoutes: only("REFERENCE_RESOURCE_LIST"),
},
},
{
id: "context-b",
route: {
path: "examples/reference-resources/status/*",
loadRoutes: only("REFERENCE_RESOURCE_STATUS"),
},
},
{
id: "context-offline",
route: {
path: "examples/reference-resources/new/*",
loadRoutes: async () => {
throw new TypeError(
"Failed to fetch dynamically imported module: /assets/context-routes.js",
);
},
},
},
],
},
};
});
function navigateByHistory(url: string) {
act(() => {
window.history.pushState({}, "", url);
window.dispatchEvent(new PopStateEvent("popstate"));
});
}
function reportedRoutes(record: ReturnType<typeof vi.fn>): string[] {
return record.mock.calls
.map(([event]) => event as { eventId: string; context: { route_id: string } })
.filter((event) => event.eventId === "route.changed")
.map((event) => event.context.route_id);
}
describe("context route gateway", () => {
it("never matches a new context's URL against the previous context's routes", async () => {
const record = vi.fn();
window.history.pushState({}, "", "/examples/reference-resources");
render(
<ApplicationProvider
application={createTestApplication({ diagnostics: { record } })}
>
<PreferenceProviders>
<AppRouter />
</PreferenceProviders>
</ApplicationProvider>,
);
await waitFor(() =>
expect(reportedRoutes(record)).toContain("REFERENCE_RESOURCE_LIST"),
);
navigateByHistory("/examples/reference-resources/status");
await waitFor(() =>
expect(reportedRoutes(record)).toContain("REFERENCE_RESOURCE_STATUS"),
);
expect(reportedRoutes(record)).not.toContain("NOT_FOUND");
});
it("sends a failed routes-module download through chunk recovery", async () => {
const reload = vi.fn();
const release = (buildId: string, releaseId: string) => ({
buildId,
releaseId,
configSchemaVersion: "1",
apiContractVersion: "1",
assetManifestHash: "hash",
routeChunks: { "route-reference-resource-form": "assets/form.js" },
});
window.history.pushState({}, "", "/examples/reference-resources/new");
render(
<ApplicationProvider
application={createTestApplication({
navigation: { reload },
// A deploy happened: the page runs build-a, the server has build-b.
releaseInfo: {
getCurrent: async () => release("build-a", "release-a"),
refresh: async () => release("build-b", "release-b"),
},
})}
>
<PreferenceProviders>
<AppRouter />
</PreferenceProviders>
</ApplicationProvider>,
);
await waitFor(() => expect(reload).toHaveBeenCalledOnce());
expect(
await screen.findByText("새 버전으로 한 번만 전환합니다."),
).toBeVisible();
});
});