Files
co-lite/solvway-admin-portal/app/components/pages/PageEditor.html.test.tsx
T
ngqileandClaude Opus 5.5 cfc817082d feat(pages): Custom HTML block and hide-header switch in the page editor
The iframe guard allow-lists HtmlFrame.tsx for the named FRAME_SANDBOX
only, and asserts it never grants same-origin, forms, top navigation
or modals.

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

68 lines
3.6 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";
// add-custom-html-block: the editor's Custom HTML block and "Hide page header" switch.
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, hideHeader: true,
blocks: [{ type: "HTML" as const, html: "<h1>Designed</h1>" }],
};
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 }));
});
describe("PageEditor: Custom HTML", () => {
it("loads the HTML and the hide-header option, and saves both", async () => {
const user = userEvent.setup();
render(<PageEditor pageId={5} />);
const box = await screen.findByLabelText("HTML");
expect(box).toHaveValue("<h1>Designed</h1>");
expect(box).toHaveClass("font-mono");
expect(screen.getByText("17/200000")).toBeInTheDocument();
expect(screen.getByText(/Forms inside the pasted HTML don't send/)).toBeInTheDocument();
const toggle = screen.getByRole("switch", { name: "Hide page header" });
expect(toggle).toBeChecked();
await user.click(toggle);
await user.click(screen.getByRole("button", { name: "Add Custom HTML" }));
await user.type(screen.getAllByLabelText("HTML")[1], "<p>x</p>");
await user.click(screen.getByRole("button", { name: "Save" }));
await waitFor(() => expect(mocks.savePage).toHaveBeenCalled());
const body = mocks.savePage.mock.calls[0][1];
expect(body.hideHeader).toBe(false);
expect(body.blocks).toEqual([{ type: "HTML", html: "<h1>Designed</h1>" }, { type: "HTML", html: "<p>x</p>" }]);
});
it("shows a refusal of the HTML at its field", async () => {
mocks.savePage.mockRejectedValue(new ApiError("x", 422, { code: "PagesTooLong", details: { field: "blocks[0].html" } }));
const user = userEvent.setup();
render(<PageEditor pageId={5} />);
const box = await screen.findByLabelText("HTML");
await user.click(screen.getByRole("button", { name: "Save" }));
expect(await screen.findByText("Too long.")).toBeInTheDocument();
expect(box.closest("div")).toHaveTextContent("Too long.");
expect(box).toHaveAttribute("aria-invalid", "true");
});
});