diff --git a/src/api/errors.test.ts b/src/api/errors.test.ts index 06ed29f..6e92425 100644 --- a/src/api/errors.test.ts +++ b/src/api/errors.test.ts @@ -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"); }); }); diff --git a/src/editor/Main/Elements/AddElementForm/AddElementForm.test.tsx b/src/editor/Main/Elements/AddElementForm/AddElementForm.test.tsx new file mode 100644 index 0000000..b96b2da --- /dev/null +++ b/src/editor/Main/Elements/AddElementForm/AddElementForm.test.tsx @@ -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(); + + await user.type(screen.getByLabelText("Title"), "Footer"); + await user.type(screen.getByLabelText("Value"), "

bye

"); + await user.click(screen.getByRole("button", { name: "Submit" })); + + await waitFor(() => + expect(onSubmit).toHaveBeenCalledWith({ title: "Footer", value: "

bye

" }), + ); + }); + + it("does not submit without required fields", async () => { + const user = userEvent.setup(); + const onSubmit = vi.fn(); + render(); + + 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(); + + await user.click(screen.getByRole("button", { name: "Cancel" })); + + expect(onCancel).toHaveBeenCalled(); + }); + + it("resets the fields", async () => { + const user = userEvent.setup(); + render(); + + 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("")); + }); +}); diff --git a/src/editor/Main/Elements/EditElementForm/EditElementForm.test.tsx b/src/editor/Main/Elements/EditElementForm/EditElementForm.test.tsx new file mode 100644 index 0000000..1233394 --- /dev/null +++ b/src/editor/Main/Elements/EditElementForm/EditElementForm.test.tsx @@ -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: "

{{title}}

" }; + +describe("Elements EditElementForm", () => { + it("fills the form from data and submits the original key", async () => { + const user = userEvent.setup(); + const onSubmit = vi.fn(); + render(); + + const title = screen.getByLabelText("Title"); + const value = screen.getByLabelText("Value"); + expect(title).toHaveValue("Header"); + expect(value).toHaveValue("

{{title}}

"); + + 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: "

{{title}}

", + }), + ); + }); + + it("does not submit an empty title", async () => { + const user = userEvent.setup(); + const onSubmit = vi.fn(); + render(); + + 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(); + + 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(); + + await user.click(screen.getByRole("button", { name: "Cancel" })); + + expect(onCancel).toHaveBeenCalled(); + }); +}); diff --git a/src/editor/Main/TemplateTree/AddElementForm/AddElementForm.test.tsx b/src/editor/Main/TemplateTree/AddElementForm/AddElementForm.test.tsx new file mode 100644 index 0000000..6680c61 --- /dev/null +++ b/src/editor/Main/TemplateTree/AddElementForm/AddElementForm.test.tsx @@ -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: "{{content}}
", children: [] }; + +const header = { key: "e1", title: "Header", value: "

{{title}}

{{body}}

" }; + +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(); + + 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(); + + 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"), "

bye

"); + 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: "

bye

", + }), + }), + ); + }); + + it("does not submit without a type", async () => { + const user = userEvent.setup(); + const onSubmit = vi.fn(); + render(); + + 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(); + + await user.click(screen.getByRole("button", { name: "Cancel" })); + + expect(onCancel).toHaveBeenCalled(); + }); +}); diff --git a/src/editor/Main/notifyError.test.ts b/src/editor/Main/notifyError.test.ts new file mode 100644 index 0000000..6d0c718 --- /dev/null +++ b/src/editor/Main/notifyError.test.ts @@ -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(() => {}); + }); + + 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", + }); + }); +});