// @vitest-environment jsdom import { render, screen, waitFor, within } 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 { 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"; // Routes and pages come from a test-only feature, not the removable sample // (FE-GATE-020 runs the suite after deleting the sample) nor the starter // screens a product replaces (0929/10-02; starter-*.test.tsx test those). 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], })); function deferred() { let reject: (error: unknown) => void = () => {}; const promise = new Promise((_, fail) => { reject = fail; }); return { promise, reject }; } function renderApp( url: string, overrides: Partial = {}, basename?: string, ) { window.history.pushState({}, "", url); return render( , ); } function sessionWithSignIn(beginSignIn: (returnTo?: string) => Promise) { return { ...createAnonymousSessionAdapter(), beginSignIn }; } describe("route-level status surfaces", () => { // First in the file: the page chunk has not been loaded yet, so the route // suspends and shows its own loading surface (the context's routes load // behind a surface without the route's name). it("shows the busy route loading surface while a page chunk loads", async () => { const { container } = renderApp("/fixture/first"); await waitFor(() => expect( container.querySelector('.route-loading[data-surface="fixture-page"]'), ).not.toBeNull(), ); const surface = container.querySelector( '.route-loading[data-surface="fixture-page"]', )!; const copy = within(surface).getByText("화면을 준비하고 있습니다."); expect(copy.closest('[aria-busy="true"]')).not.toBeNull(); expect( await screen.findByRole( "heading", { name: "Fixture first page", level: 1 }, ), ).toBeVisible(); }); it("keeps the auth-required surface, its pending label and its failure alert", async () => { const user = userEvent.setup(); const pending = deferred(); const beginSignIn = vi.fn(() => pending.promise); const { container } = renderApp("/fixture/items#results", { session: sessionWithSignIn(beginSignIn), }); const heading = await screen.findByRole( "heading", { name: "세션이 필요합니다.", level: 1 }, ); const surface = container.querySelector( '[data-surface="authentication-required"]', ); expect(surface).not.toBeNull(); expect(surface).toContainElement(heading); await user.click(screen.getByRole("button", { name: "로그인 시작" })); expect(beginSignIn).toHaveBeenCalledWith( "/fixture/items#results", ); expect(screen.getByRole("button", { name: "처리 중…" })).toBeDisabled(); pending.reject(new Error("identity provider unavailable")); expect(await screen.findByRole("alert")).toHaveTextContent( "세션 작업을 완료하지 못했습니다.", ); expect(screen.getByRole("button", { name: "로그인 시작" })).toBeEnabled(); }); it("returns to the current URL with its hash from the shell session action", async () => { const user = userEvent.setup(); const pending = deferred(); const beginSignIn = vi.fn(() => pending.promise); renderApp("/fixture/first#buttons", { session: sessionWithSignIn(beginSignIn) }); const banner = screen.getByRole("banner"); await user.click( await within(banner).findByRole("button", { name: "로그인" }), ); expect(beginSignIn).toHaveBeenCalledWith("/fixture/first#buttons"); expect(within(banner).getByRole("button", { name: "처리 중…" })).toBeDisabled(); pending.reject(new Error("identity provider unavailable")); expect(await within(banner).findByRole("alert")).toHaveTextContent( "세션 작업을 완료하지 못했습니다.", ); }); it("answers an unknown address with a home link that keeps the basename", async () => { renderApp("/app/missing", {}, "/app"); const heading = await screen.findByRole( "heading", { name: "페이지를 찾을 수 없습니다.", level: 1 }, ); const page = heading.closest("section"); expect(page).not.toBeNull(); expect( within(page!).getByRole("link", { name: "홈으로 이동" }), ).toHaveAttribute("href", "/app"); }); });