fix(presentation): run the route lifecycle on every route surface (R-8.5)
RouteLifecycle lived inside the route content, which ProtectedRoute,
InvalidRouteSurface and DisabledFeatureSurface never render. On those
screens document.title kept the previous page's title, focus stayed on the
link the user left (screen readers got no page-change cue), and no
route.changed diagnostic was recorded.
RegisteredRoute is now a thin wrapper that renders RouteLifecycle first and
the former body (renamed RouteSurface) after it, so every branch gets title,
main focus and telemetry, and the lifecycle effect still runs before the
page heading's focus effect.
Verified: RED "runs the route lifecycle on the {authentication-required,
invalid-route, disabled-feature} surface" (focus left on a nav link, then a
history navigation) all failed before with the home title still set 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>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
938882dbd3
commit
5e7cee1476
@@ -273,17 +273,35 @@ type ExecutableRouteRuntime = Readonly<{
|
||||
Component: ElementType;
|
||||
}>;
|
||||
|
||||
function RegisteredRoute({
|
||||
routeId,
|
||||
buildId,
|
||||
runtimeOverride,
|
||||
paramsOverride,
|
||||
}: {
|
||||
type RegisteredRouteProps = {
|
||||
routeId: RouteId;
|
||||
buildId: string;
|
||||
runtimeOverride?: ExecutableRouteRuntime;
|
||||
paramsOverride?: Readonly<Record<string, string | undefined>>;
|
||||
}) {
|
||||
};
|
||||
|
||||
/**
|
||||
* Title, focus and route telemetry belong to every answer a route gives,
|
||||
* including auth-required, invalid-address and disabled-feature surfaces.
|
||||
*/
|
||||
function RegisteredRoute(props: RegisteredRouteProps) {
|
||||
return (
|
||||
<>
|
||||
<RouteLifecycle
|
||||
definition={getRoute(props.routeId)}
|
||||
buildId={props.buildId}
|
||||
/>
|
||||
<RouteSurface {...props} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function RouteSurface({
|
||||
routeId,
|
||||
buildId,
|
||||
runtimeOverride,
|
||||
paramsOverride,
|
||||
}: RegisteredRouteProps) {
|
||||
const definition = getRoute(routeId);
|
||||
const routerParams = useParams();
|
||||
const params = paramsOverride ?? routerParams;
|
||||
@@ -311,7 +329,6 @@ function RegisteredRoute({
|
||||
const content = (
|
||||
<RouteInputProvider input={parsed.data}>
|
||||
<CanonicalRouteRedirect input={parsed.data} />
|
||||
<RouteLifecycle definition={definition} buildId={buildId} />
|
||||
<Suspense fallback={<RouteLoadingSurface definition={definition} />}>
|
||||
{/* React Router reuses this instance across sibling routes; the key
|
||||
keeps one route's failed chunk from latching onto the next. */}
|
||||
|
||||
@@ -1,23 +1,27 @@
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import { render, screen, waitFor } from "@testing-library/react";
|
||||
import { act, render, screen, waitFor } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { createAnonymousSessionAdapter } from "../../src/adapters/auth/external-session-adapter.ts";
|
||||
import type { ApplicationOutputPorts } from "../../src/application/create-application.ts";
|
||||
import { INSTALLED_PRODUCT_FEATURE_IDS } from "../../src/features/installed-product-manifest.ts";
|
||||
import { ApplicationProvider } from "../../src/presentation/providers/application-provider.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";
|
||||
|
||||
function renderRouter(
|
||||
diagnostics: ApplicationOutputPorts["diagnostics"] = { record: () => {} },
|
||||
productFeatures?: ApplicationOutputPorts["productFeatures"],
|
||||
) {
|
||||
return render(
|
||||
<ApplicationProvider
|
||||
application={createTestApplication({
|
||||
session: createAnonymousSessionAdapter(),
|
||||
diagnostics,
|
||||
...(productFeatures ? { productFeatures } : {}),
|
||||
})}
|
||||
>
|
||||
<AppRouter />
|
||||
@@ -25,6 +29,14 @@ function renderRouter(
|
||||
);
|
||||
}
|
||||
|
||||
/** A back/forward style navigation that leaves focus where the user left it. */
|
||||
function navigateByHistory(url: string) {
|
||||
act(() => {
|
||||
window.history.pushState({}, "", url);
|
||||
window.dispatchEvent(new PopStateEvent("popstate"));
|
||||
});
|
||||
}
|
||||
|
||||
function routeChangeReports(record: ReturnType<typeof vi.fn>) {
|
||||
return record.mock.calls.filter(
|
||||
([event]) => (event as { eventId: string }).eventId === "route.changed",
|
||||
@@ -123,6 +135,68 @@ describe("generic application router", () => {
|
||||
expect(routeChangeReports(record)).toBe(reportsBefore);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
surface: "authentication-required",
|
||||
url: "/examples/reference-resources",
|
||||
routeId: "REFERENCE_RESOURCE_LIST",
|
||||
title: "Reference resources · Frontend Skeleton",
|
||||
disableFeature: false,
|
||||
},
|
||||
{
|
||||
surface: "invalid-route",
|
||||
url: "/examples/ui?unexpected=1",
|
||||
routeId: "EXAMPLES_UI",
|
||||
title: "UI 구성요소 · Frontend Skeleton",
|
||||
disableFeature: false,
|
||||
},
|
||||
{
|
||||
surface: "disabled-feature",
|
||||
url: "/examples/reference-resources",
|
||||
routeId: "REFERENCE_RESOURCE_LIST",
|
||||
title: "Reference resources · Frontend Skeleton",
|
||||
disableFeature: true,
|
||||
},
|
||||
])(
|
||||
"runs the route lifecycle on the $surface surface",
|
||||
async ({ surface, url, routeId, title, disableFeature }) => {
|
||||
const record = vi.fn();
|
||||
window.history.pushState({}, "", "/");
|
||||
const { container } = renderRouter(
|
||||
{ record },
|
||||
disableFeature
|
||||
? createProductFeaturesStub({
|
||||
[INSTALLED_PRODUCT_FEATURE_IDS[0]!]: "DISABLED_BY_CONFIG",
|
||||
})
|
||||
: undefined,
|
||||
);
|
||||
const link = (
|
||||
await screen.findAllByRole("link", { name: "UI 구성요소" })
|
||||
)[0]!;
|
||||
link.focus();
|
||||
|
||||
navigateByHistory(url);
|
||||
|
||||
await waitFor(
|
||||
() =>
|
||||
expect(
|
||||
container.querySelector(`[data-surface="${surface}"]`),
|
||||
).not.toBeNull(),
|
||||
{ timeout: 5000 },
|
||||
);
|
||||
await waitFor(() => expect(document.title).toBe(title));
|
||||
expect(
|
||||
screen.getByRole("main").contains(document.activeElement),
|
||||
).toBe(true);
|
||||
expect(record).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
eventId: "route.changed",
|
||||
context: expect.objectContaining({ route_id: routeId }),
|
||||
}),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
it("focuses the route heading again when the lazy chunk is already cached", async () => {
|
||||
// §9.7. The first visit resolves the route module asynchronously, so the
|
||||
// router lifecycle and the page header commit separately. A later visit
|
||||
|
||||
Reference in New Issue
Block a user