문제: 라우터·chunk 복구·render 경계·앱 셸·런타임 트리 테스트가 스타터
화면(`/`의 h1, 홈의 "플랫폼 구성 보기" 링크, /examples/ui·states)을
대상으로 쓴다. 문서는 스타터 화면을 "페이지 단위로 교체 가능"하다고 하는데,
제품이 화면을 바꾸면 스타터와 무관한 플랫폼 테스트가 깨지고 동기화 때마다
같은 파일이 충돌한다(두 제품 기록).
방법: tests/helpers/fixture-feature.tsx에 테스트 전용 public 페이지 4개를
더했다. 메뉴에 나오는 지연 로드 페이지 2개(제목 h1, 런타임 릴리스 표시),
chunk 로드 실패 페이지, render 결함 페이지다. 플랫폼 테스트는 이미 쓰던
방식대로 생성 인덱스를 mock해 이 기능을 설치하고 그 페이지를 쓴다.
스타터 화면 자체를 검사하던 것(홈 시작 링크, 홈·플랫폼 구성의 릴리스 로딩
실패, 인증 예제의 복귀 URL, 상태 갤러리 제목 구조, 플랫폼 구성 화면)은
starter-pages.test.tsx와 starter-platform-overview-page.test.tsx로
모았다. 제품은 스타터 화면과 함께 starter-* 파일을 지우면 된다.
fixture는 design-system 배럴 대신 page-header.tsx를 직접 import한다.
배럴이 설치 카탈로그를 거쳐 mock된 생성 인덱스를 다시 불러 mock 팩토리가
자기 자신을 기다린다.
버린 대안: README에 "함께 사라지는 검사" 목록만 적는다. 비용은 0이지만
화면을 바꾸면 검사는 여전히 깨지거나 사라진다.
위험: 플랫폼 경로(createRegisteredRoutes의 platformChildren)를 지나는
화면 이동 테스트가 줄고 context 라우트 경로로 옮겨 갔다. `/`와 not-found는
여전히 플랫폼 경로로 돈다. ui-gallery-page는 이제 component 테스트에서
렌더되지 않는다(e2e·visual이 본다). sibling chunk 초기화 테스트는
key={definition.routeId}를 지우면 여전히 실패한다(변이 확인).
검증: RED는 템플릿 복사본에서 home-page.tsx와 examples/*를 다른 화면으로
바꾸고 test:component를 돌렸다. 7파일 20개 실패(router 5, route-status 4,
platform-overview 7, app-shell·async-surface·chunk-recovery·
runtime-application 각 1). GREEN은 같은 조작에서 starter-* 2파일 12개만
실패하고, starter-*를 지우면 34파일 216개 전부 통과. worktree에서
tests/component 36파일 228개 통과, tsc -p tsconfig.test.json 통과, 바꾼
파일 eslint 통과.
247 lines
8.3 KiB
TypeScript
247 lines
8.3 KiB
TypeScript
import { type ComponentType, lazy, useEffect, useState } from "react";
|
|
import { type RouteObject, useSearchParams } from "react-router-dom";
|
|
import { z } from "zod";
|
|
|
|
import { defineBoundedContextModule } from "../../src/contexts/bounded-context-module.ts";
|
|
import { defineFeatureModule } from "../../src/features/feature-module.ts";
|
|
// Not the design-system barrel: it reaches the installed catalogs, whose
|
|
// generated index the tests mock with this module, and the mock would wait on
|
|
// itself.
|
|
import { PageHeader } from "../../src/presentation/components/page-header.tsx";
|
|
import { useApplicationRuntime } from "../../src/presentation/providers/application-provider.tsx";
|
|
|
|
/**
|
|
* A test-only product feature for platform tests that need routes and pages of
|
|
* their own: routes that require a session and parse a search codec, public
|
|
* pages in the primary navigation, and pages that fail to render. Those tests
|
|
* install it by mocking the generated indexes, so they depend neither on the
|
|
* removable sample feature (FE-GATE-020) nor on the starter screens a product
|
|
* replaces (`/`, `/examples/*`; 0929/10-02):
|
|
*
|
|
* 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/.generated/product-context-index.ts", async () => ({
|
|
* PRODUCT_CONTEXT_MODULES: [(await import("../helpers/fixture-feature.tsx")).FIXTURE_CONTEXT_MODULE],
|
|
* }));
|
|
*/
|
|
export const FIXTURE_FEATURE_ID = "fixture-feature";
|
|
|
|
function fixtureRoute(
|
|
routeId: string,
|
|
path: string,
|
|
searchSchema: string | null,
|
|
chunkId: string,
|
|
) {
|
|
return Object.freeze({
|
|
routeId,
|
|
path,
|
|
paramsSchema: null,
|
|
searchSchema,
|
|
access: "session-required",
|
|
loadingSurface: "fixture-page",
|
|
errorSurface: "feature-boundary",
|
|
chunkId,
|
|
title: routeId,
|
|
navigationLabel: null,
|
|
navigationOrder: null,
|
|
});
|
|
}
|
|
|
|
/** A public page like the starter pages: route boundary, optional menu entry. */
|
|
function publicFixtureRoute(
|
|
routeId: string,
|
|
path: string,
|
|
chunkId: string,
|
|
navigationOrder: number | null,
|
|
) {
|
|
return Object.freeze({
|
|
...fixtureRoute(routeId, path, null, chunkId),
|
|
access: "public" as const,
|
|
errorSurface: "route-boundary",
|
|
navigationLabel: navigationOrder === null ? null : routeId,
|
|
navigationOrder,
|
|
});
|
|
}
|
|
|
|
const FIXTURE_ROUTES = Object.freeze({
|
|
FIXTURE_ITEMS: fixtureRoute("FIXTURE_ITEMS", "/fixture/items", "FixtureItemsQuery", "route-fixture-items"),
|
|
FIXTURE_ITEM_STATUS: fixtureRoute("FIXTURE_ITEM_STATUS", "/fixture/items/status", null, "route-fixture-item-status"),
|
|
FIXTURE_ITEM_FORM: fixtureRoute("FIXTURE_ITEM_FORM", "/fixture/items/new", null, "route-fixture-item-form"),
|
|
FIXTURE_FIRST_PAGE: publicFixtureRoute("FIXTURE_FIRST_PAGE", "/fixture/first", "route-fixture-first", 900),
|
|
FIXTURE_SECOND_PAGE: publicFixtureRoute("FIXTURE_SECOND_PAGE", "/fixture/second", "route-fixture-second", 910),
|
|
FIXTURE_OFFLINE_PAGE: publicFixtureRoute("FIXTURE_OFFLINE_PAGE", "/fixture/offline", "route-fixture-offline", null),
|
|
FIXTURE_BROKEN_PAGE: publicFixtureRoute("FIXTURE_BROKEN_PAGE", "/fixture/broken", "route-fixture-broken", null),
|
|
});
|
|
|
|
type FixtureRouteId = keyof typeof FIXTURE_ROUTES;
|
|
|
|
const FIXTURE_TITLES: Readonly<Record<FixtureRouteId, string>> = {
|
|
FIXTURE_ITEMS: "Fixture items",
|
|
FIXTURE_ITEM_STATUS: "Fixture item status",
|
|
FIXTURE_ITEM_FORM: "Create fixture item",
|
|
FIXTURE_FIRST_PAGE: "Fixture first page",
|
|
FIXTURE_SECOND_PAGE: "Fixture second page",
|
|
FIXTURE_OFFLINE_PAGE: "Fixture offline page",
|
|
FIXTURE_BROKEN_PAGE: "Fixture broken page",
|
|
};
|
|
|
|
const FIXTURE_MESSAGES = Object.freeze(
|
|
Object.fromEntries(
|
|
Object.entries(FIXTURE_TITLES).flatMap(([routeId, title]) => [
|
|
[`route.${routeId}.title`, title],
|
|
[`route.${routeId}.navigation`, title],
|
|
]),
|
|
),
|
|
);
|
|
|
|
export const FIXTURE_FEATURE_MODULE = defineFeatureModule({
|
|
id: FIXTURE_FEATURE_ID,
|
|
contracts: Object.freeze({
|
|
featureId: FIXTURE_FEATURE_ID,
|
|
routes: FIXTURE_ROUTES,
|
|
routeRuntimeContracts: Object.freeze(
|
|
Object.fromEntries(
|
|
Object.values(FIXTURE_ROUTES).map((route) => [
|
|
route.routeId,
|
|
Object.freeze({
|
|
routeId: route.routeId,
|
|
moduleId: `${route.chunkId}-page`,
|
|
paramsCodec: "none",
|
|
searchCodec: route.searchSchema ?? "none",
|
|
}),
|
|
]),
|
|
),
|
|
),
|
|
invalidation: Object.freeze({ topics: [], namespaces: [], edges: [] }),
|
|
topicVersions: Object.freeze([]),
|
|
contractPackages: Object.freeze([]),
|
|
}),
|
|
routes: FIXTURE_ROUTES,
|
|
messages: Object.freeze({ "ko-KR": FIXTURE_MESSAGES, "en-US": FIXTURE_MESSAGES }),
|
|
loadRuntime: async () =>
|
|
Object.freeze({
|
|
featureId: FIXTURE_FEATURE_ID,
|
|
apiContract: Object.freeze({
|
|
apiOperations: Object.freeze({}),
|
|
schemas: Object.freeze({}),
|
|
runtimeSchemas: Object.freeze({}),
|
|
mappers: Object.freeze({}),
|
|
}),
|
|
adapters: Object.freeze([]),
|
|
contractContributions: Object.freeze([]),
|
|
}),
|
|
});
|
|
|
|
// 15-01. Route codecs travel on the context route handle, like the sample's.
|
|
const FIXTURE_ROUTE_CODECS = Object.freeze({
|
|
FixtureItemsQuery: z.object({
|
|
limit: z.coerce.number().int().min(1).max(100).default(20),
|
|
}),
|
|
});
|
|
|
|
/** A filter bound to the URL query, the way a product list screen keeps it. */
|
|
function FixturePage() {
|
|
const [search, setSearch] = useSearchParams();
|
|
return (
|
|
<>
|
|
<p>Fixture page</p>
|
|
<label>
|
|
Fixture limit
|
|
<select
|
|
value={search.get("limit") ?? "20"}
|
|
onChange={(event) => setSearch({ limit: event.target.value })}
|
|
>
|
|
<option value="20">20</option>
|
|
<option value="50">50</option>
|
|
</select>
|
|
</label>
|
|
</>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* A lazily loaded page that opens with its heading and shows the release the
|
|
* runtime resolved, the way a product page does. Lazy, so a first visit
|
|
* suspends on the route loading surface and a later visit renders at once.
|
|
*/
|
|
function titledPage(title: string): ComponentType {
|
|
return lazy(async () => ({
|
|
default: function FixtureTitledPage() {
|
|
const runtime = useApplicationRuntime();
|
|
const [release, setRelease] = useState<string | null>(null);
|
|
useEffect(() => {
|
|
let active = true;
|
|
runtime.getReleaseSummary().then(
|
|
(summary) => {
|
|
if (active) {
|
|
setRelease(`Fixture release ${summary.buildId} · ${summary.releaseId}`);
|
|
}
|
|
},
|
|
() => {},
|
|
);
|
|
return () => {
|
|
active = false;
|
|
};
|
|
}, [runtime]);
|
|
return (
|
|
<section className="ui-page">
|
|
<PageHeader title={title} />
|
|
{release === null ? null : <p>{release}</p>}
|
|
</section>
|
|
);
|
|
},
|
|
}));
|
|
}
|
|
|
|
/** A route chunk the network could not deliver: the rejection rethrows on every render. */
|
|
function OfflineChunkPage(): never {
|
|
throw new TypeError(
|
|
"Failed to fetch dynamically imported module: /assets/fixture-offline.js",
|
|
);
|
|
}
|
|
|
|
/** A route page with a programmer defect. */
|
|
function BrokenPage(): never {
|
|
throw new Error("ordinary render defect");
|
|
}
|
|
|
|
const FIXTURE_PAGES: Readonly<Record<FixtureRouteId, ComponentType>> = {
|
|
FIXTURE_ITEMS: FixturePage,
|
|
FIXTURE_ITEM_STATUS: FixturePage,
|
|
FIXTURE_ITEM_FORM: FixturePage,
|
|
FIXTURE_FIRST_PAGE: titledPage(FIXTURE_TITLES.FIXTURE_FIRST_PAGE),
|
|
FIXTURE_SECOND_PAGE: titledPage(FIXTURE_TITLES.FIXTURE_SECOND_PAGE),
|
|
FIXTURE_OFFLINE_PAGE: OfflineChunkPage,
|
|
FIXTURE_BROKEN_PAGE: BrokenPage,
|
|
};
|
|
|
|
export const FIXTURE_CONTEXT_ROUTES: readonly RouteObject[] = Object.freeze(
|
|
(Object.keys(FIXTURE_ROUTES) as FixtureRouteId[]).map((routeId) => {
|
|
const route = FIXTURE_ROUTES[routeId];
|
|
return Object.freeze({
|
|
id: route.routeId,
|
|
path: route.path,
|
|
handle: Object.freeze({
|
|
routeId: route.routeId,
|
|
runtime: Object.freeze({
|
|
moduleId: `${route.chunkId}-page`,
|
|
Component: FIXTURE_PAGES[routeId],
|
|
}),
|
|
codecs: FIXTURE_ROUTE_CODECS,
|
|
}),
|
|
});
|
|
}),
|
|
);
|
|
|
|
export const FIXTURE_CONTEXT_MODULE = defineBoundedContextModule({
|
|
id: "fixture-context",
|
|
features: [FIXTURE_FEATURE_MODULE],
|
|
budget: { maxFeatures: 1 },
|
|
route: {
|
|
path: "fixture/*",
|
|
loadRoutes: async () => FIXTURE_CONTEXT_ROUTES,
|
|
},
|
|
});
|