-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(jest): add unit test for FormRenderer
- Loading branch information
1 parent
b2ba9a6
commit 3ec848a
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/scenes/ApplicationFormStep/components/__tests__/FormRenderer.test.tsx
This file contains 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,49 @@ | ||
import { act, render, screen, waitFor } from "@testing-library/react"; | ||
import FormRenderer from "../FormRenderer"; | ||
import { Button } from "@/components/ui/button"; | ||
import userEvent from "@testing-library/user-event"; | ||
|
||
const onSubmitMock = jest.fn(); | ||
const actionButtons = <Button type="submit">Save</Button>; | ||
describe("FormRenderer", () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
it("should render simple form with a text field", async () => { | ||
render( | ||
<FormRenderer | ||
formFields={[ | ||
{ | ||
id: 1, | ||
position: 1, | ||
name: "name", | ||
label: "Name", | ||
type: "text", | ||
required: true, | ||
initialValue: "", | ||
optionList: [], | ||
}, | ||
]} | ||
onSubmit={onSubmitMock} | ||
actionButtons={actionButtons} | ||
/> | ||
); | ||
|
||
const input = screen.getByLabelText("Name"); | ||
const button = screen.getByRole("button", { name: "Save" }); | ||
expect(input).toBeVisible(); | ||
expect(button).toBeVisible(); | ||
|
||
act(() => userEvent.click(button)); | ||
expect(onSubmitMock).not.toHaveBeenCalled(); | ||
await waitFor(() => | ||
expect(screen.getByText("This field is required")).toBeVisible() | ||
); | ||
|
||
act(() => userEvent.type(input, "John")); | ||
expect(input).toHaveValue("John"); | ||
|
||
act(() => userEvent.click(button)); | ||
await waitFor(() => expect(onSubmitMock).toHaveBeenCalledTimes(1)); | ||
}); | ||
}); |