Files
co-lite/solvway-admin-portal/app/components/pages/PageEditor.test.tsx
T
ngqileandClaude Opus 5.5 b55b8d2d86 feat(pages): a hero can run full width with points and a form card heading
A full-width hero spans the window with a larger serif headline and a lighter
left-to-right shade. It may list up to 6 points and give its form card a heading
and text. Full-width neighbours join with no gap. Custom HTML frames now shrink
to 20 px and re-report their height when the page asks, so a short top bar no
longer leaves a blank band.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
2026-12-04 09:46:32 +08:00

168 lines
9.7 KiB
TypeScript

import { render, screen, waitFor } 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(),
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 }));
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";
const stored = {
id: 5, companyId: 2, slug: "hotel", title: "Hotel", metaDescription: null, status: "PUBLISHED" as const,
blocks: [{ type: "HERO" as const, heading: "Hi", imageId: 9 }, { type: "FORM" as const, heading: "Join", eventUuid: "3312726793093644288" }],
};
beforeEach(() => {
vi.clearAllMocks();
memoryRouter.reset("/pages/5");
mocks.useAdminAuth.mockReturnValue({ isSuper: false, companyCode: "INS" });
mocks.useCompanyScope.mockReturnValue({ companyId: undefined, companies: [{ id: 2, code: "INS", name: "INS", status: "A" }], codeOf: (id?: number) => (id === 2 ? "INS" : "") });
mocks.listAdminEvents.mockResolvedValue({ items: [
{ uuid: "3312726793093644288", name: "Website enquiries", endDate: "2099-12-31", enabled: true, location: "", startDate: "" },
{ uuid: "1", name: "Old fair", endDate: "2020-01-01", enabled: true, location: "", startDate: "" },
], total: 2 });
mocks.getPage.mockResolvedValue(stored);
mocks.savePage.mockImplementation(async (id: number | undefined, body: object) => ({ ...stored, ...body, id: id ?? 7 }));
});
describe("PageEditor", () => {
it("copies the saved page's full address", async () => {
const user = userEvent.setup();
render(<PageEditor pageId={5} />);
await screen.findByLabelText("Event");
const write = vi.spyOn(navigator.clipboard, "writeText");
await user.click(screen.getByRole("button", { name: "Copy link /p/INS/hotel" }));
expect(write).toHaveBeenCalledWith(`${window.location.origin}/p/INS/hotel`);
});
it("names the rich text box after its field label", async () => {
const user = userEvent.setup();
render(<PageEditor pageId={5} />);
await screen.findByLabelText("Event");
await user.click(screen.getByRole("button", { name: "Add Text" }));
expect(await screen.findByRole("textbox", { name: "Content" })).toHaveAttribute("contenteditable", "true");
});
it("loads a page, offers only events that have not ended, and saves the blocks in order", async () => {
const user = userEvent.setup();
render(<PageEditor pageId={5} />);
const picker = await screen.findByLabelText("Event");
// Scoped to the FORM block's picker: since add-hero-signup-form the hero offers the same events in its own picker.
await waitFor(() => expect(within(picker).getByRole("option", { name: "Website enquiries" })).toBeInTheDocument());
expect(screen.queryByRole("option", { name: "Old fair" })).not.toBeInTheDocument();
expect(picker).toHaveValue("3312726793093644288");
expect(screen.getByRole("link", { name: "View page" })).toHaveAttribute("href", "/p/INS/hotel");
expect(screen.getByRole("link", { name: "Preview" })).toHaveAttribute("href", "/pages/5/preview");
await user.click(screen.getAllByRole("button", { name: "Move down" })[0]);
await user.click(screen.getByRole("button", { name: "Save" }));
await waitFor(() => expect(mocks.savePage).toHaveBeenCalled());
const [id, body] = mocks.savePage.mock.calls[0];
expect(id).toBe(5);
expect(body.blocks.map((b: { type: string }) => b.type)).toEqual(["FORM", "HERO"]);
expect(body.blocks[0].eventUuid).toBe("3312726793093644288");
expect(await screen.findByRole("status")).toHaveTextContent("Page saved");
});
it("shows a server refusal at its field", async () => {
mocks.savePage.mockRejectedValue(new ApiError("x", 422, { code: "PagesBadLink", details: { field: "blocks[0].buttonUrl" } }));
const user = userEvent.setup();
render(<PageEditor pageId={5} />);
await screen.findByLabelText("Event");
await user.click(screen.getByRole("button", { name: "Save" }));
const link = screen.getAllByLabelText("Button link")[0];
expect(await screen.findByText("Use a link that starts with https://, mailto:, tel:, / or #.")).toBeInTheDocument();
expect(link.closest("div")).toHaveTextContent("Use a link that starts with https://, mailto:, tel:, / or #.");
});
it("uploads a chosen image on save and puts its id on the block", async () => {
mocks.uploadPageImage.mockResolvedValue({ id: 44 });
const user = userEvent.setup();
render(<PageEditor pageId={5} />);
await screen.findByLabelText("Event");
await user.upload(screen.getByLabelText("Image"), new File(["png"], "hero.png", { type: "image/png" }));
await user.click(screen.getByRole("button", { name: "Save" }));
await waitFor(() => expect(mocks.savePage).toHaveBeenCalled());
expect(mocks.uploadPageImage).toHaveBeenCalledWith(2, expect.any(String));
expect(mocks.savePage.mock.calls[0][1].blocks[0].imageId).toBe(44);
});
it("asks before deleting", async () => {
mocks.deletePage.mockResolvedValue(undefined);
const user = userEvent.setup();
render(<PageEditor pageId={5} />);
await user.click(await screen.findByRole("button", { name: "Delete page" }));
expect(mocks.deletePage).not.toHaveBeenCalled();
await user.click(within(screen.getByRole("dialog")).getByRole("button", { name: "Delete page" }));
await waitFor(() => expect(mocks.deletePage).toHaveBeenCalledWith(5));
expect(memoryRouter.push).toHaveBeenCalledWith("/pages");
});
// add-hero-signup-form: the hero's own event picker, "No form" by default; its event is saved with the hero.
it("gives a hero a sign-up form for a chosen event, and No form takes it away", async () => {
const user = userEvent.setup();
render(<PageEditor pageId={5} />);
const picker = await screen.findByLabelText("Sign-up form");
expect(picker).toHaveValue("");
expect(within(picker).getByRole("option", { name: "No form" })).toBeInTheDocument();
await waitFor(() => expect(within(picker).getByRole("option", { name: "Website enquiries" })).toBeInTheDocument());
await user.selectOptions(picker, "3312726793093644288");
await user.click(screen.getByRole("button", { name: "Save" }));
await waitFor(() => expect(mocks.savePage).toHaveBeenCalledTimes(1));
expect(mocks.savePage.mock.calls[0][1].blocks[0]).toMatchObject({ type: "HERO", eventUuid: "3312726793093644288" });
await user.selectOptions(screen.getByLabelText("Sign-up form"), "");
await user.click(screen.getByRole("button", { name: "Save" }));
await waitFor(() => expect(mocks.savePage).toHaveBeenCalledTimes(2));
expect(mocks.savePage.mock.calls[1][1].blocks[0].eventUuid ?? null).toBeNull();
});
// add-hero-full-width: Full width, points, and the form card's heading and text (only while the hero has a form).
it("saves a full-width hero with points and a form heading; No form drops the heading and text", async () => {
const user = userEvent.setup();
render(<PageEditor pageId={5} />);
const picker = await screen.findByLabelText("Sign-up form");
expect(screen.queryByLabelText("Form heading")).not.toBeInTheDocument();
await user.click(screen.getByRole("switch", { name: "Full width" }));
await user.click(screen.getByRole("button", { name: "Add point" }));
await user.type(screen.getByRole("textbox", { name: "Point 1" }), "Local support team");
await waitFor(() => expect(within(picker).getByRole("option", { name: "Website enquiries" })).toBeInTheDocument());
await user.selectOptions(picker, "3312726793093644288");
await user.type(screen.getByLabelText("Form heading"), "Get a free consultation");
await user.type(screen.getByLabelText("Form text"), "It takes a minute.");
await user.click(screen.getByRole("button", { name: "Save" }));
await waitFor(() => expect(mocks.savePage).toHaveBeenCalledTimes(1));
expect(mocks.savePage.mock.calls[0][1].blocks[0]).toMatchObject({
type: "HERO", fullWidth: true, cards: [{ title: "Local support team" }],
formHeading: "Get a free consultation", formText: "It takes a minute.",
});
await user.selectOptions(screen.getByLabelText("Sign-up form"), "");
expect(screen.queryByLabelText("Form heading")).not.toBeInTheDocument();
await user.click(screen.getByRole("button", { name: "Save" }));
await waitFor(() => expect(mocks.savePage).toHaveBeenCalledTimes(2));
const hero = mocks.savePage.mock.calls[1][1].blocks[0];
expect([hero.formHeading, hero.formText]).toEqual([undefined, undefined]);
});
it("a super admin must choose a company for a new page", async () => {
mocks.useAdminAuth.mockReturnValue({ isSuper: true, companyCode: null });
const user = userEvent.setup();
render(<PageEditor />);
await user.type(screen.getByLabelText("Title"), "Hotel Day");
expect(screen.getByLabelText("Slug")).toHaveValue("hotel-day");
await user.click(screen.getByRole("button", { name: "Save" }));
expect(await screen.findByText("Choose a company.")).toBeInTheDocument();
expect(mocks.savePage).not.toHaveBeenCalled();
});
});
import { within } from "@testing-library/react";