import { render, screen, waitFor, within } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { describe, it, expect, vi, beforeEach } from "vitest"; import { ApiError } from "@/app/lib/clientApi"; const mocks = vi.hoisted(() => ({ getPage: vi.fn(), savePage: vi.fn(), deletePage: vi.fn(), uploadPageImage: vi.fn(), publishPage: vi.fn(), discardPage: vi.fn(), listAdminEvents: vi.fn(), useAdminAuth: vi.fn(), useCompanyScope: vi.fn(), })); vi.mock("@/app/lib/services/adminPages", () => ({ getPage: mocks.getPage, savePage: mocks.savePage, deletePage: mocks.deletePage, uploadPageImage: mocks.uploadPageImage, publishPage: mocks.publishPage, discardPage: mocks.discardPage, })); vi.mock("@/app/lib/services/adminEvents", () => ({ listAdminEvents: mocks.listAdminEvents })); vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: mocks.useAdminAuth })); vi.mock("@/app/lib/CompanyScope", () => ({ useCompanyScope: mocks.useCompanyScope })); vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock); import { memoryRouter } from "@/test/memoryRouter"; import { PageEditor } from "./PageEditor"; // add-page-drafts-and-templates: a published page's edits stay a draft until Publish changes. const live = { id: 5, companyId: 2, slug: "hotel", title: "Hotel day", metaDescription: null, status: "PUBLISHED" as const, hideHeader: false, blocks: [{ type: "CTA" as const, heading: "Live heading", buttonLabel: "Go", buttonUrl: "#signup" }], draft: null, hasDraft: false, }; const draft = { slug: "hotel-week", title: "Hotel week", metaDescription: null, hideHeader: false, blocks: [{ type: "CTA" as const, heading: "Draft heading", buttonLabel: "Go", buttonUrl: "#signup" }] }; const withDraft = { ...live, draft, hasDraft: true }; beforeEach(() => { vi.clearAllMocks(); memoryRouter.reset("/pages/5"); mocks.useAdminAuth.mockReturnValue({ isSuper: false, companyCode: "INS" }); mocks.useCompanyScope.mockReturnValue({ companyId: undefined, companies: [], codeOf: (id?: number) => (id === 2 ? "INS" : "") }); mocks.listAdminEvents.mockResolvedValue({ items: [], total: 0 }); mocks.getPage.mockResolvedValue(live); mocks.savePage.mockResolvedValue(withDraft); mocks.publishPage.mockResolvedValue({ ...live, slug: "hotel-week", title: "Hotel week", blocks: draft.blocks }); mocks.discardPage.mockResolvedValue(live); }); describe("PageEditor drafts", () => { it("edits the draft when there is one, while View page stays on the live address", async () => { mocks.getPage.mockResolvedValue(withDraft); render(); expect(await screen.findByLabelText("Title")).toHaveValue("Hotel week"); expect(screen.getByLabelText("Heading")).toHaveValue("Draft heading"); expect(screen.getByRole("link", { name: "View page" })).toHaveAttribute("href", "/p/INS/hotel"); expect(screen.getByText(/Visitors still see the published version/)).toBeInTheDocument(); }); it("saving a published page shows the banner, and Publish changes saves then publishes", async () => { const user = userEvent.setup(); render(); expect(await screen.findByLabelText("Title")).toHaveValue("Hotel day"); expect(screen.queryByText(/Visitors still see the published version/)).not.toBeInTheDocument(); await user.clear(screen.getByLabelText("Title")); await user.type(screen.getByLabelText("Title"), "Hotel week"); const scroll = vi.fn(); Element.prototype.scrollIntoView = scroll; await user.click(screen.getByRole("button", { name: "Save" })); expect(await screen.findByText(/Visitors still see the published version/)).toBeInTheDocument(); // Save sits at the bottom of a long form: the banner is brought into view. await waitFor(() => expect(scroll).toHaveBeenCalled()); expect(scroll.mock.contexts[0]).toHaveTextContent(/Visitors still see the published version/); expect(screen.getByLabelText("Title")).toHaveValue("Hotel week"); await user.click(screen.getByRole("button", { name: "Publish changes" })); await waitFor(() => expect(mocks.publishPage).toHaveBeenCalledWith(5)); expect(mocks.savePage).toHaveBeenCalledTimes(2); expect(mocks.savePage.mock.invocationCallOrder[1]).toBeLessThan(mocks.publishPage.mock.invocationCallOrder[0]); await waitFor(() => expect(screen.queryByText(/Visitors still see the published version/)).not.toBeInTheDocument()); expect(screen.getByRole("status")).toHaveTextContent("Changes published"); expect(screen.getByRole("link", { name: "View page" })).toHaveAttribute("href", "/p/INS/hotel-week"); }); it("offers Duplicate for a saved page", async () => { render(); expect(await screen.findByRole("link", { name: "Duplicate" })).toHaveAttribute("href", "/pages/new?from=5"); }); it("a publish refusal shows at its field and keeps the banner", async () => { mocks.getPage.mockResolvedValue(withDraft); mocks.publishPage.mockRejectedValue(new ApiError("x", 409, { code: "PagesSlugTaken", details: { field: "slug" } })); const user = userEvent.setup(); render(); await user.click(await screen.findByRole("button", { name: "Publish changes" })); expect(await screen.findByRole("alert")).toHaveTextContent("Another page already uses this slug."); expect(screen.getByText(/Visitors still see the published version/)).toBeInTheDocument(); }); it("Discard changes asks first, then shows the live page again", async () => { mocks.getPage.mockResolvedValue(withDraft); const user = userEvent.setup(); render(); await user.click(await screen.findByRole("button", { name: "Discard changes" })); expect(mocks.discardPage).not.toHaveBeenCalled(); await user.click(within(screen.getByRole("dialog")).getByRole("button", { name: "Discard changes" })); await waitFor(() => expect(mocks.discardPage).toHaveBeenCalledWith(5)); expect(await screen.findByLabelText("Title")).toHaveValue("Hotel day"); expect(screen.getByLabelText("Heading")).toHaveValue("Live heading"); expect(screen.queryByText(/Visitors still see the published version/)).not.toBeInTheDocument(); }); });