Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
106 lines
5.9 KiB
TypeScript
106 lines
5.9 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");
|
|
});
|
|
|
|
// add-html-image-upload: upload an image from the block's panel and copy its link; the HTML stays as it was.
|
|
it("uploads an image at once for the page's company and shows its link to copy", async () => {
|
|
mocks.uploadPageImage.mockResolvedValueOnce({ id: 42 });
|
|
const user = userEvent.setup();
|
|
render(<PageEditor pageId={5} />);
|
|
await screen.findByLabelText("HTML");
|
|
await user.upload(screen.getByLabelText("Upload image"), new File(["png"], "lobby.png", { type: "image/png" }));
|
|
expect(await screen.findByText("/api/public/page-images/42")).toBeInTheDocument();
|
|
expect(mocks.uploadPageImage).toHaveBeenCalledWith(2, btoa("png"));
|
|
expect(screen.getByRole("img", { name: "Image preview" })).toHaveAttribute("src", "/api/public/page-images/42");
|
|
const write = vi.spyOn(navigator.clipboard, "writeText");
|
|
await user.click(screen.getByRole("button", { name: "Copy link /api/public/page-images/42" }));
|
|
expect(write).toHaveBeenCalledWith("/api/public/page-images/42");
|
|
expect(screen.getByLabelText("HTML")).toHaveValue("<h1>Designed</h1>");
|
|
expect(mocks.savePage).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("shows a refused upload's reason and no link", async () => {
|
|
mocks.uploadPageImage.mockRejectedValueOnce(new ApiError("x", 422, { code: "PagesImageTooLarge" }));
|
|
const user = userEvent.setup();
|
|
render(<PageEditor pageId={5} />);
|
|
await screen.findByLabelText("HTML");
|
|
await user.upload(screen.getByLabelText("Upload image"), new File(["big"], "big.jpg", { type: "image/jpeg" }));
|
|
expect(await screen.findByText("The image is larger than 2 MB.")).toBeInTheDocument();
|
|
expect(screen.queryByText(/\/api\/public\/page-images\//)).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("asks a super admin's new page for a company before uploading", async () => {
|
|
mocks.useAdminAuth.mockReturnValue({ isSuper: true, companyCode: null });
|
|
memoryRouter.reset("/pages/new");
|
|
const user = userEvent.setup();
|
|
render(<PageEditor />);
|
|
await user.click(screen.getByRole("button", { name: "Add Custom HTML" }));
|
|
await user.upload(await screen.findByLabelText("Upload image"), new File(["png"], "lobby.png", { type: "image/png" }));
|
|
expect(await screen.findByText("Choose a company.")).toBeInTheDocument();
|
|
expect(mocks.uploadPageImage).not.toHaveBeenCalled();
|
|
});
|
|
});
|