|
1 | 1 | import { describe, expect, it, vi } from 'vitest' |
2 | 2 | import { FormApi } from '../src/FormApi' |
| 3 | +import type { AnyFormApi } from '../src/FormApi' |
3 | 4 |
|
4 | 5 | describe('Form reset during submit', () => { |
5 | 6 | it('should correctly reset to new default values when called during onSubmit', async () => { |
@@ -86,4 +87,53 @@ describe('Form reset during submit', () => { |
86 | 87 | expect(form.state.values.value).toBe(3) |
87 | 88 | expect(form.state.isDirty).toBe(false) |
88 | 89 | }) |
| 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 | + }) |
89 | 139 | }) |
0 commit comments