diff --git a/src/presentation/routes/app-router.tsx b/src/presentation/routes/app-router.tsx index c22429d4..7dc3fdfd 100644 --- a/src/presentation/routes/app-router.tsx +++ b/src/presentation/routes/app-router.tsx @@ -139,14 +139,17 @@ function RouteLifecycle({ definition: RouteDefinition; buildId: string; }) { - const location = useLocation(); + const { pathname, search } = useLocation(); const { message, resolve } = useLocale(); const diagnostics = useApplicationDiagnostics(); + // A locale change re-renders the title only; it is not a route change. useEffect(() => { document.title = message("route.documentTitle", { title: resolve(`route.${definition.routeId}.title`), appName: message("common.appName"), }); + }, [definition, message, resolve]); + useEffect(() => { const main = document.getElementById("main-content"); main?.focus({ preventScroll: true }); try { @@ -160,15 +163,7 @@ function RouteLifecycle({ routeId: definition.routeId, buildId, }); - }, [ - buildId, - definition, - diagnostics, - location.key, - location.pathname, - message, - resolve, - ]); + }, [buildId, definition, diagnostics, pathname, search]); return null; } diff --git a/tests/component/router.test.tsx b/tests/component/router.test.tsx index ef183781..e7542a33 100644 --- a/tests/component/router.test.tsx +++ b/tests/component/router.test.tsx @@ -2,18 +2,22 @@ import { render, screen, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; -import { describe, expect, it } from "vitest"; +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 { 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"; -function renderRouter() { +function renderRouter( + diagnostics: ApplicationOutputPorts["diagnostics"] = { record: () => {} }, +) { return render( @@ -21,6 +25,12 @@ function renderRouter() { ); } +function routeChangeReports(record: ReturnType) { + return record.mock.calls.filter( + ([event]) => (event as { eventId: string }).eventId === "route.changed", + ).length; +} + describe("generic application router", () => { it("reaches the platform overview from the home starter actions", async () => { const user = userEvent.setup(); @@ -81,6 +91,38 @@ describe("generic application router", () => { ); }); + it("changes only the title and copy when the locale changes", async () => { + // docs/architecture/routing-pages-and-patterns.md §9: a locale change + // re-renders the document title and copy; it is not a route change. + const user = userEvent.setup(); + const record = vi.fn(); + window.history.pushState({}, "", "/missing"); + renderRouter({ record }); + + await waitFor(() => + expect( + screen.getByRole("heading", { + name: "페이지를 찾을 수 없습니다.", + level: 1, + }), + ).toHaveFocus(), + ); + const reportsBefore = routeChangeReports(record); + expect(reportsBefore).toBeGreaterThan(0); + + const localeSelect = screen.getByLabelText("언어"); + await user.selectOptions(localeSelect, "en-US"); + + await waitFor(() => + expect(document.title).toBe("Page not found · Frontend Skeleton"), + ); + expect( + screen.getByRole("heading", { name: "Page not found.", level: 1 }), + ).toBeVisible(); + expect(localeSelect).toHaveFocus(); + expect(routeChangeReports(record)).toBe(reportsBefore); + }); + 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