import { describe, it, expect, vi, beforeEach } from "vitest"; import { render, screen, within } from "@testing-library/react"; import type { CmsFormListItem } from "@/app/lib/cmsForms"; // add-cms-forms Task 9: the /forms list (specs/admin-portal-cms-forms-ui "The Forms List Shows Every Form"). const auth = vi.hoisted(() => ({ hasModule: vi.fn(), loading: false })); vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: () => ({ hasModule: auth.hasModule, loading: auth.loading }), })); const svc = vi.hoisted(() => ({ listForms: vi.fn() })); vi.mock("@/app/lib/services/adminForms", () => ({ listForms: () => svc.listForms(), })); import FormsPage from "./page"; const rows: CmsFormListItem[] = [ { id: 3, name: "Contact us", status: "Y", fieldCount: 4, submissionCount: 2, updatedAt: "2026-10-05T10:15:00" }, { id: 4, name: "Distributor", status: "N", fieldCount: 7, submissionCount: 0, updatedAt: "2026-10-06T08:00:00" }, ]; beforeEach(() => { auth.hasModule.mockReset(); auth.hasModule.mockReturnValue(true); svc.listForms.mockReset(); }); describe("/forms", () => { it("lists each form with its counts, status and links", async () => { svc.listForms.mockResolvedValue(rows); render(); const table = await screen.findByRole("table", { name: "Forms" }); const contact = within(table).getByRole("row", { name: /Contact us/ }); expect(within(contact).getByRole("link", { name: "Contact us" })).toHaveAttribute("href", "/forms/3"); expect(within(contact).getByText("4")).toBeInTheDocument(); expect(within(contact).getByRole("link", { name: "2 submissions" })).toHaveAttribute("href", "/forms/3/submissions"); expect(within(contact).getByText("Visible")).toBeInTheDocument(); expect(within(contact).getByText(/5 Oct 2026/)).toBeInTheDocument(); const distributor = within(table).getByRole("row", { name: /Distributor/ }); expect(within(distributor).getByText("Hidden")).toBeInTheDocument(); expect(within(distributor).getByRole("link", { name: "0 submissions" })).toHaveAttribute("href", "/forms/4/submissions"); expect(screen.getByRole("link", { name: /new form/i })).toHaveAttribute("href", "/forms/new"); }); it("says so when there are no forms", async () => { svc.listForms.mockResolvedValue([]); render(); expect(await screen.findByText("No forms yet.")).toBeInTheDocument(); }); it("shows a load failure", async () => { svc.listForms.mockRejectedValue(new Error("Boom")); render(); expect(await screen.findByText("Boom")).toBeInTheDocument(); }); });