Skip to content

fix(FieldApi): onChangeListenTo should not block onChangeAsync validator on field it is listening to #1420

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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions packages/form-core/tests/FieldApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,105 @@ describe('field api', () => {
expect(form.state.canSubmit).toBe(true)
})

it('should not block onChangeAsync of a field when onChangeListenTo is used in another field', async () => {
const syncErrorMessage = 'Sync error triggered'
const asyncErrorMessage = 'An Async Error was triggered'

const nameValidators = {
onChange: ({ value }: { value: string }) => !value && syncErrorMessage,
onChangeAsyncDebounceMs: 500,
onChangeAsync: async ({ value }: { value: string }) =>
value.includes('error') && asyncErrorMessage,
} as const

vi.useFakeTimers()

const form = new FormApi({
defaultValues: {
firstName: '',
lastName: '',
},
})

form.mount()

const firstNameField = new FieldApi({
form,
name: 'firstName',
validators: {
...nameValidators,
},
})

const lastNameField = new FieldApi({
form,
name: 'lastName',
validators: {
...nameValidators,
onChangeListenTo: ['firstName'], // FIXME - commenting out this line will make the test pass
},
})

firstNameField.mount()
lastNameField.mount()

firstNameField.setValue('error')

await vi.runAllTimersAsync()

expect(firstNameField.getMeta().errors).toContain(asyncErrorMessage)
})

it('should not block onBlurAsync of a field when onBlurListenTo is used in another field', async () => {
const syncErrorMessage = 'Sync error triggered'
const asyncErrorMessage = 'An Async Error was triggered'

const nameValidators = {
onBlur: ({ value }: { value: string }) => !value && syncErrorMessage,
onBlurAsyncDebounceMs: 500,
onBlurAsync: async ({ value }: { value: string }) =>
value.includes('error') && asyncErrorMessage,
} as const

vi.useFakeTimers()

const form = new FormApi({
defaultValues: {
firstName: '',
lastName: '',
},
})

form.mount()

const firstNameField = new FieldApi({
form,
name: 'firstName',
validators: {
...nameValidators,
},
})

const lastNameField = new FieldApi({
form,
name: 'lastName',
validators: {
...nameValidators,
onBlurListenTo: ['firstName'], // FIXME - commenting out this line will make the test pass
},
})

firstNameField.mount()
lastNameField.mount()

firstNameField.setValue('error')
firstNameField.handleBlur()

await vi.runAllTimersAsync()

expect(firstNameField.getMeta().errors).toContain(asyncErrorMessage)
})

it('should debounce onChange listener', async () => {
vi.useFakeTimers()
const form = new FormApi({
Expand Down
Loading