Skip to content

Commit 3dbed4a

Browse files
committed
test(form-core): add tests for formApi.reset() with defaultState and no defaults
1 parent 4cc6985 commit 3dbed4a

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

packages/form-core/tests/reset-during-submit.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it, vi } from 'vitest'
22
import { FormApi } from '../src/FormApi'
3+
import type { AnyFormApi } from '../src/FormApi'
34

45
describe('Form reset during submit', () => {
56
it('should correctly reset to new default values when called during onSubmit', async () => {
@@ -86,4 +87,53 @@ describe('Form reset during submit', () => {
8687
expect(form.state.values.value).toBe(3)
8788
expect(form.state.isDirty).toBe(false)
8889
})
90+
91+
it('should handle reset with defaultState values fallback', async () => {
92+
const form = new FormApi({
93+
defaultState: {
94+
values: {
95+
name: 'from-default-state',
96+
},
97+
},
98+
onSubmit: async ({ formApi }) => {
99+
// Reset without providing values - should fall back to defaultState.values
100+
formApi.reset()
101+
},
102+
})
103+
104+
form.mount()
105+
106+
// Change the field value
107+
form.setFieldValue('name', 'changed')
108+
expect(form.state.isDirty).toBe(true)
109+
110+
// Submit the form
111+
await form.handleSubmit()
112+
113+
// After reset without values, should fall back to defaultState.values
114+
expect(form.state.values.name).toBe('from-default-state')
115+
expect(form.state.isDirty).toBe(false)
116+
})
117+
118+
it('should handle reset with no default values or defaultState', async () => {
119+
const form = new FormApi({
120+
onSubmit: async ({ formApi }) => {
121+
// Reset without providing values and no defaults - should reset to empty
122+
formApi.reset()
123+
},
124+
}) as AnyFormApi
125+
126+
form.mount()
127+
128+
// Change the field value
129+
form.setFieldValue('name', 'some-value')
130+
expect(form.state.isDirty).toBe(true)
131+
132+
// Submit the form
133+
await form.handleSubmit()
134+
135+
// After reset with no defaults, should be empty/undefined
136+
expect(form.state.values.name).toBeUndefined()
137+
expect(form.state.isDirty).toBe(false)
138+
})
89139
})

0 commit comments

Comments
 (0)