From 0d9819eeb23ca8b78de3cc769f3d7d0937a69838 Mon Sep 17 00:00:00 2001 From: Arthur Tkachenko Date: Mon, 23 Nov 2026 22:29:44 +0200 Subject: [PATCH 1/4] Add unit tests for API error edges and Antd form components - Expand errorMessage coverage (non-string API message, missing fields). - Elements AddElementForm / EditElementForm: required validation, submit, cancel, reset. - TemplateTree AddElementForm: loads library, type switch (element vs HTML), placeholder fields, submit payload. - notifyError shows title + errorMessage description. --- src/api/errors.test.ts | 18 +++ .../AddElementForm/AddElementForm.test.tsx | 54 +++++++++ .../EditElementForm/EditElementForm.test.tsx | 65 +++++++++++ .../AddElementForm/AddElementForm.test.tsx | 105 ++++++++++++++++++ src/editor/Main/notifyError.test.ts | 31 ++++++ 5 files changed, 273 insertions(+) create mode 100644 src/editor/Main/Elements/AddElementForm/AddElementForm.test.tsx create mode 100644 src/editor/Main/Elements/EditElementForm/EditElementForm.test.tsx create mode 100644 src/editor/Main/TemplateTree/AddElementForm/AddElementForm.test.tsx create mode 100644 src/editor/Main/notifyError.test.ts diff --git a/src/api/errors.test.ts b/src/api/errors.test.ts index 962d8cf..43ba0b6 100644 --- a/src/api/errors.test.ts +++ b/src/api/errors.test.ts @@ -10,11 +10,29 @@ 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..563caf5 --- /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" })); + + expect(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..1efbc6f --- /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" })); + + expect(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..977c2ec --- /dev/null +++ b/src/editor/Main/TemplateTree/AddElementForm/AddElementForm.test.tsx @@ -0,0 +1,105 @@ +import { render, screen, waitFor, within } 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).toHaveBeenCalled()); + + await user.type(screen.getByLabelText("Title"), "Intro"); + await user.click(screen.getAllByRole("combobox")[0]); + await user.click(await screen.findByTitle("Element")); + + 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" })); + + expect(await screen.findByText(/required/i)).toBeInTheDocument(); + 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..de08758 --- /dev/null +++ b/src/editor/Main/notifyError.test.ts @@ -0,0 +1,31 @@ +import { notification } from "antd"; +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { notifyError } from "./notifyError"; + +describe("notifyError", () => { + beforeEach(() => { + 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", + }); + }); +}); From b80e91270af4a1281cb9a23bef94ffc4c38ca9d9 Mon Sep 17 00:00:00 2001 From: petroved Date: Wed, 25 Nov 2026 18:06:29 +0000 Subject: [PATCH 2/4] Fix failing unit tests for the Antd form components The reset tests asserted against a stale input node after Ant Design remounted the field. Re-query the title inside waitFor. TemplateTree's Type option query also matched the Element field label, and submitting without a type shows two required messages (Type and Element). Co-authored-by: petroved --- .../Elements/AddElementForm/AddElementForm.test.tsx | 2 +- .../EditElementForm/EditElementForm.test.tsx | 2 +- .../AddElementForm/AddElementForm.test.tsx | 12 +++++++++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/editor/Main/Elements/AddElementForm/AddElementForm.test.tsx b/src/editor/Main/Elements/AddElementForm/AddElementForm.test.tsx index 563caf5..b96b2da 100644 --- a/src/editor/Main/Elements/AddElementForm/AddElementForm.test.tsx +++ b/src/editor/Main/Elements/AddElementForm/AddElementForm.test.tsx @@ -49,6 +49,6 @@ describe("Elements AddElementForm", () => { await user.click(screen.getByRole("button", { name: "Reset" })); - expect(title).toHaveValue(""); + 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 index 1efbc6f..1233394 100644 --- a/src/editor/Main/Elements/EditElementForm/EditElementForm.test.tsx +++ b/src/editor/Main/Elements/EditElementForm/EditElementForm.test.tsx @@ -50,7 +50,7 @@ describe("Elements EditElementForm", () => { await user.type(title, "Changed"); await user.click(screen.getByRole("button", { name: "Reset" })); - expect(title).toHaveValue("Header"); + await waitFor(() => expect(screen.getByLabelText("Title")).toHaveValue("Header")); }); it("calls onCancel", async () => { diff --git a/src/editor/Main/TemplateTree/AddElementForm/AddElementForm.test.tsx b/src/editor/Main/TemplateTree/AddElementForm/AddElementForm.test.tsx index 977c2ec..707cd26 100644 --- a/src/editor/Main/TemplateTree/AddElementForm/AddElementForm.test.tsx +++ b/src/editor/Main/TemplateTree/AddElementForm/AddElementForm.test.tsx @@ -1,4 +1,4 @@ -import { render, screen, waitFor, within } from "@testing-library/react"; +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"; @@ -27,7 +27,12 @@ describe("TemplateTree AddElementForm", () => { await user.type(screen.getByLabelText("Title"), "Intro"); await user.click(screen.getAllByRole("combobox")[0]); - await user.click(await screen.findByTitle("Element")); + // 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")); @@ -89,7 +94,8 @@ describe("TemplateTree AddElementForm", () => { await user.type(screen.getByLabelText("Title"), "Something"); await user.click(screen.getByRole("button", { name: "Submit" })); - expect(await screen.findByText(/required/i)).toBeInTheDocument(); + // 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(); }); From 0f0a7122fbd0c3e7bc98452ade4bf09d725d6569 Mon Sep 17 00:00:00 2001 From: Vadim Date: Tue, 1 Dec 2026 12:07:27 +0000 Subject: [PATCH 3/4] Raise coverage thresholds and reset mocks in notifyError test --- src/editor/Main/notifyError.test.ts | 1 + vitest.config.mts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/editor/Main/notifyError.test.ts b/src/editor/Main/notifyError.test.ts index de08758..6d0c718 100644 --- a/src/editor/Main/notifyError.test.ts +++ b/src/editor/Main/notifyError.test.ts @@ -4,6 +4,7 @@ import { notifyError } from "./notifyError"; describe("notifyError", () => { beforeEach(() => { + vi.restoreAllMocks(); vi.spyOn(notification, "error").mockImplementation(() => {}); }); diff --git a/vitest.config.mts b/vitest.config.mts index 52cd0c1..cd4b35b 100644 --- a/vitest.config.mts +++ b/vitest.config.mts @@ -17,7 +17,7 @@ export default defineConfig({ exclude: ["src/**/*.test.{ts,tsx}", "src/**/*.types.ts", "src/setupTests.ts"], reporter: ["text-summary", "html"], // A ratchet: set just below the current numbers. Raise them when coverage goes up; never lower them. - thresholds: { lines: 85, statements: 83, functions: 79, branches: 75 }, + thresholds: { lines: 86, statements: 85, functions: 82, branches: 77 }, }, }, resolve: { From 6244c8a442f11718918ee1a08cd66643bf1013ba Mon Sep 17 00:00:00 2001 From: Vadim Date: Tue, 1 Dec 2026 12:17:27 +0000 Subject: [PATCH 4/4] Pass templateId to AddElementForm in tests --- .../AddElementForm/AddElementForm.test.tsx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/editor/Main/TemplateTree/AddElementForm/AddElementForm.test.tsx b/src/editor/Main/TemplateTree/AddElementForm/AddElementForm.test.tsx index 707cd26..6680c61 100644 --- a/src/editor/Main/TemplateTree/AddElementForm/AddElementForm.test.tsx +++ b/src/editor/Main/TemplateTree/AddElementForm/AddElementForm.test.tsx @@ -21,9 +21,9 @@ 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(); + render(); - await waitFor(() => expect(getAllElements).toHaveBeenCalled()); + await waitFor(() => expect(getAllElements).toHaveBeenCalledWith("t1")); await user.type(screen.getByLabelText("Title"), "Intro"); await user.click(screen.getAllByRole("combobox")[0]); @@ -64,7 +64,7 @@ describe("TemplateTree AddElementForm", () => { it("submits raw HTML when type is HTML", async () => { const user = userEvent.setup(); const onSubmit = vi.fn(); - render(); + render(); await user.type(screen.getByLabelText("Title"), "Footer"); await user.click(screen.getAllByRole("combobox")[0]); @@ -89,7 +89,7 @@ describe("TemplateTree AddElementForm", () => { it("does not submit without a type", async () => { const user = userEvent.setup(); const onSubmit = vi.fn(); - render(); + render(); await user.type(screen.getByLabelText("Title"), "Something"); await user.click(screen.getByRole("button", { name: "Submit" })); @@ -102,7 +102,7 @@ describe("TemplateTree AddElementForm", () => { it("calls onCancel", async () => { const user = userEvent.setup(); const onCancel = vi.fn(); - render(); + render(); await user.click(screen.getByRole("button", { name: "Cancel" }));