-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose validate, validateAttribute and setErrors to the context
This is to make the context and state management more flexible. This comes in handy when you have larger forms when nested context. You can now validate sections of the form independently.
- Loading branch information
1 parent
06ff464
commit 18f0610
Showing
4 changed files
with
46 additions
and
7 deletions.
There are no files selected for viewing
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
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,28 @@ | ||
import React from "react"; | ||
import { renderHook, act } from "@testing-library/react"; | ||
|
||
import { Form, useFormContext, createValidator } from "../src"; | ||
|
||
it("will allow you to validate the form from the context", async () => { | ||
const onSubmit = jest.fn(); | ||
|
||
const validator = createValidator({ test: [() => "I will always fail"] }); | ||
|
||
const { result } = renderHook(() => useFormContext(), { | ||
wrapper: ({ children }) => ( | ||
<Form onSubmit={onSubmit} validator={validator}> | ||
{children} | ||
</Form> | ||
), | ||
}); | ||
|
||
const errors = await result.current.validate(result.current.formState); | ||
expect(errors).toStrictEqual({ test: ["I will always fail"] }); | ||
|
||
const attributeErrors = await result.current.validateAttribute("test-1", result.current.formState); | ||
expect(attributeErrors).toHaveLength(0); | ||
|
||
expect(result.current.status).toBe("clean"); | ||
await act(async () => result.current.setErrors(errors)); | ||
expect(result.current.status).toBe("error"); | ||
}); |
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
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