Skip to content
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