// @vitest-environment jsdom import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react"; import { afterEach, beforeEach, expect, test, vi } from "vitest"; const { push } = vi.hoisted(() => ({ push: vi.fn() })); vi.mock("next/navigation", () => ({ usePathname: () => "/redaktion/alben/abc", useRouter: () => ({ push }), })); vi.mock("next-themes", () => ({ useTheme: () => ({ resolvedTheme: "light", setTheme: vi.fn() }) })); vi.mock("@/modules/auth/client", () => ({ authClient: { signOut: vi.fn() } })); import { authClient } from "@/modules/auth/client"; import { AppShell } from "./app-shell"; import { RELEASE } from "./release-notes"; const legal = { imprint: "https://example.com/impressum", privacy: "https://example.com/datenschutz", terms: "https://example.com/agb", }; beforeEach(() => { // The release-notes popup is not under test here unless a test clears this. localStorage.setItem("release-notes-seen:redaktion", RELEASE.version); }); afterEach(() => { cleanup(); localStorage.clear(); push.mockClear(); }); test("marks the active section and routes sidebar links client-side", () => { render(Inhalt); const albums = screen.getByRole("link", { name: "Alben" }); expect(albums.getAttribute("aria-current")).toBe("page"); const lightbox = screen.getByRole("link", { name: "Merkzettel" }); expect(fireEvent.click(lightbox)).toBe(false); // default (full reload) prevented expect(push).toHaveBeenCalledWith("/redaktion/leuchtkasten"); push.mockClear(); fireEvent.click(lightbox, { metaKey: true }); // new-tab clicks stay native expect(push).not.toHaveBeenCalled(); }); test("legal links sit in the sidebar and open natively in a new tab", () => { render(Inhalt); const imprint = screen.getByRole("link", { name: "Impressum" }); expect(imprint.getAttribute("href")).toBe(legal.imprint); expect(imprint.getAttribute("target")).toBe("_blank"); expect(fireEvent.click(imprint)).toBe(true); // not intercepted expect(push).not.toHaveBeenCalled(); expect(screen.getByRole("link", { name: "Datenschutz" })).toBeTruthy(); expect(screen.getByRole("link", { name: "Nutzungsbedingungen" })).toBeTruthy(); }); test("release notes pop up once per version and reopen from the sidebar", async () => { localStorage.clear(); const { unmount } = render(Inhalt); expect(await screen.findByRole("dialog", { name: "Was ist neu?" })).toBeTruthy(); fireEvent.click(screen.getByRole("button", { name: "Verstanden" })); await waitFor(() => expect(screen.queryByRole("dialog")).toBeNull()); expect(localStorage.getItem("release-notes-seen:admin")).toBe(RELEASE.version); unmount(); render(Inhalt); expect(screen.queryByRole("dialog")).toBeNull(); // seen: stays closed fireEvent.click(screen.getByRole("button", { name: new RegExp(`Was ist neu ยท v${RELEASE.version}`) })); expect(await screen.findByRole("dialog", { name: "Was ist neu?" })).toBeTruthy(); }); test.each(["rejected", "error response"])("failed sign-out (%s) stays on the page and can be retried", async (kind) => { const signOut = vi.mocked(authClient.signOut); const error = { error: { status: 503, statusText: "Unavailable" }, data: null }; signOut.mockReset(); if (kind === "rejected") signOut.mockRejectedValueOnce(new Error("network failure")); else signOut.mockResolvedValueOnce(error); signOut.mockResolvedValueOnce(error); render(Inhalt); fireEvent.click(screen.getByRole("button", { name: "Abmelden" })); expect((await screen.findByRole("alert")).textContent).toMatch(/fehlgeschlagen/i); expect(screen.getByText("Inhalt")).toBeTruthy(); const retry = screen.getByRole("button", { name: "Abmelden" }); expect((retry as HTMLButtonElement).disabled).toBe(false); fireEvent.click(retry); expect(signOut).toHaveBeenCalledTimes(2); await screen.findByRole("alert"); });