Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

createFormGroup.test.tsx

323 lines9.1 KB

1import { describe, expect, it, vi } from 'vitest'
2import { render, waitFor } from '@solidjs/testing-library'
3import { userEvent } from '@testing-library/user-event'
4import { createForm } from '../src/index'
5​
6const user = userEvent.setup()
7​
8describe('form.FormGroup', () => {
9 it('should call onGroupSubmit but not the form onSubmit when submitting the group', async () => {
10 const onSubmit = vi.fn()
11 const onGroupSubmit = vi.fn()
12​
13 function Comp() {
14 const form = createForm(() => ({
15 defaultValues: {
16 step1: { name: 'test' },
17 step2: { name: 'test2' },
18 },
19 onSubmit,
20 }))
21​
22 return (
23 <form.FormGroup name="step1" onGroupSubmit={onGroupSubmit}>
24 {(group) => (
25 <form
26 onSubmit={(e) => {
27 e.preventDefault()
28 e.stopPropagation()
29 group().handleSubmit()
30 }}
31 >
32 <form.Field
33 name="step1.name"
34 children={(field) => (
35 <input
36 data-testid="step1-name"
37 value={field().state.value}
38 onInput={(e) => field().handleChange(e.currentTarget.value)}
39 />
40 )}
41 />
42 <button type="submit" data-testid="submit-group">
43 Submit Group
44 </button>
45 </form>
46 )}
47 </form.FormGroup>
48 )
49 }
50​
51 const { getByTestId } = render(() => <Comp />)
52 await user.click(getByTestId('submit-group'))
53​
54 await waitFor(() => expect(onGroupSubmit).toHaveBeenCalledTimes(1))
55 expect(onSubmit).not.toHaveBeenCalled()
56 })
57​
58 it('should expose group state value reactively', async () => {
59 function Comp() {
60 const form = createForm(() => ({
61 defaultValues: {
62 step1: { name: 'initial' },
63 step2: { name: 'other' },
64 },
65 }))
66​
67 return (
68 <form.FormGroup name="step1">
69 {(group) => (
70 <>
71 <form.Field
72 name="step1.name"
73 children={(field) => (
74 <input
75 data-testid="step1-name"
76 value={field().state.value}
77 onInput={(e) => field().handleChange(e.currentTarget.value)}
78 />
79 )}
80 />
81 <pre data-testid="group-value">
82 {JSON.stringify(group().state.value)}
83 </pre>
84 </>
85 )}
86 </form.FormGroup>
87 )
88 }
89​
90 const { getByTestId } = render(() => <Comp />)
91 expect(getByTestId('group-value').textContent).toBe('{"name":"initial"}')
92​
93 await user.clear(getByTestId('step1-name'))
94 await user.type(getByTestId('step1-name'), 'updated')
95​
96 await waitFor(() =>
97 expect(getByTestId('group-value').textContent).toBe('{"name":"updated"}'),
98 )
99 })
100​
101 it('should call onGroupSubmitInvalid when group-level validation fails', async () => {
102 const onSubmit = vi.fn()
103 const onGroupSubmit = vi.fn()
104 const onGroupSubmitInvalid = vi.fn()
105​
106 function Comp() {
107 const form = createForm(() => ({
108 defaultValues: {
109 step1: { name: '' },
110 step2: { name: 'test2' },
111 },
112 onSubmit,
113 }))
114​
115 return (
116 <form.FormGroup
117 name="step1"
118 validators={{
119 onSubmit: ({ value }) =>
120 !value.name ? 'Name is required' : undefined,
121 }}
122 onGroupSubmit={onGroupSubmit}
123 onGroupSubmitInvalid={onGroupSubmitInvalid}
124 >
125 {(group) => (
126 <form
127 onSubmit={(e) => {
128 e.preventDefault()
129 e.stopPropagation()
130 group().handleSubmit()
131 }}
132 >
133 <form.Field
134 name="step1.name"
135 children={(field) => (
136 <input
137 data-testid="step1-name"
138 value={field().state.value}
139 onInput={(e) => field().handleChange(e.currentTarget.value)}
140 />
141 )}
142 />
143 <button type="submit" data-testid="submit-group">
144 Submit Group
145 </button>
146 <pre data-testid="group-error">
147 {String(group().state.meta.errorMap.onSubmit ?? '')}
148 </pre>
149 </form>
150 )}
151 </form.FormGroup>
152 )
153 }
154​
155 const { getByTestId } = render(() => <Comp />)
156 await user.click(getByTestId('submit-group'))
157​
158 await waitFor(() => expect(onGroupSubmitInvalid).toHaveBeenCalledTimes(1))
159 expect(onGroupSubmit).not.toHaveBeenCalled()
160 expect(onSubmit).not.toHaveBeenCalled()
161 await waitFor(() =>
162 expect(getByTestId('group-error').textContent).toBe('Name is required'),
163 )
164 })
165​
166 it('should ignore form-level field errors outside the group when submitting the group', async () => {
167 const onGroupSubmit = vi.fn()
168 const onGroupSubmitInvalid = vi.fn()
169​
170 function Comp() {
171 const form = createForm(() => ({
172 defaultValues: {
173 step1: { name: 'test' },
174 step2: { name: 'test2' },
175 },
176 validators: {
177 onSubmit: () => ({
178 fields: {
179 'step2.name': 'Required',
180 },
181 }),
182 },
183 }))
184​
185 return (
186 <form.FormGroup
187 name="step1"
188 onGroupSubmit={onGroupSubmit}
189 onGroupSubmitInvalid={onGroupSubmitInvalid}
190 >
191 {(group) => (
192 <form
193 onSubmit={(e) => {
194 e.preventDefault()
195 e.stopPropagation()
196 group().handleSubmit()
197 }}
198 >
199 <form.Field
200 name="step1.name"
201 children={(field) => (
202 <input
203 data-testid="step1-name"
204 value={field().state.value}
205 onInput={(e) => field().handleChange(e.currentTarget.value)}
206 />
207 )}
208 />
209 <button type="submit" data-testid="submit-group">
210 Submit Group
211 </button>
212 </form>
213 )}
214 </form.FormGroup>
215 )
216 }
217​
218 const { getByTestId } = render(() => <Comp />)
219 await user.click(getByTestId('submit-group'))
220​
221 await waitFor(() => expect(onGroupSubmit).toHaveBeenCalledTimes(1))
222 expect(onGroupSubmitInvalid).not.toHaveBeenCalled()
223 })
224​
225 it('should pass submit meta through handleSubmit', async () => {
226 const onGroupSubmit = vi.fn()
227​
228 function Comp() {
229 const form = createForm(() => ({
230 defaultValues: {
231 step1: { name: 'test' },
232 step2: { name: 'test2' },
233 },
234 }))
235​
236 return (
237 <form.FormGroup
238 name="step1"
239 onGroupSubmit={onGroupSubmit}
240 onSubmitMeta={{} as { source: string }}
241 >
242 {(group) => (
243 <button
244 type="button"
245 data-testid="submit-group"
246 onClick={() => group().handleSubmit({ source: 'button' })}
247 >
248 Submit Group
249 </button>
250 )}
251 </form.FormGroup>
252 )
253 }
254​
255 const { getByTestId } = render(() => <Comp />)
256 await user.click(getByTestId('submit-group'))
257​
258 await waitFor(() => expect(onGroupSubmit).toHaveBeenCalledTimes(1))
259 expect(onGroupSubmit).toHaveBeenCalledWith(
260 expect.objectContaining({
261 value: { name: 'test' },
262 meta: { source: 'button' },
263 }),
264 )
265 })
266​
267 it('should rerender group.state.meta.isSubmitting during an async submit', async () => {
268 let resolveSubmit!: () => void
269 const onGroupSubmit = vi.fn(
270 () =>
271 new Promise<void>((resolve) => {
272 resolveSubmit = resolve
273 }),
274 )
275​
276 function Comp() {
277 const form = createForm(() => ({
278 defaultValues: {
279 step1: { name: 'test' },
280 step2: { name: 'test2' },
281 },
282 }))
283​
284 return (
285 <form.FormGroup name="step1" onGroupSubmit={onGroupSubmit}>
286 {(group) => (
287 <form
288 onSubmit={(e) => {
289 e.preventDefault()
290 e.stopPropagation()
291 void group().handleSubmit()
292 }}
293 >
294 <button
295 type="submit"
296 data-testid="submit-group"
297 disabled={group().state.meta.isSubmitting}
298 >
299 {group().state.meta.isSubmitting ? 'Saving...' : 'Continue'}
300 </button>
301 </form>
302 )}
303 </form.FormGroup>
304 )
305 }
306​
307 const { getByTestId } = render(() => <Comp />)
308 const button = getByTestId('submit-group') as HTMLButtonElement
309 expect(button.textContent).toBe('Continue')
310 expect(button.disabled).toBe(false)
311​
312 await user.click(button)
313​
314 await waitFor(() => expect(button.textContent).toBe('Saving...'))
315 expect(button.disabled).toBe(true)
316​
317 resolveSubmit()
318​
319 await waitFor(() => expect(button.textContent).toBe('Continue'))
320 expect(button.disabled).toBe(false)
321 expect(onGroupSubmit).toHaveBeenCalledTimes(1)
322 })
323})