Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/api/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,30 @@ describe("errorMessage", () => {
expect(errorMessage(error)).toBe("Element e1 not found");
});

it("ignores a non-string message from the backend and falls back", () => {
const error = Object.assign(new Error("Request failed"), {
response: { data: { error: { message: 404 } } },
});

expect(errorMessage(error)).toBe("Request failed");
});

it("falls back when the response has no error.message", () => {
const error = Object.assign(new Error("Network Error"), {
response: { data: { error: { code: "UNKNOWN" } } },
});

expect(errorMessage(error)).toBe("Network Error");
});

it("falls back to the error's own message", () => {
expect(errorMessage(new Error("Network Error"))).toBe("Network Error");
});

it("stringifies anything that is not an Error", () => {
expect(errorMessage("boom")).toBe("boom");
expect(errorMessage(null)).toBe("null");
expect(errorMessage(undefined)).toBe("undefined");
});
});

Expand Down
54 changes: 54 additions & 0 deletions src/editor/Main/Elements/AddElementForm/AddElementForm.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, it, vi } from "vitest";
import AddElementForm from "./AddElementForm";

describe("Elements AddElementForm", () => {
it("submits title and value", async () => {
const user = userEvent.setup();
const onSubmit = vi.fn();
render(<AddElementForm onSubmit={onSubmit} onCancel={vi.fn()} />);

await user.type(screen.getByLabelText("Title"), "Footer");
await user.type(screen.getByLabelText("Value"), "<p>bye</p>");
await user.click(screen.getByRole("button", { name: "Submit" }));

await waitFor(() =>
expect(onSubmit).toHaveBeenCalledWith({ title: "Footer", value: "<p>bye</p>" }),
);
});

it("does not submit without required fields", async () => {
const user = userEvent.setup();
const onSubmit = vi.fn();
render(<AddElementForm onSubmit={onSubmit} onCancel={vi.fn()} />);

await user.click(screen.getByRole("button", { name: "Submit" }));

expect(await screen.findAllByText(/required/i)).toHaveLength(2);
expect(onSubmit).not.toHaveBeenCalled();
});

it("calls onCancel", async () => {
const user = userEvent.setup();
const onCancel = vi.fn();
render(<AddElementForm onSubmit={vi.fn()} onCancel={onCancel} />);

await user.click(screen.getByRole("button", { name: "Cancel" }));

expect(onCancel).toHaveBeenCalled();
});

it("resets the fields", async () => {
const user = userEvent.setup();
render(<AddElementForm onSubmit={vi.fn()} onCancel={vi.fn()} />);

const title = screen.getByLabelText("Title");
await user.type(title, "Temp");
expect(title).toHaveValue("Temp");

await user.click(screen.getByRole("button", { name: "Reset" }));

await waitFor(() => expect(screen.getByLabelText("Title")).toHaveValue(""));
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, it, vi } from "vitest";
import EditElementForm from "./EditElementForm";

const data = { key: "e1", title: "Header", value: "<h1>{{title}}</h1>" };

describe("Elements EditElementForm", () => {
it("fills the form from data and submits the original key", async () => {
const user = userEvent.setup();
const onSubmit = vi.fn();
render(<EditElementForm data={data} onSubmit={onSubmit} onCancel={vi.fn()} />);

const title = screen.getByLabelText("Title");
const value = screen.getByLabelText("Value");
expect(title).toHaveValue("Header");
expect(value).toHaveValue("<h1>{{title}}</h1>");

await user.clear(title);
await user.type(title, "Big header");
await user.click(screen.getByRole("button", { name: "Submit" }));

await waitFor(() =>
expect(onSubmit).toHaveBeenCalledWith({
key: "e1",
title: "Big header",
value: "<h1>{{title}}</h1>",
}),
);
});

it("does not submit an empty title", async () => {
const user = userEvent.setup();
const onSubmit = vi.fn();
render(<EditElementForm data={data} onSubmit={onSubmit} onCancel={vi.fn()} />);

await user.clear(screen.getByLabelText("Title"));
await user.click(screen.getByRole("button", { name: "Submit" }));

expect(await screen.findByText(/required/i)).toBeInTheDocument();
expect(onSubmit).not.toHaveBeenCalled();
});

it("resets back to the original values", async () => {
const user = userEvent.setup();
render(<EditElementForm data={data} onSubmit={vi.fn()} onCancel={vi.fn()} />);

const title = screen.getByLabelText("Title");
await user.clear(title);
await user.type(title, "Changed");
await user.click(screen.getByRole("button", { name: "Reset" }));

await waitFor(() => expect(screen.getByLabelText("Title")).toHaveValue("Header"));
});

it("calls onCancel", async () => {
const user = userEvent.setup();
const onCancel = vi.fn();
render(<EditElementForm data={data} onSubmit={vi.fn()} onCancel={onCancel} />);

await user.click(screen.getByRole("button", { name: "Cancel" }));

expect(onCancel).toHaveBeenCalled();
});
});
111 changes: 111 additions & 0 deletions src/editor/Main/TemplateTree/AddElementForm/AddElementForm.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { getAllElements } from "@/api/main";
import AddElementForm from "./AddElementForm";

vi.mock("@/api/main", () => ({
getAllElements: vi.fn(),
}));

const parent = { key: "1", title: "root", value: "<table>{{content}}</table>", children: [] };

const header = { key: "e1", title: "Header", value: "<h1>{{title}}</h1><p>{{body}}</p>" };

beforeEach(() => {
vi.clearAllMocks();
vi.mocked(getAllElements).mockResolvedValue([header]);
});

describe("TemplateTree AddElementForm", () => {
it("loads library elements and submits an element node with placeholder fills", async () => {
const user = userEvent.setup();
const onSubmit = vi.fn();
render(<AddElementForm templateId="t1" data={parent} onSubmit={onSubmit} onCancel={vi.fn()} />);

await waitFor(() => expect(getAllElements).toHaveBeenCalledWith("t1"));

await user.type(screen.getByLabelText("Title"), "Intro");
await user.click(screen.getAllByRole("combobox")[0]);
// The Element field label also has title="Element", so target the open Type option.
const typeOption = (await screen.findAllByTitle("Element")).find((el) =>
el.classList.contains("ant-select-item-option"),
);
expect(typeOption).toBeDefined();
await user.click(typeOption!);

await user.click(screen.getAllByRole("combobox")[1]);
await user.click(await screen.findByTitle("Header"));

// Title is pre-filled from the selected element
await waitFor(() => expect(screen.getByLabelText("Title")).toHaveValue("Header"));

const titleFill = await screen.findByLabelText("{{title}}");
const bodyFill = screen.getByLabelText("{{body}}");
await user.type(titleFill, "Welcome");
await user.type(bodyFill, "Hello there");

await user.click(screen.getByRole("button", { name: "Submit" }));

await waitFor(() =>
expect(onSubmit).toHaveBeenCalledWith({
currentItemKey: "1",
element: header,
values: expect.objectContaining({
type: "element",
title: "Header",
elementKey: "e1",
children: { "{{title}}": "Welcome", "{{body}}": "Hello there" },
}),
}),
);
});

it("submits raw HTML when type is HTML", async () => {
const user = userEvent.setup();
const onSubmit = vi.fn();
render(<AddElementForm templateId="t1" data={parent} onSubmit={onSubmit} onCancel={vi.fn()} />);

await user.type(screen.getByLabelText("Title"), "Footer");
await user.click(screen.getAllByRole("combobox")[0]);
await user.click(await screen.findByTitle("HTML"));

await user.type(await screen.findByLabelText("Value"), "<p>bye</p>");
await user.click(screen.getByRole("button", { name: "Submit" }));

await waitFor(() =>
expect(onSubmit).toHaveBeenCalledWith({
currentItemKey: "1",
element: null,
values: expect.objectContaining({
type: "html",
title: "Footer",
value: "<p>bye</p>",
}),
}),
);
});

it("does not submit without a type", async () => {
const user = userEvent.setup();
const onSubmit = vi.fn();
render(<AddElementForm templateId="t1" data={parent} onSubmit={onSubmit} onCancel={vi.fn()} />);

await user.type(screen.getByLabelText("Title"), "Something");
await user.click(screen.getByRole("button", { name: "Submit" }));

// Type and Element are both required while type is unset, so two messages appear.
expect(await screen.findAllByText(/required/i)).toHaveLength(2);
expect(onSubmit).not.toHaveBeenCalled();
});

it("calls onCancel", async () => {
const user = userEvent.setup();
const onCancel = vi.fn();
render(<AddElementForm templateId="t1" data={parent} onSubmit={vi.fn()} onCancel={onCancel} />);

await user.click(screen.getByRole("button", { name: "Cancel" }));

expect(onCancel).toHaveBeenCalled();
});
});
32 changes: 32 additions & 0 deletions src/editor/Main/notifyError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { notification } from "antd";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { notifyError } from "./notifyError";

describe("notifyError", () => {
beforeEach(() => {
vi.restoreAllMocks();
vi.spyOn(notification, "error").mockImplementation(() => {});
});
Comment thread
vadim9999 marked this conversation as resolved.
Resolved

it("shows the title and the backend message", () => {
const error = Object.assign(new Error("Request failed"), {
response: { data: { error: { message: "title is required" } } },
});

notifyError("Could not add the element")(error);

expect(notification.error).toHaveBeenCalledWith({
title: "Could not add the element",
description: "title is required",
});
});

it("falls back to the plain error message", () => {
notifyError("Could not load the elements")(new Error("Network Error"));

expect(notification.error).toHaveBeenCalledWith({
title: "Could not load the elements",
description: "Network Error",
});
});
});