Skip to content

Commit 5ba4ca9

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

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

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

Lines changed: 28 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,33 @@ 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: 'hi' }],
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')
3686+
3687+
expect(form.state.values.shallow).toEqual('Shallow')
3688+
expect(form.state.values.nested.arr[0]?.name).toEqual('Nested')
3689+
3690+
form.resetField('shallow')
3691+
expect(form.state.values.shallow).toEqual('')
3692+
3693+
form.resetField('nested.arr[0].name')
3694+
expect(form.state.values.nested.arr[0]?.name).toEqual('')
3695+
expect(form.state.values.nested.arr[1]?.test).toEqual('hi')
3696+
})
3697+
36713698
it('should preserve nested fields on resetField if defaultValues is not provided', () => {
36723699
const state = {
36733700
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)