Skip to content

Commit

Permalink
fix(Forms): only run onBlurValidator when no other errors are present (
Browse files Browse the repository at this point in the history
…#4172)

Fixes #4074

With an exception for when `onChange` or `validator` is async. Then we
still want to show the `onBlurValidator`.

**An example**

We have a slow `onChange` event. 

- The user types.
- `onChange` fires and is ongoing while the user types more.
- Before `onChange` has finished, the user blurs the field.
- The `onBlurValidator` fails.
- We show the error coming from the `onBlurValidator`.
  • Loading branch information
tujoworker authored Oct 25, 2024
1 parent 2ae86f2 commit d2797f1
Show file tree
Hide file tree
Showing 3 changed files with 356 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1192,14 +1192,17 @@ describe('DataContext.Provider', () => {

it('should evaluate sync validation, such as required, before continue with async validation', async () => {
const onSubmit: OnSubmit = jest.fn(async () => {
await wait(10)
return { info: 'Info message' } as const
})
const validator = jest.fn(async (value) => {
await wait(10)
if (value === 'validator-error') {
return new Error('validator-error')
}
})
const onBlurValidator = jest.fn(async (value) => {
await wait(10)
if (value === 'onBlurValidator-error') {
return new Error('onBlurValidator-error')
}
Expand Down Expand Up @@ -1331,9 +1334,11 @@ describe('DataContext.Provider', () => {

await userEvent.click(submitButton)

expect(onSubmit).toHaveBeenCalledTimes(1)
expect(onBlurValidator).toHaveBeenCalledTimes(3)
expect(validator).toHaveBeenCalledTimes(12)
await waitFor(() => {
expect(onSubmit).toHaveBeenCalledTimes(1)
expect(onBlurValidator).toHaveBeenCalledTimes(3)
expect(validator).toHaveBeenCalledTimes(12)
})
})

it('should set "formState" to "pending" when "validator" is async', async () => {
Expand Down
Loading

0 comments on commit d2797f1

Please sign in to comment.