Skip to content

Commit 5f2935c

Browse files
authored
test(form-core): add onChange validation test (#414)
1 parent c392c61 commit 5f2935c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,30 @@ describe('field api', () => {
140140

141141
expect(subfield.getValue()).toBe('one')
142142
})
143+
144+
it('should run validation onChange', async () => {
145+
const form = new FormApi({
146+
defaultValues: {
147+
name: 'test',
148+
},
149+
})
150+
151+
const field = new FieldApi({
152+
form,
153+
name: 'name',
154+
onChange: (value) => {
155+
if (value === 'other') {
156+
return 'Please enter a different value'
157+
}
158+
159+
return
160+
},
161+
})
162+
163+
field.mount()
164+
165+
expect(field.getMeta().error).toBeUndefined()
166+
field.setValue('other', { touch: true })
167+
expect(field.getMeta().error).toBe('Please enter a different value')
168+
})
143169
})

0 commit comments

Comments
 (0)