Files
clean-architecture-frontend…/tests/helpers/fixture-feature.tsx
T
DongHyeonka d0f3993b60 fix(presentation): 검색값만 바뀌면 포커스·스크롤·진단을 건드리지 않는다 (08-02)
문제: RouteLifecycle이 pathname·search·navigationType을 effect 의존값으로
썼다. 그래서 URL에 묶은 필터를 바꾸면 포커스가 컨트롤에서 <main>으로
튀고, POP이 아니면 맨 위로 스크롤하고, route.changed를 한 번 더 남겼다.
이미 보고 있는 화면의 링크를 눌러도(React Router가 REPLACE로 바꿈)
navigationType만 바뀌어 같은 일이 났다. 템플릿은 필터를 URL에 두라고
권하므로 첫 목록 화면에서 바로 드러난다(WCAG 3.2.2).

고른 방법: 이동 판단을 route ID 또는 pathname 변경으로 좁혔다(리뷰 5.1
(B)). 포커스·스크롤·진단은 useEffectEvent로 옮겨 navigationType을 읽기만
하고 트리거로 쓰지 않는다. 검색값 변경에 맨 위가 필요한 화면(페이지
번호)은 그 화면이 직접 스크롤한다. routing 문서 6.3절에 규칙을 적었다.
버린 대안: (C) 라우트 계약에 searchChangeIsNavigation 선언 — 그런
라우트가 템플릿에 없고 생성기·문서까지 바뀐다. 필요해지면 (B) 위에
더할 수 있다. navigationType을 ref로 들고 있기 — 렌더 중 ref 쓰기라
React 19의 useEffectEvent보다 의도가 덜 드러난다.

위험·한계: 지금 검색값 변경을 이동으로 세던 대시보드가 있다면 수치가
준다. 실제 브라우저의 스크롤 유지는 jsdom이 스크롤을 건너뛰어 정적
확인만 했다. 테스트용 fixture 페이지에 URL에 묶은 select를 더했다.

검증:
- RED: router.test.tsx 새 테스트 2건이 수정 전 실패. 포커스가
  <main id="main-content">에 있었고 route.changed가 필터 테스트 1→3,
  링크 테스트 1→2로 늘었다.
- GREEN: router.test.tsx 11/11, tests/component 34 files 213 tests 통과.
  tsc(app, test) 통과, eslint(바꾼 파일) 통과,
  verify-documentation-readiness PASS_SCOPED.
- check:architecture는 pnpm 실행을 요구해 이 worktree에서 미실행.

Refs: docs/reviews/2026-11-01-maturity-review/08-presentation.md 08-02
2026-11-01 23:20:31 +09:00

153 lines
4.8 KiB
TypeScript

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";
/**
* A test-only product feature for platform tests that need a feature route: one
* that requires a session, parses a search codec, and belongs to a feature the
* runtime can switch off. Those tests install it by mocking the generated
* indexes, so they do not depend on the removable sample feature (FE-GATE-020):
*
* 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,
});
}
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"),
});
const FIXTURE_TITLES: Readonly<Record<keyof typeof FIXTURE_ROUTES, string>> = {
FIXTURE_ITEMS: "Fixture items",
FIXTURE_ITEM_STATUS: "Fixture item status",
FIXTURE_ITEM_FORM: "Create fixture item",
};
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>
</>
);
}
export const FIXTURE_CONTEXT_ROUTES: readonly RouteObject[] = Object.freeze(
Object.values(FIXTURE_ROUTES).map((route) =>
Object.freeze({
id: route.routeId,
path: route.path,
handle: Object.freeze({
routeId: route.routeId,
runtime: Object.freeze({ moduleId: `${route.chunkId}-page`, Component: FixturePage }),
codecs: FIXTURE_ROUTE_CODECS,
}),
}),
),
);
export const FIXTURE_CONTEXT_MODULE = defineBoundedContextModule({
id: "fixture-context",
features: [FIXTURE_FEATURE_MODULE],
budget: { maxFeatures: 1 },
route: {
path: "fixture/items/*",
loadRoutes: async () => FIXTURE_CONTEXT_ROUTES,
},
});