Files
clean-architecture-frontend…/tests/component/context-route-gateway.test.tsx
T
DongHyeonkaandClaude Opus 5.5 a8111f7647 fix(features): apply VITE_PRODUCT_FEATURES in the index generator (09-02)
The bundle applied the selection at runtime while Node build scripts and the
context router ignored it, and a typo only failed in the browser. The generator
now imports only the selected features and rejects unknown ids (D-9); declared
ids stay in the index for NOT_INSTALLED reporting. Default build modules are
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 22:29:51 +09:00

148 lines
4.8 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 test-only feature's 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/.generated/product-feature-index.ts", async () => ({
DECLARED_PRODUCT_FEATURE_IDS: ["fixture-feature"],
PRODUCT_FEATURE_MODULES: [(await import("../helpers/fixture-feature.tsx")).FIXTURE_FEATURE_MODULE],
}));
vi.mock("../../src/apps/template-app.ts", async () => {
const { FIXTURE_CONTEXT_ROUTES } = await import("../helpers/fixture-feature.tsx");
const only = (id: string) => async () =>
FIXTURE_CONTEXT_ROUTES.filter((route) => route.id === id);
return {
TEMPLATE_APP_MODULE: {
id: "two-context-app",
budget: { maxContexts: 3 },
contexts: [
{
id: "context-a",
route: {
path: "fixture/items/*",
loadRoutes: only("FIXTURE_ITEMS"),
},
},
{
id: "context-b",
route: {
path: "fixture/items/status/*",
loadRoutes: only("FIXTURE_ITEM_STATUS"),
},
},
{
id: "context-offline",
route: {
path: "fixture/items/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({}, "", "/fixture/items");
render(
<ApplicationProvider
application={createTestApplication({ diagnostics: { record } })}
>
<PreferenceProviders>
<AppRouter />
</PreferenceProviders>
</ApplicationProvider>,
);
await waitFor(() =>
expect(reportedRoutes(record)).toContain("FIXTURE_ITEMS"),
);
navigateByHistory("/fixture/items/status");
await waitFor(() =>
expect(reportedRoutes(record)).toContain("FIXTURE_ITEM_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-fixture-item-form": "assets/form.js" },
});
// The reload-once guard only trusts a marker it can read back (Q-6.9-9).
const stored = new Map<string, unknown>();
window.history.pushState({}, "", "/fixture/items/new");
render(
<ApplicationProvider
application={createTestApplication({
preferences: {
read: (name) => ({
ok: true as const,
value: stored.has(name) ? stored.get(name) : "system",
}),
write: (name, value) => {
stored.set(name, value);
return { ok: true as const };
},
remove: (name) => {
stored.delete(name);
return { ok: true as const };
},
},
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();
});
});