The toolbar hung 20 px above the selected section, so it was cut off for the first section and for any section scrolled to the top of the canvas. It now sits inside the section's top-right corner, and the hero's image button moves to the top-left so the two never overlap. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
219 lines
12 KiB
TypeScript
219 lines
12 KiB
TypeScript
import { fireEvent, render, screen, waitFor, within } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { describe, it, expect, vi, beforeEach, afterEach } 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", () => mocks);
|
|
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-visual-editor: the on-page editor at 1024 px and up.
|
|
const stored = {
|
|
id: 5, companyId: 2, slug: "hotel", title: "Hotel", metaDescription: null, status: "DRAFT" as const,
|
|
company: { code: "INS", name: "INS Sdn Bhd", logoImageId: null, accentColor: null },
|
|
blocks: [
|
|
{ type: "HERO" as const, heading: "Hi" },
|
|
{ type: "FEATURES" as const, heading: "Why", cards: [{ title: "Fast" }, { title: "Local" }, { title: "Safe" }] },
|
|
{ type: "FAQ" as const, heading: "Questions", items: [{ question: "Parking?", answer: "Yes" }] },
|
|
{ type: "FORM" as const, heading: "Join", eventUuid: null },
|
|
],
|
|
};
|
|
|
|
const wide = (matches: boolean) => Object.defineProperty(window, "matchMedia", {
|
|
writable: true, configurable: true,
|
|
value: (query: string) => ({ matches, media: query, onchange: null, addListener: () => {}, removeListener: () => {}, addEventListener: () => {}, removeEventListener: () => {}, dispatchEvent: () => false }),
|
|
});
|
|
const original = window.matchMedia;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
wide(true);
|
|
memoryRouter.reset("/pages/5");
|
|
mocks.useAdminAuth.mockReturnValue({ isSuper: false, companyCode: "INS" });
|
|
mocks.useCompanyScope.mockReturnValue({ companyId: undefined, companies: [], codeOf: () => "INS" });
|
|
mocks.listAdminEvents.mockResolvedValue({ items: [{ uuid: "77", name: "Hotel week", endDate: "2099-12-31", enabled: true, location: "", startDate: "" }], total: 1 });
|
|
mocks.getPage.mockResolvedValue(stored);
|
|
mocks.savePage.mockImplementation(async (id: number, body: object) => ({ ...stored, ...body, id }));
|
|
});
|
|
afterEach(() => { window.matchMedia = original; });
|
|
|
|
const frame = () => screen.getByTitle("Page canvas") as HTMLIFrameElement;
|
|
async function open() {
|
|
const view = render(<PageEditor pageId={5} />);
|
|
await waitFor(() => expect(frame().contentDocument!.querySelector("[data-index]")).not.toBeNull());
|
|
return Object.assign(within(frame().contentDocument!.body), { rerender: view.rerender });
|
|
}
|
|
const section = (i: number) => frame().contentDocument!.querySelector(`[data-index="${i}"]`) as HTMLElement;
|
|
const panel = () => screen.getByRole("complementary");
|
|
const typeInto = (el: HTMLElement, value: string) => { el.focus(); el.textContent = value; fireEvent.input(el); };
|
|
|
|
describe("VisualEditor", () => {
|
|
it("keeps the form editor in a narrow window", async () => {
|
|
wide(false);
|
|
render(<PageEditor pageId={5} />);
|
|
expect((await screen.findAllByLabelText("Title"))[0]).toHaveValue("Hotel");
|
|
expect(screen.queryByTitle("Page canvas")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("shows the page on the canvas with the page settings beside it, and Desktop or Phone", async () => {
|
|
const canvas = await open();
|
|
expect(canvas.getByRole("heading", { level: 1, name: "Hotel" })).toBeInTheDocument();
|
|
expect(within(panel()).getByLabelText("Title")).toHaveValue("Hotel");
|
|
expect(frame()).toHaveStyle({ width: "1280px" });
|
|
await userEvent.setup().click(screen.getByRole("button", { name: "Phone" }));
|
|
expect(frame()).toHaveStyle({ width: "390px" });
|
|
});
|
|
|
|
it("edits a heading in place: the canvas and the side panel both show it", async () => {
|
|
const canvas = await open();
|
|
typeInto(canvas.getAllByRole("textbox", { name: "Heading" })[0], "Hotel week");
|
|
fireEvent.click(section(0));
|
|
expect(within(panel()).getAllByRole("textbox").find((el) => (el as HTMLInputElement).value === "Hotel week")).toBeTruthy();
|
|
});
|
|
|
|
it("selects a section on click and shows its settings; a change there shows on the canvas at once", async () => {
|
|
const user = userEvent.setup();
|
|
const canvas = await open();
|
|
fireEvent.click(section(1));
|
|
expect(section(1)).toHaveAttribute("data-selected", "true");
|
|
const title = within(panel()).getAllByLabelText("Title")[0];
|
|
await user.clear(title);
|
|
await user.type(title, "Quick");
|
|
expect(canvas.getAllByRole("textbox", { name: "Title" })[0]).toHaveTextContent("Quick");
|
|
await user.click(within(panel()).getByRole("button", { name: "Page settings" }));
|
|
expect(within(panel()).getByLabelText("Title")).toHaveValue("Hotel");
|
|
});
|
|
|
|
it("duplicates the selected section as the next one and selects the copy", async () => {
|
|
const canvas = await open();
|
|
fireEvent.click(section(1));
|
|
fireEvent.click(canvas.getByRole("button", { name: "Duplicate section" }));
|
|
expect(frame().contentDocument!.querySelectorAll("[data-index]")).toHaveLength(5);
|
|
expect(section(2)).toHaveAttribute("data-selected", "true");
|
|
expect(within(section(2)).getAllByRole("textbox", { name: "Heading" })[0]).toHaveTextContent("Why");
|
|
});
|
|
|
|
it("adds a section between two and selects it", async () => {
|
|
const canvas = await open();
|
|
fireEvent.click(within(section(0)).getByRole("button", { name: "Add section" }));
|
|
fireEvent.click(canvas.getByRole("menuitem", { name: "Steps" }));
|
|
expect(section(1)).toHaveAttribute("data-selected", "true");
|
|
expect(within(panel()).getByRole("group", { name: /Steps/ })).toBeInTheDocument();
|
|
});
|
|
|
|
it("moves a section with the toolbar's keyboard-reachable buttons", async () => {
|
|
const canvas = await open();
|
|
fireEvent.click(section(2));
|
|
fireEvent.click(canvas.getByRole("button", { name: "Move up" }));
|
|
expect(within(section(1)).getAllByRole("textbox", { name: "Heading" })[0]).toHaveTextContent("Questions");
|
|
expect(section(1)).toHaveAttribute("data-selected", "true");
|
|
});
|
|
|
|
// A toolbar hung above the section was clipped at the canvas top (first section, or a section scrolled to the top).
|
|
it("keeps the section toolbar inside the section, clear of the hero's image button", async () => {
|
|
const canvas = await open();
|
|
fireEvent.click(section(0));
|
|
const bar = canvas.getByRole("toolbar");
|
|
expect(bar.className).not.toMatch(/(^|\s)-top-/);
|
|
expect(bar.className).toMatch(/(^|\s)top-2(\s|$)/);
|
|
expect(bar.className).toMatch(/(^|\s)right-2(\s|$)/);
|
|
expect(within(section(0)).getByRole("button", { name: "Image" }).className).toMatch(/(^|\s)left-3(\s|$)/);
|
|
});
|
|
|
|
it("undoes a delete with Ctrl+Z and redoes it with Ctrl+Shift+Z", async () => {
|
|
const canvas = await open();
|
|
fireEvent.click(section(2));
|
|
fireEvent.click(canvas.getByRole("button", { name: "Remove block" }));
|
|
expect(canvas.queryByText("Parking?")).not.toBeInTheDocument();
|
|
fireEvent.keyDown(section(0), { key: "z", ctrlKey: true });
|
|
expect(within(section(2)).getByRole("textbox", { name: "Question 1" })).toHaveTextContent("Parking?");
|
|
fireEvent.keyDown(section(0), { key: "Z", ctrlKey: true, shiftKey: true });
|
|
expect(canvas.queryByText("Parking?")).not.toBeInTheDocument();
|
|
await userEvent.setup().click(screen.getByRole("button", { name: "Undo" }));
|
|
expect(canvas.getByRole("textbox", { name: "Question 1" })).toHaveTextContent("Parking?");
|
|
});
|
|
|
|
it("undoes with Ctrl+Z when nothing is focused (a button that just went disabled lets focus fall to the page)", async () => {
|
|
const canvas = await open();
|
|
fireEvent.click(section(2));
|
|
fireEvent.click(canvas.getByRole("button", { name: "Remove block" }));
|
|
fireEvent.keyDown(document.body, { key: "z", ctrlKey: true });
|
|
expect(canvas.getByRole("textbox", { name: "Question 1" })).toHaveTextContent("Parking?");
|
|
});
|
|
|
|
it("undoes with Ctrl+Z right after a toolbar delete, when focus falls to the canvas", async () => {
|
|
const canvas = await open();
|
|
fireEvent.click(section(2));
|
|
fireEvent.click(canvas.getByRole("button", { name: "Remove block" }));
|
|
fireEvent.keyDown(frame().contentDocument!.body, { key: "z", ctrlKey: true });
|
|
expect(canvas.getByRole("textbox", { name: "Question 1" })).toHaveTextContent("Parking?");
|
|
});
|
|
|
|
it("undoes one step per Ctrl+Z from the editor's own buttons", async () => {
|
|
const canvas = await open();
|
|
fireEvent.click(section(1));
|
|
fireEvent.click(canvas.getByRole("button", { name: "Remove block" }));
|
|
fireEvent.click(section(1));
|
|
fireEvent.click(canvas.getByRole("button", { name: "Remove block" }));
|
|
fireEvent.keyDown(screen.getByRole("button", { name: "Save" }), { key: "z", ctrlKey: true });
|
|
expect(canvas.getByText("Parking?")).toBeInTheDocument();
|
|
expect(canvas.queryAllByRole("textbox", { name: "Title" })).toHaveLength(0);
|
|
});
|
|
|
|
it("selects, outlines and explains a refused form: the message is at the Event field", async () => {
|
|
mocks.savePage.mockRejectedValueOnce(new ApiError("x", 422, { code: "PagesEventRequired", details: { field: "blocks[3].eventUuid" } }));
|
|
await open();
|
|
await userEvent.setup().click(screen.getByRole("button", { name: "Save" }));
|
|
await waitFor(() => expect(section(3)).toHaveAttribute("data-selected", "true"));
|
|
expect(within(panel()).getByLabelText("Event")).toBeInTheDocument();
|
|
expect(within(panel()).getByRole("alert")).toBeInTheDocument();
|
|
});
|
|
|
|
it("marks a Custom HTML block 'Save to update' while its HTML is unsaved, and reloads it after Save", async () => {
|
|
const user = userEvent.setup();
|
|
mocks.getPage.mockResolvedValue({ ...stored, blocks: [{ type: "HTML", html: "<p>Old</p>" }] });
|
|
const canvas = await open();
|
|
const sealed = () => frame().contentDocument!.querySelector("iframe[sandbox]") as HTMLIFrameElement;
|
|
const before = sealed().getAttribute("src");
|
|
expect(before).toMatch(/^\/pages\/5\/preview\/frame\/0\?v=\d+$/);
|
|
expect(canvas.queryByText("Save to update")).not.toBeInTheDocument();
|
|
fireEvent.click(canvas.getByRole("button", { name: "Custom HTML" }));
|
|
const box = within(panel()).getByRole("textbox", { name: /HTML/ });
|
|
await user.clear(box);
|
|
await user.type(box, "New");
|
|
expect(canvas.getByText("Save to update")).toBeInTheDocument();
|
|
await user.click(screen.getByRole("button", { name: "Save" }));
|
|
await waitFor(() => expect(canvas.queryByText("Save to update")).not.toBeInTheDocument());
|
|
expect(sealed().getAttribute("src")).not.toBe(before);
|
|
});
|
|
|
|
it("opens a template and a duplicate on the canvas", async () => {
|
|
const { unmount } = render(<PageEditor initialBlocks={[{ type: "CTA", heading: "Sign up today" }]} />);
|
|
await waitFor(() => expect(within(frame().contentDocument!.body).getByRole("textbox", { name: "Heading" })).toHaveTextContent("Sign up today"));
|
|
unmount();
|
|
render(<PageEditor fromId={5} />);
|
|
await waitFor(() => expect(within(panel()).getByLabelText("Title")).toHaveValue("Hotel (copy)"));
|
|
await waitFor(() => expect(section(1)).not.toBeNull());
|
|
expect(screen.queryByRole("button", { name: "Delete" })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("keeps unsaved edits when the window narrows to the form editor", async () => {
|
|
const canvas = await open();
|
|
typeInto(canvas.getAllByRole("textbox", { name: "Heading" })[0], "Hotel week");
|
|
wide(false);
|
|
canvas.rerender(<PageEditor pageId={5} />);
|
|
expect(screen.queryByTitle("Page canvas")).not.toBeInTheDocument();
|
|
expect(screen.getAllByLabelText("Heading")[0]).toHaveValue("Hotel week");
|
|
expect(mocks.getPage).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|