// @vitest-environment jsdom
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, it, vi } from "vitest";
import { ApplicationProvider } from "../../src/presentation/providers/application-provider.tsx";
import { PreferenceProviders } from "../../src/presentation/providers/preference-providers.tsx";
import { AppRouter } from "../../src/presentation/routes/app-router.tsx";
import { createTestApplication } from "../helpers/create-test-application.ts";
// 09-03. The product in tests/helpers/fixture-layout-product.tsx declares the
// downstream products' home and layouts; nothing here touches the router.
vi.mock("../../src/.generated/product-feature-index.ts", async () => {
const product = await import("../helpers/fixture-layout-product.tsx");
return {
DECLARED_PRODUCT_FEATURE_IDS: product.LAYOUT_PRODUCT_FEATURE_IDS,
PRODUCT_FEATURE_MODULES: product.LAYOUT_PRODUCT_FEATURE_MODULES,
};
});
vi.mock("../../src/.generated/product-context-index.ts", async () => ({
PRODUCT_CONTEXT_MODULES: (await import("../helpers/fixture-layout-product.tsx"))
.LAYOUT_PRODUCT_CONTEXT_MODULES,
}));
function renderAt(url: string, record = vi.fn()) {
window.history.pushState({}, "", url);
render(
,
);
return record;
}
const APP_SHELL_NAVIGATION = { name: "주요 탐색" } as const;
async function page(title: string) {
return screen.findByRole("heading", { name: title, level: 1 });
}
/** The skip link, the one `` and the focus move every screen keeps. */
async function expectAccessibleScreen(title: string) {
const heading = await page(title);
const mains = document.querySelectorAll("main");
expect(mains).toHaveLength(1);
expect(mains[0]).toHaveAttribute("id", "main-content");
expect(mains[0]).toContainElement(heading);
expect(document.querySelectorAll('a[href="#main-content"]')).toHaveLength(1);
await waitFor(() => expect(heading).toHaveFocus());
}
describe("context layouts and a feature-owned home (09-03)", () => {
it("renders the product home a feature took over inside its context's layout", async () => {
renderAt("/");
const heading = await page("Site home");
expect(heading.closest("[data-layout]")).toHaveAttribute("data-layout", "site");
expect(screen.queryByRole("navigation", APP_SHELL_NAVIGATION)).toBeNull();
});
it("mounts each context under the layout it declares", async () => {
renderAt("/studio/documents");
const heading = await page("Studio documents");
expect(heading.closest("[data-layout]")).toHaveAttribute("data-layout", "studio");
expect(screen.queryByRole("navigation", APP_SHELL_NAVIGATION)).toBeNull();
});
it("keeps one layout instance across the context roots under it", async () => {
const user = userEvent.setup();
renderAt("/explore");
const layout = (await page("Site explore")).closest("[data-layout]");
await user.click(screen.getByRole("link", { name: "Topics" }));
const topics = await page("Site topics");
expect(topics.closest("[data-layout]")).toBe(layout);
});
it("leaves unknown URLs and contexts without a layout in AppShell", async () => {
renderAt("/studio/missing");
expect(await page("페이지를 찾을 수 없습니다.")).toBeVisible();
expect(screen.getByRole("navigation", APP_SHELL_NAVIGATION)).toBeVisible();
expect(document.querySelector("[data-layout]")).toBeNull();
});
it("keeps the skip link, one main and the focus move across layouts", async () => {
const user = userEvent.setup();
renderAt("/fixture/first");
await expectAccessibleScreen("Fixture first page");
expect(screen.getByRole("navigation", APP_SHELL_NAVIGATION)).toBeVisible();
// AppShell -> site layout, through the home link the feature now owns.
await user.click(screen.getAllByRole("link", { name: "Site home" })[0]!);
await expectAccessibleScreen("Site home");
// site layout -> studio layout.
await user.click(screen.getByRole("link", { name: "Open the studio" }));
await expectAccessibleScreen("Studio home");
// studio layout -> AppShell.
await user.click(screen.getByRole("link", { name: "Back to the app shell" }));
await expectAccessibleScreen("Fixture first page");
});
it("recovers a layout chunk that did not arrive like a route chunk", async () => {
const record = renderAt("/offline");
expect(
await screen.findByRole("heading", { name: "화면 자산을 복구하지 못했습니다." }),
).toBeVisible();
expect(record).toHaveBeenCalledWith({
level: "error",
eventId: "ui.render.failed",
context: expect.objectContaining({ route_id: "OFFLINE_HOME", build_id: "build-a" }),
});
});
});