Landing pages gain a Top bar section (the company logo and an optional button, kept at the top of the window, first section only), a short label above the headings of five section types, and serif section headings. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
141 lines
8.3 KiB
TypeScript
141 lines
8.3 KiB
TypeScript
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";
|
|
|
|
// add-page-sections: the new section types, their cards and the look picker in the form editor.
|
|
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: "DRAFT" as const, hideHeader: false,
|
|
blocks: [
|
|
{ type: "STEPS" as const, heading: "How it works", cards: [{ title: "Survey", text: "We visit." }, { title: "Install", text: "We fit it." }] },
|
|
{ type: "HTML" as const, html: "<p>x</p>" },
|
|
],
|
|
};
|
|
|
|
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: [], total: 0 });
|
|
mocks.getPage.mockResolvedValue(stored);
|
|
mocks.savePage.mockImplementation(async (id: number | undefined, body: object) => ({ ...stored, ...body, id: id ?? 7 }));
|
|
});
|
|
|
|
const group = (name: string) => screen.getByRole("group", { name });
|
|
|
|
describe("PageEditor: sections", () => {
|
|
it("adds feature cards with icons and a look, and saves them", async () => {
|
|
const user = userEvent.setup();
|
|
render(<PageEditor pageId={5} />);
|
|
await screen.findByDisplayValue("How it works");
|
|
await user.click(screen.getByRole("button", { name: "Add Feature cards" }));
|
|
const block = group("Block 3: Feature cards");
|
|
await user.type(within(block).getByLabelText("Heading"), "What we do");
|
|
await user.type(within(block).getByLabelText("Intro"), "One partner.");
|
|
const card1 = within(block).getByRole("group", { name: "Card 1" });
|
|
await user.selectOptions(within(card1).getByLabelText("Icon"), "WiFi");
|
|
await user.type(within(card1).getByLabelText("Title"), "Managed WiFi");
|
|
await user.type(within(card1).getByLabelText("Text"), "Guests connect.");
|
|
const card2 = within(block).getByRole("group", { name: "Card 2" });
|
|
await user.type(within(card2).getByLabelText("Title"), "TV");
|
|
await user.type(within(card2).getByLabelText("Text"), "Casting.");
|
|
await user.selectOptions(within(block).getByLabelText("Look"), "Dark");
|
|
await user.click(screen.getByRole("button", { name: "Save" }));
|
|
await waitFor(() => expect(mocks.savePage).toHaveBeenCalled());
|
|
expect(mocks.savePage.mock.calls[0][1].blocks[2]).toEqual({
|
|
type: "FEATURES", heading: "What we do", text: "One partner.", look: "DARK",
|
|
cards: [{ icon: "wifi", title: "Managed WiFi", text: "Guests connect." }, { icon: "star", title: "TV", text: "Casting." }],
|
|
});
|
|
});
|
|
|
|
it("keeps 2 to 6 cards and moves them", async () => {
|
|
const user = userEvent.setup();
|
|
render(<PageEditor pageId={5} />);
|
|
await screen.findByDisplayValue("How it works");
|
|
const block = group("Block 1: Steps");
|
|
expect(within(within(block).getByRole("group", { name: "Step 1" })).getByRole("button", { name: "Remove" })).toBeDisabled();
|
|
await user.click(within(within(block).getByRole("group", { name: "Step 2" })).getByRole("button", { name: "Move up" }));
|
|
expect(within(within(block).getByRole("group", { name: "Step 1" })).getByLabelText("Title")).toHaveValue("Install");
|
|
const add = within(block).getByRole("button", { name: "Add step" });
|
|
for (let i = 0; i < 4; i++) await user.click(add);
|
|
expect(add).toBeDisabled();
|
|
expect(within(within(block).getByRole("group", { name: "Step 6" })).getByRole("button", { name: "Remove" })).toBeEnabled();
|
|
});
|
|
|
|
// add-landing-thanks-and-footer: a dark footer of 1 to 4 columns with a legal line.
|
|
it("adds a dark footer with columns and a legal line, and keeps 1 to 4 columns", async () => {
|
|
const user = userEvent.setup();
|
|
render(<PageEditor pageId={5} />);
|
|
await screen.findByDisplayValue("How it works");
|
|
await user.click(screen.getByRole("button", { name: "Add Footer" }));
|
|
const block = group("Block 3: Footer");
|
|
expect(within(block).getByLabelText("Look")).toHaveValue("DARK");
|
|
const col1 = within(block).getByRole("group", { name: "Column 1" });
|
|
await user.type(within(col1).getByLabelText("Title"), "Talk to us");
|
|
await user.type(within(col1).getByLabelText("Text"), "+603-7623 9807");
|
|
await user.click(within(within(block).getByRole("group", { name: "Column 2" })).getByRole("button", { name: "Remove" }));
|
|
expect(within(col1).getByRole("button", { name: "Remove" })).toBeDisabled();
|
|
await user.type(within(block).getByLabelText("Legal line"), "© 2026 INS");
|
|
const add = within(block).getByRole("button", { name: "Add column" });
|
|
for (let i = 0; i < 3; i++) await user.click(add);
|
|
expect(add).toBeDisabled();
|
|
for (const n of [4, 3, 2]) await user.click(within(within(block).getByRole("group", { name: `Column ${n}` })).getByRole("button", { name: "Remove" }));
|
|
await user.click(screen.getByRole("button", { name: "Save" }));
|
|
await waitFor(() => expect(mocks.savePage).toHaveBeenCalled());
|
|
expect(mocks.savePage.mock.calls[0][1].blocks[2]).toEqual({
|
|
type: "FOOTER", look: "DARK", text: "© 2026 INS", cards: [{ title: "Talk to us", text: "+603-7623 9807" }],
|
|
});
|
|
});
|
|
|
|
// add-landing-page-polish: the top bar's button, and a label above a section's heading.
|
|
it("adds a top bar with a button and no look, and saves a section label", async () => {
|
|
const user = userEvent.setup();
|
|
render(<PageEditor pageId={5} />);
|
|
await screen.findByDisplayValue("How it works");
|
|
await user.type(within(group("Block 1: Steps")).getByLabelText("Label"), "How we work");
|
|
await user.click(screen.getByRole("button", { name: "Add Top bar" }));
|
|
const block = group("Block 3: Top bar");
|
|
expect(within(block).queryByLabelText("Look")).not.toBeInTheDocument();
|
|
expect(within(block).queryByLabelText("Label")).not.toBeInTheDocument();
|
|
await user.type(within(block).getByLabelText("Button label"), "Get My Free Consultation");
|
|
await user.type(within(block).getByLabelText("Button link"), "#consult");
|
|
await user.click(screen.getByRole("button", { name: "Save" }));
|
|
await waitFor(() => expect(mocks.savePage).toHaveBeenCalled());
|
|
const blocks = mocks.savePage.mock.calls[0][1].blocks;
|
|
expect(blocks[0]).toMatchObject({ type: "STEPS", eyebrow: "How we work" });
|
|
expect(blocks[2]).toEqual({ type: "TOPBAR", buttonLabel: "Get My Free Consultation", buttonUrl: "#consult" });
|
|
});
|
|
|
|
it("offers no look for a Custom HTML block", async () => {
|
|
render(<PageEditor pageId={5} />);
|
|
await screen.findByDisplayValue("How it works");
|
|
expect(within(group("Block 1: Steps")).getByLabelText("Look")).toBeInTheDocument();
|
|
expect(within(group("Block 2: Custom HTML")).queryByLabelText("Look")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("shows a refused card field at that card", async () => {
|
|
mocks.savePage.mockRejectedValue(new ApiError("x", 422, { code: "PagesTooLong", details: { field: "blocks[0].cards[1].title" } }));
|
|
const user = userEvent.setup();
|
|
render(<PageEditor pageId={5} />);
|
|
await screen.findByDisplayValue("How it works");
|
|
await user.click(screen.getByRole("button", { name: "Save" }));
|
|
const step2 = within(group("Block 1: Steps")).getByRole("group", { name: "Step 2" });
|
|
expect(await within(step2).findByText("Too long.")).toBeInTheDocument();
|
|
expect(within(step2).getByLabelText("Title")).toHaveAttribute("aria-invalid", "true");
|
|
});
|
|
});
|