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

createFormGroup.test-d.tsx

389 lines9.9 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { createForm, createFormGroup } from '../src/index'
3import type { FormGroupApi } from '../src/index'
4​
5describe('createFormGroup form-like surface', () => {
6 it('should type state.value based on the selected field', () => {
7 function Comp() {
8 const form = createForm(() => ({
9 defaultValues: {
10 step1: { name: 'test' },
11 step2: { name: 'test2' },
12 },
13 }))
14​
15 const group = createFormGroup(() => ({
16 form,
17 name: 'step1',
18 onGroupSubmit: () => {},
19 }))
20​
21 expectTypeOf(group().state.value).toEqualTypeOf<{ name: string }>()
22 expectTypeOf(group().name).toEqualTypeOf<'step1'>()
23 }
24 })
25​
26 it('should type onGroupSubmit value and meta', () => {
27 type SubmitMeta = { source: string }
28​
29 function Comp() {
30 const form = createForm(() => ({
31 defaultValues: {
32 step1: { name: 'test' },
33 step2: { name: 'test2' },
34 },
35 }))
36​
37 const group = createFormGroup(() => ({
38 form,
39 name: 'step1',
40 onSubmitMeta: {} as SubmitMeta,
41 onGroupSubmit: ({ value, meta }) => {
42 expectTypeOf(value).toEqualTypeOf<{ name: string }>()
43 expectTypeOf(meta).toEqualTypeOf<SubmitMeta>()
44 },
45 }))
46 }
47 })
48​
49 it('should type onGroupSubmitInvalid value and meta', () => {
50 type SubmitMeta = { source: string }
51​
52 function Comp() {
53 const form = createForm(() => ({
54 defaultValues: {
55 step1: { name: 'test' },
56 step2: { name: 'test2' },
57 },
58 }))
59​
60 const group = createFormGroup(() => ({
61 form,
62 name: 'step1',
63 onSubmitMeta: {} as SubmitMeta,
64 onGroupSubmit: () => {},
65 onGroupSubmitInvalid: ({ value, meta }) => {
66 expectTypeOf(value).toEqualTypeOf<{ name: string }>()
67 expectTypeOf(meta).toEqualTypeOf<SubmitMeta>()
68 },
69 }))
70 }
71 })
72​
73 it('should type validators with the scoped value', () => {
74 function Comp() {
75 const form = createForm(() => ({
76 defaultValues: {
77 step1: { name: 'test', age: 10 },
78 step2: { name: 'test2' },
79 },
80 }))
81​
82 const group = createFormGroup(() => ({
83 form,
84 name: 'step1',
85 onGroupSubmit: () => {},
86 validators: {
87 onChange: ({ value }) => {
88 expectTypeOf(value).toEqualTypeOf<{ name: string; age: number }>()
89 return undefined
90 },
91 onSubmit: ({ value }) => {
92 expectTypeOf(value).toEqualTypeOf<{ name: string; age: number }>()
93 return undefined
94 },
95 },
96 }))
97 }
98 })
99​
100 it('should type listeners with the scoped value', () => {
101 function Comp() {
102 const form = createForm(() => ({
103 defaultValues: {
104 step1: { name: 'test' },
105 step2: { name: 'test2' },
106 },
107 }))
108​
109 const group = createFormGroup(() => ({
110 form,
111 name: 'step1',
112 onGroupSubmit: () => {},
113 listeners: {
114 onChange: ({ value }) => {
115 expectTypeOf(value).toEqualTypeOf<{ name: string }>()
116 },
117 },
118 }))
119 }
120 })
121​
122 it('should type handleSubmit return as Promise<void>', () => {
123 function Comp() {
124 const form = createForm(() => ({
125 defaultValues: { step1: { name: 'test' } },
126 }))
127​
128 const group = createFormGroup(() => ({
129 form,
130 name: 'step1',
131 onGroupSubmit: () => {},
132 }))
133​
134 expectTypeOf(group().handleSubmit()).toEqualTypeOf<Promise<void>>()
135 }
136 })
137​
138 it('should type handleSubmit overload when onSubmitMeta is provided', () => {
139 type SubmitMeta = { source: string }
140​
141 function Comp() {
142 const form = createForm(() => ({
143 defaultValues: { step1: { name: 'test' } },
144 }))
145​
146 const group = createFormGroup(() => ({
147 form,
148 name: 'step1',
149 onSubmitMeta: {} as SubmitMeta,
150 onGroupSubmit: () => {},
151 }))
152​
153 expectTypeOf(group().handleSubmit).toEqualTypeOf<{
154 (): Promise<void>
155 (submitMeta: SubmitMeta): Promise<void>
156 }>()
157 }
158 })
159​
160 it('should type setValue updater', () => {
161 function Comp() {
162 const form = createForm(() => ({
163 defaultValues: {
164 step1: { name: 'test' },
165 step2: { name: 'test2' },
166 },
167 }))
168​
169 const group = createFormGroup(() => ({
170 form,
171 name: 'step1',
172 onGroupSubmit: () => {},
173 }))
174​
175 group().setValue({ name: 'new name' })
176 group().setValue((prev) => {
177 expectTypeOf(prev).toEqualTypeOf<{ name: string }>()
178 return { name: 'updated' }
179 })
180 }
181 })
182​
183 it('should infer the FormGroupApi instance type for createFormGroup', () => {
184 function Comp() {
185 const form = createForm(() => ({
186 defaultValues: {
187 step1: { name: 'test' },
188 step2: { name: 'test2' },
189 },
190 }))
191​
192 const group = createFormGroup(() => ({
193 form,
194 name: 'step1',
195 onGroupSubmit: () => {},
196 }))
197​
198 expectTypeOf(group()).toMatchTypeOf<
199 FormGroupApi<
200 any,
201 any,
202 any,
203 any,
204 any,
205 any,
206 any,
207 any,
208 any,
209 any,
210 any,
211 any,
212 any,
213 any,
214 any,
215 any,
216 any,
217 any,
218 any,
219 any,
220 any,
221 any,
222 any,
223 any
224 >
225 >()
226 }
227 })
228})
229​
230describe('createFormGroup field-like meta surface', () => {
231 it('should type meta booleans', () => {
232 function Comp() {
233 const form = createForm(() => ({
234 defaultValues: {
235 step1: { name: 'test' },
236 step2: { name: 'test2' },
237 },
238 }))
239​
240 const group = createFormGroup(() => ({
241 form,
242 name: 'step1',
243 onGroupSubmit: () => {},
244 }))
245​
246 expectTypeOf(group().state.meta.isTouched).toEqualTypeOf<boolean>()
247 expectTypeOf(group().state.meta.isBlurred).toEqualTypeOf<boolean>()
248 expectTypeOf(group().state.meta.isDirty).toEqualTypeOf<boolean>()
249 expectTypeOf(group().state.meta.isValidating).toEqualTypeOf<boolean>()
250 }
251 })
252​
253 it('should type errorMap entries based on validator return types', () => {
254 function Comp() {
255 const form = createForm(() => ({
256 defaultValues: { step1: { name: 'test' } },
257 }))
258​
259 const group = createFormGroup(() => ({
260 form,
261 name: 'step1',
262 onGroupSubmit: () => {},
263 validators: {
264 onChange: () => 'sync-change' as const,
265 onChangeAsync: async () => 'async-change' as const,
266 onBlur: () => 'sync-blur' as const,
267 onBlurAsync: async () => 'async-blur' as const,
268 onSubmit: () => 'sync-submit' as const,
269 onSubmitAsync: async () => 'async-submit' as const,
270 },
271 }))
272​
273 expectTypeOf(group().state.meta.errorMap.onChange).toEqualTypeOf<
274 'sync-change' | 'async-change' | undefined
275 >()
276 expectTypeOf(group().state.meta.errorMap.onBlur).toEqualTypeOf<
277 'sync-blur' | 'async-blur' | undefined
278 >()
279 expectTypeOf(group().state.meta.errorMap.onSubmit).toEqualTypeOf<
280 'sync-submit' | 'async-submit' | undefined
281 >()
282 }
283 })
284​
285 it('should type errors array from group validators', () => {
286 function Comp() {
287 const form = createForm(() => ({
288 defaultValues: {
289 step1: { name: 'test' },
290 step2: { name: 'test2' },
291 },
292 }))
293​
294 const group = createFormGroup(() => ({
295 form,
296 name: 'step1',
297 onGroupSubmit: () => {},
298 validators: {
299 onChange: () => 'change-error' as const,
300 },
301 }))
302​
303 expectTypeOf(group().state.meta.errors).toEqualTypeOf<
304 Array<'change-error' | undefined>
305 >()
306 }
307 })
308})
309​
310describe('form.FormGroup component surface', () => {
311 it('should type the children render prop with the scoped value', () => {
312 function Comp() {
313 const form = createForm(() => ({
314 defaultValues: {
315 step1: { name: 'test' },
316 step2: { name: 'test2' },
317 },
318 }))
319​
320 return (
321 <form.FormGroup name="step1" onGroupSubmit={() => {}}>
322 {(group) => {
323 expectTypeOf(group().state.value).toEqualTypeOf<{ name: string }>()
324 expectTypeOf(group().name).toEqualTypeOf<'step1'>()
325 return null
326 }}
327 </form.FormGroup>
328 )
329 }
330 })
331​
332 it('should type the children render prop with onSubmitMeta', () => {
333 type SubmitMeta = { source: string }
334​
335 function Comp() {
336 const form = createForm(() => ({
337 defaultValues: {
338 step1: { name: 'test' },
339 step2: { name: 'test2' },
340 },
341 }))
342​
343 return (
344 <form.FormGroup
345 name="step1"
346 onSubmitMeta={{} as SubmitMeta}
347 onGroupSubmit={({ value, meta }) => {
348 expectTypeOf(value).toEqualTypeOf<{ name: string }>()
349 expectTypeOf(meta).toEqualTypeOf<SubmitMeta>()
350 }}
351 >
352 {(group) => {
353 expectTypeOf(group().handleSubmit).toEqualTypeOf<{
354 (): Promise<void>
355 (submitMeta: SubmitMeta): Promise<void>
356 }>()
357 return null
358 }}
359 </form.FormGroup>
360 )
361 }
362 })
363​
364 it('should type validators on the FormGroup component', () => {
365 function Comp() {
366 const form = createForm(() => ({
367 defaultValues: {
368 step1: { name: 'test', age: 10 },
369 step2: { name: 'test2' },
370 },
371 }))
372​
373 return (
374 <form.FormGroup
375 name="step1"
376 validators={{
377 onChange: ({ value }) => {
378 expectTypeOf(value).toEqualTypeOf<{ name: string; age: number }>()
379 return undefined
380 },
381 }}
382 onGroupSubmit={() => {}}
383 >
384 {() => null}
385 </form.FormGroup>
386 )
387 }
388 })
389})