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

tanstack-form-group.spec.ts

352 lines10.4 KB

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