Files
co-lite/solvway-admin-portal/app/components/pages/CmsBlocks.edit.test.tsx
T
ngqileandClaude Opus 5.5 68159086a9 fix(pages): empty rich text on the canvas gets room and a faded cue
A new Text section was a thin line under its Add section button, with nothing
to click or read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
2026-12-03 16:12:59 +08:00

123 lines
7.4 KiB
TypeScript

import { fireEvent, render, screen, within } from "@testing-library/react";
import { describe, it, expect, vi } from "vitest";
import { LandingPage, type PublicBlock } from "./CmsBlocks";
import { setField, type PageEdit } from "./CmsEdit";
// add-page-visual-editor: the page in edit mode. Text edits in place, sections select, images open the picker, and
// nothing navigates. Without `edit` the page renders exactly as before (CmsBlocks*.test.tsx).
const company = { code: "INS", name: "INS Sdn Bhd" };
const page = (blocks: PublicBlock[]) => ({ slug: "hotel", title: "Hotel day", blocks });
function editor(overrides: Partial<PageEdit> = {}): PageEdit {
return {
selected: null, select: vi.fn(), text: vi.fn(), html: vi.fn(), image: vi.fn(),
preview: () => undefined, stale: () => false, frameVersion: 0, chrome: () => null, ...overrides,
};
}
const typeInto = (el: HTMLElement, value: string) => { el.textContent = value; fireEvent.input(el); };
describe("LandingPage in edit mode", () => {
it("edits a heading, a card title and a FAQ answer in place as plain text", () => {
const edit = editor();
render(<LandingPage company={company} page={page([
{ type: "HERO", heading: "Hotel week" },
{ type: "FEATURES", heading: "Why", cards: [{ title: "A" }, { title: "B" }] },
{ type: "FAQ", items: [{ question: "Q", answer: "A1" }] },
])} edit={edit} preview />);
const heading = screen.getAllByRole("textbox", { name: "Heading" })[0];
expect(heading).toHaveAttribute("contenteditable", "plaintext-only");
expect(heading).toHaveTextContent("Hotel week");
typeInto(heading, "Hotel month");
expect(edit.text).toHaveBeenLastCalledWith(0, "heading", "Hotel month");
typeInto(screen.getAllByRole("textbox", { name: "Title" })[1], "Second");
expect(edit.text).toHaveBeenLastCalledWith(1, "cards.1.title", "Second");
typeInto(screen.getByRole("textbox", { name: "Answer 1" }), "Yes\nno");
expect(edit.text).toHaveBeenLastCalledWith(2, "items.0.answer", "Yes\nno");
});
it("offers an empty heading as a spot to type, and shows the limit once close to it", () => {
render(<LandingPage company={company} page={page([{ type: "CTA", heading: "x".repeat(100) }, { type: "FEATURES", cards: [{ title: "A" }, { title: "B" }] }])} edit={editor()} preview />);
expect(screen.getByText("100/120")).toBeInTheDocument();
expect(screen.getAllByRole("textbox", { name: "Heading" })[1]).toHaveTextContent("");
});
it("selects a section on click, outlines the selected one, and draws the editor's tools in it", () => {
const edit = editor({ selected: 1, chrome: (i) => <span>tools {i}</span> });
render(<LandingPage company={company} page={page([{ type: "CTA", heading: "One" }, { type: "CTA", heading: "Two" }])} edit={edit} preview />);
const sections = document.querySelectorAll("[data-index]");
expect([...sections].map((s) => s.getAttribute("data-index"))).toEqual(["0", "1"]);
expect(sections[1]).toHaveAttribute("data-selected", "true");
expect(sections[0]).not.toHaveAttribute("data-selected");
expect(within(sections[1] as HTMLElement).getByText("tools 1")).toBeInTheDocument();
fireEvent.click(sections[0]);
expect(edit.select).toHaveBeenCalledWith(0);
});
it("keeps links and buttons from navigating, and edits a button's label", () => {
const edit = editor();
render(<LandingPage company={company} page={page([{ type: "CTA", heading: "Go", buttonLabel: "Join", buttonUrl: "https://example.com" }])} edit={edit} preview />);
const label = screen.getByRole("textbox", { name: "Button label" });
const link = label.closest("a")!;
expect(fireEvent.click(link)).toBe(false); // default prevented
typeInto(label, "Join now");
expect(edit.text).toHaveBeenLastCalledWith(0, "buttonLabel", "Join now");
});
it("opens the picker from an image spot and shows a chosen image before it is saved", () => {
const edit = editor({ preview: (i) => (i === 0 ? "data:image/png;base64,AAAA" : undefined) });
render(<LandingPage company={company} page={page([{ type: "IMAGE_TEXT", heading: "Pic", imageId: 4 }, { type: "IMAGE_TEXT", heading: "None" }])} edit={edit} preview />);
const spots = screen.getAllByRole("button", { name: "Image" });
expect(spots).toHaveLength(2);
expect(spots[0].querySelector("img")).toHaveAttribute("src", "data:image/png;base64,AAAA");
fireEvent.click(spots[1]);
expect(edit.image).toHaveBeenCalledWith(1);
});
it("shows a Custom HTML block's saved version, selectable, marked while its HTML has unsaved edits", () => {
const edit = editor({ stale: (i) => i === 0, frameVersion: 3 });
render(<LandingPage company={company} page={page([{ type: "HTML", html: "<p>x</p>" }])} edit={edit} preview frameBase="/pages/5/preview/frame" />);
expect(document.querySelector("iframe")).toHaveAttribute("src", "/pages/5/preview/frame/0?v=3");
expect(screen.getByText("Save to update")).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Custom HTML" }));
expect(edit.select).toHaveBeenCalledWith(0);
});
it("edits rich text in place in the page's own style, for a Text block and an image-and-text block", async () => {
render(<LandingPage company={company} page={page([{ type: "RICH_TEXT", html: "<p>Body copy</p>" }, { type: "IMAGE_TEXT", heading: "Pic", html: "" }])} edit={editor()} preview />);
const boxes = await screen.findAllByRole("textbox", { name: "Content" });
expect(boxes).toHaveLength(2);
expect(boxes[0]).toHaveAttribute("contenteditable", "true");
expect(boxes[0]).toHaveTextContent("Body copy");
expect(screen.getAllByText("Body copy")).toHaveLength(1);
});
it("offers empty rich text as a spot to type: a faded cue that goes once there is text", async () => {
const { rerender } = render(<LandingPage company={company} page={page([{ type: "RICH_TEXT", html: "" }])} edit={editor()} preview />);
await screen.findByRole("textbox", { name: "Content" });
const cue = screen.getByText("Content", { selector: "p[aria-hidden]" });
expect(cue.parentElement!.className).toContain("[&_.ProseMirror]:min-h-24");
rerender(<LandingPage company={company} page={page([{ type: "RICH_TEXT", html: "<p>Hi</p>" }])} edit={editor()} preview />);
expect(screen.queryByText("Content", { selector: "p[aria-hidden]" })).not.toBeInTheDocument();
});
it("renders no editing without edit mode", () => {
render(<LandingPage company={company} page={page([{ type: "HERO", heading: "Hotel week" }, { type: "IMAGE_TEXT", heading: "Pic", imageId: 4 }])} />);
expect(document.querySelector("[contenteditable]")).toBeNull();
expect(document.querySelector("[data-index]")).toBeNull();
expect(screen.queryByRole("button")).toBeNull();
expect(screen.getByRole("heading", { level: 2, name: "Hotel week" })).toBeInTheDocument();
});
});
describe("setField", () => {
it("sets a top-level field or one inside a card or question, leaving the rest", () => {
const block = { type: "FEATURES" as const, heading: "H", cards: [{ title: "A", text: "a" }, { title: "B" }] };
expect(setField(block, "heading", "New")).toEqual({ ...block, heading: "New" });
expect(setField(block, "cards.1.title", "C").cards).toEqual([{ title: "A", text: "a" }, { title: "C" }]);
expect(block.cards[1].title).toBe("B");
expect(setField({ type: "FAQ", items: [{ question: "Q", answer: "A" }] }, "items.0.answer", "Z").items).toEqual([{ question: "Q", answer: "Z" }]);
});
});