Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import { useForm, useFormGroup } from '../src/index'
3import type { FormGroupApi } from '../src/index'
4β
5describe('useFormGroup form-like surface', () => {
6 it('should type state.value based on the selected field', () => {
7 function Comp() {
8 const form = useForm({
9 defaultValues: {
10 step1: { name: 'test' },
11 step2: { name: 'test2' },
12 },
13 })
14β
15 const group = useFormGroup({
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 = useForm({
31 defaultValues: {
32 step1: { name: 'test' },
33 step2: { name: 'test2' },
34 },
35 })
36β
37 const group = useFormGroup({
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 = useForm({
54 defaultValues: {
55 step1: { name: 'test' },
56 step2: { name: 'test2' },
57 },
58 })
59β
60 const group = useFormGroup({
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 = useForm({
76 defaultValues: {
77 step1: { name: 'test', age: 10 },
78 step2: { name: 'test2' },
79 },
80 })
81β
82 const group = useFormGroup({
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 = useForm({
103 defaultValues: {
104 step1: { name: 'test' },
105 step2: { name: 'test2' },
106 },
107 })
108β
109 const group = useFormGroup({
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 = useForm({
125 defaultValues: { step1: { name: 'test' } },
126 })
127β
128 const group = useFormGroup({
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 = useForm({
143 defaultValues: { step1: { name: 'test' } },
144 })
145β
146 const group = useFormGroup({
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 = useForm({
163 defaultValues: {
164 step1: { name: 'test' },
165 step2: { name: 'test2' },
166 },
167 })
168β
169 const group = useFormGroup({
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 useFormGroup', () => {
184 function Comp() {
185 const form = useForm({
186 defaultValues: {
187 step1: { name: 'test' },
188 step2: { name: 'test2' },
189 },
190 })
191β
192 const group = useFormGroup({
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('useFormGroup field-like meta surface', () => {
231 it('should type meta booleans', () => {
232 function Comp() {
233 const form = useForm({
234 defaultValues: {
235 step1: { name: 'test' },
236 step2: { name: 'test2' },
237 },
238 })
239β
240 const group = useFormGroup({
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 = useForm({
256 defaultValues: { step1: { name: 'test' } },
257 })
258β
259 const group = useFormGroup({
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 = useForm({
288 defaultValues: {
289 step1: { name: 'test' },
290 step2: { name: 'test2' },
291 },
292 })
293β
294 const group = useFormGroup({
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 = useForm({
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 = useForm({
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 = useForm({
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})