diff --git a/docs/architecture/starter-experience.md b/docs/architecture/starter-experience.md index 76bac2e1..f8f7f75d 100644 --- a/docs/architecture/starter-experience.md +++ b/docs/architecture/starter-experience.md @@ -235,6 +235,8 @@ than `AppShell`, declares it. The router and the catalogs do not change. exactly one `
` around ``. The router moves focus there on every route change. Layout routes are siblings of `AppShell`, never inside it. + `tests/component/layout-landmarks.test.tsx` renders `AppShell` and the + layout of every installed context and fails one without them. - **Failures.** A layout chunk a deploy removed gets the reload-once recovery of a route chunk. Route and feature boundaries work as they do in `AppShell`. diff --git a/tests/component/layout-landmarks.test.tsx b/tests/component/layout-landmarks.test.tsx new file mode 100644 index 00000000..5aa1803d --- /dev/null +++ b/tests/component/layout-landmarks.test.tsx @@ -0,0 +1,62 @@ +// @vitest-environment jsdom + +import { render, screen } from "@testing-library/react"; +import type { ComponentType } from "react"; +import { createMemoryRouter, RouterProvider } from "react-router-dom"; +import { describe, expect, it } from "vitest"; + +import { TEMPLATE_APP_MODULE } from "../../src/apps/template-app.ts"; +import { AppShell } from "../../src/presentation/layouts/app-shell.tsx"; +import { ApplicationProvider } from "../../src/presentation/providers/application-provider.tsx"; +import { PreferenceProviders } from "../../src/presentation/providers/preference-providers.tsx"; +import { SessionProvider } from "../../src/presentation/providers/session-provider.tsx"; +import { createTestApplication } from "../helpers/create-test-application.ts"; +import { LAYOUT_PRODUCT_CONTEXT_MODULES } from "../helpers/fixture-layout-product.tsx"; + +/** + * 09-03 (1101/14.4). The router moves focus to `#main-content` on every route + * change, so every layout it mounts has to render a skip link to it and make + * it the one main landmark around the page. The layouts are read from the + * contexts of the installed product and of the fixture product, so a new + * `context-layout.tsx` is checked without being named here. + */ +const contexts = [ + ...TEMPLATE_APP_MODULE.contexts, + // The fixture's offline layout chunk never arrives, by design. + ...LAYOUT_PRODUCT_CONTEXT_MODULES.filter((context) => context.id !== "offline"), +]; +const LAYOUTS: readonly (readonly [string, ComponentType])[] = [ + ["AppShell", AppShell], + ...(await Promise.all( + contexts.flatMap((context) => { + const loadLayout = context.route?.loadLayout; + return loadLayout + ? [loadLayout().then((Layout) => [`context ${context.id}`, Layout] as const)] + : []; + }), + )), +]; + +describe("every layout the router mounts", () => { + it.each(LAYOUTS)("%s links its skip link to its one main landmark", async (_name, Layout) => { + const router = createMemoryRouter([ + { path: "/", element: , children: [{ index: true, element:

Probe page

}] }, + ]); + render( + + + + + + + , + ); + + const page = await screen.findByRole("heading", { name: "Probe page", level: 1 }); + const mains = document.querySelectorAll('main, [role="main"]'); + expect(mains).toHaveLength(1); + expect(mains[0]).toHaveAttribute("id", "main-content"); + expect(mains[0]).toContainElement(page); + expect(document.querySelector('a[href="#main-content"]')).not.toBeNull(); + }); +});