Skip to content

Commit d1d007a

Browse files
committed
chore: test-d and array test
1 parent 8363470 commit d1d007a

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3640,7 +3640,7 @@ it('should mark sourceMap as undefined when async field error is resolved', asyn
36403640
expect(field.getMeta().errorSourceMap.onChange).toBeUndefined()
36413641
})
36423642

3643-
it('should reset nested fields', () => {
3643+
it('should reset nested object fields', () => {
36443644
const defaultValues = {
36453645
shallow: '',
36463646
nested: {
@@ -3668,6 +3668,35 @@ it('should reset nested fields', () => {
36683668
expect(form.state.values.nested.field.name).toEqual('')
36693669
})
36703670

3671+
it('should reset nested array fields', () => {
3672+
const defaultValues = {
3673+
shallow: '',
3674+
nested: {
3675+
arr: [{ name: '' }, { test: 'array-test' }],
3676+
},
3677+
}
3678+
3679+
const form = new FormApi({
3680+
defaultValues,
3681+
})
3682+
form.mount()
3683+
3684+
form.setFieldValue('shallow', 'Shallow')
3685+
form.setFieldValue('nested.arr[0].name', 'nested-arr')
3686+
form.setFieldValue('nested.arr[1].test', 'array-test-changed')
3687+
3688+
expect(form.state.values.shallow).toEqual('Shallow')
3689+
expect(form.state.values.nested.arr[0]?.name).toEqual('nested-arr')
3690+
expect(form.state.values.nested.arr[1]?.test).toEqual('array-test-changed')
3691+
3692+
form.resetField('shallow')
3693+
expect(form.state.values.shallow).toEqual('')
3694+
3695+
form.resetField('nested.arr[0].name')
3696+
expect(form.state.values.nested.arr[0]?.name).toEqual('')
3697+
expect(form.state.values.nested.arr[1]?.test).toEqual('array-test-changed')
3698+
})
3699+
36713700
it('should preserve nested fields on resetField if defaultValues is not provided', () => {
36723701
const state = {
36733702
shallow: '',

packages/form-core/tests/FormApi.test-d.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,33 @@ it('should only allow array fields for array-specific methods', () => {
273273
// @ts-expect-error too wide!
274274
const validate3 = form.validateArrayFieldsStartingFrom<RandomKeys>
275275
})
276+
277+
it('should infer full field name union for form.resetField parameters', () => {
278+
type FormData = {
279+
shallow: string
280+
nested: {
281+
field: {
282+
name: string
283+
}
284+
}
285+
}
286+
287+
const defaultValue = {
288+
shallow: '',
289+
nested: {
290+
field: {
291+
name: '',
292+
},
293+
},
294+
}
295+
296+
const form = new FormApi({
297+
defaultValues: defaultValue as FormData,
298+
})
299+
300+
expectTypeOf(form.resetField)
301+
.parameter(0)
302+
.toEqualTypeOf<
303+
'shallow' | 'nested' | 'nested.field' | 'nested.field.name'
304+
>()
305+
})

0 commit comments

Comments
 (0)