Skip to content

Commit 4baddd5

Browse files
committed
fix(FieldApi): onChangeListenTo should not block onChangeAsync validator on field it is listening to
Signed-off-by: Pascal Küsgen <[email protected]>
1 parent c6512bd commit 4baddd5

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

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

+49
Original file line numberDiff line numberDiff line change
@@ -1891,6 +1891,55 @@ describe('field api', () => {
18911891
expect(form.state.canSubmit).toBe(true)
18921892
})
18931893

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'], // FIXME - commenting out this line will make the test pass
1930+
},
1931+
})
1932+
1933+
firstNameField.mount()
1934+
lastNameField.mount()
1935+
1936+
firstNameField.setValue('error')
1937+
1938+
await vi.runAllTimersAsync()
1939+
1940+
expect(firstNameField.getMeta().errors).toContain(asyncErrorMessage)
1941+
})
1942+
18941943
it('should debounce onChange listener', async () => {
18951944
vi.useFakeTimers()
18961945
const form = new FormApi({

0 commit comments

Comments
 (0)