Skip to content

Commit 1dd099a

Browse files
committed
fix(FieldApi): onChangeListenTo should not block onChangeAsync validator on field it is listening to
1 parent 45c697d commit 1dd099a

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

packages/form-core/tests/FieldApi.spec.ts

+52
Original file line numberDiff line numberDiff line change
@@ -1890,4 +1890,56 @@ describe('field api', () => {
18901890
expect(field.getMeta().errors).toStrictEqual([])
18911891
expect(form.state.canSubmit).toBe(true)
18921892
})
1893+
1894+
it('should not block onChangeAsync of a field when onChangeListenTo is used in another field', async () => {
1895+
const syncErrorMessage = 'Sync error triggered'
1896+
const asyncErrorMessage = 'An Async Error was triggered'
1897+
1898+
const nameValidators = {
1899+
onChange: ({ value }: { value: string }) => !value && syncErrorMessage,
1900+
onChangeAsyncDebounceMs: 500,
1901+
onChangeAsync: async ({ value }: { value: string }) =>
1902+
value.includes('error') && asyncErrorMessage,
1903+
} as const
1904+
1905+
vi.useFakeTimers()
1906+
1907+
const form = new FormApi({
1908+
defaultValues: {
1909+
firstName: '',
1910+
lastName: '',
1911+
},
1912+
})
1913+
1914+
form.mount()
1915+
1916+
const firstNameField = new FieldApi({
1917+
form,
1918+
name: 'firstName',
1919+
validators: {
1920+
...nameValidators,
1921+
},
1922+
})
1923+
1924+
const lastNameField = new FieldApi({
1925+
form,
1926+
name: 'lastName',
1927+
validators: {
1928+
...nameValidators,
1929+
onChangeListenTo: ['firstName'],
1930+
},
1931+
})
1932+
1933+
firstNameField.mount()
1934+
lastNameField.mount()
1935+
1936+
// Set a value in firstName that triggers async validation
1937+
firstNameField.setValue('error')
1938+
1939+
// Run all timers to resolve async validation
1940+
await vi.runAllTimersAsync()
1941+
1942+
// Verify that the async validation error is triggered
1943+
expect(firstNameField.getMeta().errors).toContain(asyncErrorMessage)
1944+
})
18931945
})

0 commit comments

Comments
 (0)