Repository navigation
Add unit tests for API error edges and Antd form components #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
atherdon
wants to merge
6
commits into
master
Choose a base branch
from
test/more-api-and-form-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0d9819e
Add unit tests for API error edges and Antd form components
atherdon b80e912
Fix failing unit tests for the Antd form components
atherdon ec9ed54
Merge branch 'master' into test/more-api-and-form-tests
atherdon 0f0a712
Raise coverage thresholds and reset mocks in notifyError test
vadim9999 4d6f06c
Merge branch 'master' into test/more-api-and-form-tests
vadim9999 6244c8a
Pass templateId to AddElementForm in tests
vadim9999 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/editor/Main/Elements/AddElementForm/AddElementForm.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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("")); | ||
| }); | ||
| }); |
65 changes: 65 additions & 0 deletions
65
src/editor/Main/Elements/EditElementForm/EditElementForm.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
111
src/editor/Main/TemplateTree/AddElementForm/AddElementForm.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(() => {}); | ||
| }); | ||
|
|
||
| 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", | ||
| }); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.