Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
149 lines
7.1 KiB
TypeScript
149 lines
7.1 KiB
TypeScript
import { act, renderHook, waitFor } from "@testing-library/react";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { ApiError } from "@/app/lib/clientApi";
|
|
|
|
const mocks = vi.hoisted(() => ({ getPage: vi.fn(), savePage: vi.fn(), listAdminEvents: vi.fn(), useAdminAuth: vi.fn(), useCompanyScope: vi.fn() }));
|
|
vi.mock("@/app/lib/services/adminPages", () => ({ getPage: mocks.getPage, savePage: mocks.savePage }));
|
|
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 { moveItem, usePageDraft } from "./usePageDraft";
|
|
|
|
// add-page-visual-editor: the on-page editor's selection, section actions, undo and refusal mapping.
|
|
const stored = {
|
|
id: 5, companyId: 2, slug: "hotel", title: "Hotel", metaDescription: null, status: "DRAFT" as const,
|
|
company: { code: "INS", name: "INS Sdn Bhd", logoImageId: 4, accentColor: null },
|
|
blocks: [
|
|
{ type: "HERO" as const, heading: "Hi" },
|
|
{ type: "FAQ" as const, items: [{ question: "Q", answer: "A" }] },
|
|
{ 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: [], codeOf: () => "INS" });
|
|
mocks.listAdminEvents.mockResolvedValue({ items: [], total: 0 });
|
|
mocks.getPage.mockResolvedValue(stored);
|
|
mocks.savePage.mockImplementation(async (id: number, body: object) => ({ ...stored, ...body, id }));
|
|
});
|
|
|
|
async function loaded() {
|
|
const hook = renderHook(() => usePageDraft({ pageId: 5 }));
|
|
await waitFor(() => expect(hook.result.current.loading).toBe(false));
|
|
return hook;
|
|
}
|
|
const types = (r: { current: ReturnType<typeof usePageDraft> }) => r.current.rows.map((row) => row.block.type);
|
|
|
|
describe("moveItem", () => {
|
|
it("moves one item to a new place, keeping the others in order", () => {
|
|
expect(moveItem(["a", "b", "c", "d"], 0, 2)).toEqual(["b", "c", "a", "d"]);
|
|
expect(moveItem(["a", "b", "c", "d"], 3, 1)).toEqual(["a", "d", "b", "c"]);
|
|
expect(moveItem(["a", "b"], 1, 1)).toEqual(["a", "b"]);
|
|
});
|
|
});
|
|
|
|
describe("usePageDraft for the on-page editor", () => {
|
|
it("keeps the company's branding from the loaded page", async () => {
|
|
const { result } = await loaded();
|
|
expect(result.current.branding).toEqual(stored.company);
|
|
});
|
|
|
|
it("duplicates a section below itself and selects the copy", async () => {
|
|
const { result } = await loaded();
|
|
act(() => result.current.duplicateBlock(1));
|
|
expect(types(result)).toEqual(["HERO", "FAQ", "FAQ", "HTML"]);
|
|
expect(result.current.rows[2].block).toEqual(result.current.rows[1].block);
|
|
expect(result.current.rows[2].key).not.toBe(result.current.rows[1].key);
|
|
expect(result.current.selected).toBe(2);
|
|
});
|
|
|
|
it("adds a section between two and selects it", async () => {
|
|
const { result } = await loaded();
|
|
act(() => result.current.insertBlock(1, "STEPS"));
|
|
expect(types(result)).toEqual(["HERO", "STEPS", "FAQ", "HTML"]);
|
|
expect(result.current.selected).toBe(1);
|
|
});
|
|
|
|
it("brings an added, duplicated or moved section into view", async () => {
|
|
const { result } = await loaded();
|
|
act(() => result.current.addBlock("CTA"));
|
|
expect(result.current.revealed).toMatchObject({ index: 3 });
|
|
act(() => result.current.duplicateBlock(0));
|
|
expect(result.current.revealed).toMatchObject({ index: 1 });
|
|
act(() => result.current.moveBlock(0, 1));
|
|
expect(result.current.revealed).toMatchObject({ index: 1 });
|
|
act(() => result.current.moveBlockTo(4, 0));
|
|
expect(result.current.revealed).toMatchObject({ index: 0 });
|
|
});
|
|
|
|
it("drags a section to a new place, and the selection follows it", async () => {
|
|
const { result } = await loaded();
|
|
act(() => result.current.select(0));
|
|
act(() => result.current.moveBlockTo(0, 2));
|
|
expect(types(result)).toEqual(["FAQ", "HTML", "HERO"]);
|
|
expect(result.current.selected).toBe(2);
|
|
});
|
|
|
|
it("undoes typing in one field as one step, redoes it, and keeps history across a save", async () => {
|
|
const { result } = await loaded();
|
|
const key = result.current.rows[0].key;
|
|
act(() => result.current.text(key, "heading", "Ho"));
|
|
act(() => result.current.text(key, "heading", "Hot"));
|
|
act(() => result.current.text(key, "heading", "Hotel week"));
|
|
await act(async () => { await result.current.save(); });
|
|
expect(result.current.canUndo).toBe(true);
|
|
act(() => result.current.undo());
|
|
expect(result.current.rows[0].block.heading).toBe("Hi");
|
|
expect(result.current.canUndo).toBe(false);
|
|
act(() => result.current.redo());
|
|
expect(result.current.rows[0].block.heading).toBe("Hotel week");
|
|
});
|
|
|
|
it("brings a deleted section back in its place with its content", async () => {
|
|
const { result } = await loaded();
|
|
act(() => result.current.removeBlock(result.current.rows[1].key));
|
|
expect(types(result)).toEqual(["HERO", "HTML"]);
|
|
act(() => result.current.undo());
|
|
expect(types(result)).toEqual(["HERO", "FAQ", "HTML"]);
|
|
expect(result.current.rows[1].block.items).toEqual([{ question: "Q", answer: "A" }]);
|
|
});
|
|
|
|
it("keeps at most 50 steps", async () => {
|
|
const { result } = await loaded();
|
|
for (let i = 0; i < 55; i++) act(() => result.current.insertBlock(0, "CTA"));
|
|
let undone = 0;
|
|
while (result.current.canUndo) { act(() => result.current.undo()); undone++; }
|
|
expect(undone).toBe(50);
|
|
});
|
|
|
|
it("marks a Custom HTML block stale once its HTML or place differs from the saved page, until the next save", async () => {
|
|
const { result } = await loaded();
|
|
expect(result.current.stale(2)).toBe(false);
|
|
act(() => result.current.updateBlock(result.current.rows[2].key, (b) => ({ ...b, html: "<p>y</p>" })));
|
|
expect(result.current.stale(2)).toBe(true);
|
|
const version = result.current.frameVersion;
|
|
await act(async () => { await result.current.save(); });
|
|
expect(result.current.stale(2)).toBe(false);
|
|
expect(result.current.frameVersion).toBe(version + 1);
|
|
act(() => result.current.moveBlockTo(2, 0));
|
|
expect(result.current.stale(0)).toBe(true);
|
|
});
|
|
|
|
it("selects the block a refusal names, and clears the selection for a page field", async () => {
|
|
const { result } = await loaded();
|
|
mocks.savePage.mockRejectedValueOnce(new ApiError("x", 422, { code: "PagesEventRequired", details: { field: "blocks[1].eventUuid" } }));
|
|
await act(async () => { await result.current.save(); });
|
|
expect(result.current.selected).toBe(1);
|
|
expect(result.current.revealed).toEqual({ index: 1, n: 1 });
|
|
mocks.savePage.mockRejectedValueOnce(new ApiError("x", 422, { code: "PagesSlugTaken", details: { field: "slug" } }));
|
|
await act(async () => { await result.current.save(); });
|
|
expect(result.current.selected).toBeNull();
|
|
});
|
|
});
|