-
-
Notifications
You must be signed in to change notification settings - Fork 622
Expand file tree
/
Copy pathFormApi.test-d.ts
More file actions
455 lines (401 loc) · 11.2 KB
/
FormApi.test-d.ts
File metadata and controls
455 lines (401 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import { expectTypeOf, it } from 'vitest'
import { z } from 'zod'
import { FormApi, formOptions } from '../src'
import type {
DeepKeys,
GlobalFormValidationError,
StandardSchemaV1Issue,
ValidationError,
ValidationErrorMap,
} from '../src'
it('should return all errors matching the right type from getAllErrors', () => {
const form = new FormApi({
defaultValues: {
firstName: '',
lastName: '',
},
validators: {
onChange: () => ['onChange'] as const,
onMount: () => 10 as const,
onBlur: () => ({ onBlur: true as const, onBlurNumber: 1 }),
onSubmit: () => 'onSubmit' as const,
onBlurAsync: () => Promise.resolve('onBlurAsync' as const),
onChangeAsync: () => Promise.resolve('onChangeAsync' as const),
onSubmitAsync: () => Promise.resolve('onSubmitAsync' as const),
},
})
const errors = form.getAllErrors()
errors.form.errorMap.onChange
expectTypeOf(errors.form.errorMap).toEqualTypeOf<{
onBlur?: { onBlur: true; onBlurNumber: number } | 'onBlurAsync'
onChange?: readonly ['onChange'] | 'onChangeAsync'
onMount?: 10
onSubmit?: 'onSubmit' | 'onSubmitAsync'
onServer?: undefined
onDynamic?: undefined
}>()
expectTypeOf(errors.form.errors).toEqualTypeOf<
(
| readonly ['onChange']
| 'onChangeAsync'
| 10
| 'onSubmit'
| 'onSubmitAsync'
| { onBlur: true; onBlurNumber: number }
| 'onBlurAsync'
| undefined
)[]
>()
expectTypeOf(errors.fields).toEqualTypeOf<{
firstName: {
errors: ValidationError[]
errorMap: ValidationErrorMap
}
lastName: {
errors: ValidationError[]
errorMap: ValidationErrorMap
}
}>()
})
it('should type handleSubmit as never when onSubmitMeta is not passed', () => {
const form = new FormApi({
defaultValues: {
name: 'test',
},
} as const)
expectTypeOf(form.handleSubmit).toEqualTypeOf<{
(): Promise<void>
(submitMeta: never): Promise<void>
}>()
})
type OnSubmitMeta = {
group: string
}
it('should type handleChange correctly', () => {
const form = new FormApi({
defaultValues: {
name: 'test',
},
onSubmitMeta: {} as OnSubmitMeta,
} as const)
form.handleSubmit({ group: 'track' })
expectTypeOf(form.handleSubmit).toEqualTypeOf<{
(): Promise<void>
(submitMeta: OnSubmitMeta): Promise<void>
}>()
})
type FormLevelStandardSchemaIssue = {
form: Record<string, StandardSchemaV1Issue[]>
fields: Record<string, StandardSchemaV1Issue[]>
}
it('should only have form-level error types returned from parseFieldValuesWithSchema and parseFieldValuesWithSchemaAsync', () => {
const form = new FormApi({
defaultValues: { name: '' },
})
form.mount()
const schema = z.object({
name: z.string(),
})
// assert that it doesn't think it's a field-level error
expectTypeOf(form.parseValuesWithSchema(schema)).toEqualTypeOf<
FormLevelStandardSchemaIssue | undefined
>()
expectTypeOf(form.parseValuesWithSchemaAsync(schema)).toEqualTypeOf<
Promise<FormLevelStandardSchemaIssue | undefined>
>()
})
it("should allow setting manual errors according to the validator's return type", () => {
type FormData = {
firstName: string
lastName: string
}
const form = new FormApi({
defaultValues: {} as FormData,
validators: {
onChange: () => ['onChange'] as const,
onMount: () => 10 as const,
onBlur: () => ({ onBlur: true as const, onBlurNumber: 1 }),
onSubmit: () => 'onSubmit' as const,
onBlurAsync: () => Promise.resolve('onBlurAsync' as const),
onDynamic: () => 'onDynamic' as const,
onDynamicAsync: () => Promise.resolve('onDynamicAsync' as const),
onChangeAsync: () => Promise.resolve('onChangeAsync' as const),
onSubmitAsync: () => Promise.resolve('onSubmitAsync' as const),
},
})
form.setErrorMap({
onMount: 10,
onChange: ['onChange'],
})
expectTypeOf(form.setErrorMap).parameter(0).toEqualTypeOf<{
onMount: 10 | undefined | GlobalFormValidationError<FormData>
onChange:
| readonly ['onChange']
| 'onChangeAsync'
| undefined
| GlobalFormValidationError<FormData>
onBlur:
| { onBlur: true; onBlurNumber: number }
| 'onBlurAsync'
| undefined
| GlobalFormValidationError<FormData>
onSubmit:
| 'onSubmit'
| 'onSubmitAsync'
| undefined
| GlobalFormValidationError<FormData>
onDynamic:
| 'onDynamic'
| 'onDynamicAsync'
| undefined
| GlobalFormValidationError<FormData>
onServer: undefined
}>
})
it('should allow setting field errors from the global form error map', () => {
type FormData = {
firstName: string
lastName: string
}
const form = new FormApi({
defaultValues: {} as FormData,
})
form.setErrorMap({
onChange: {
fields: {
firstName: 'error',
// @ts-expect-error
nonExistentField: 'error',
},
},
})
})
it('should not allow setting manual errors if no validator is specified', () => {
type FormData = {
firstName: string
lastName: string
}
const form = new FormApi({
defaultValues: {} as FormData,
})
expectTypeOf(form.setErrorMap).parameter(0).toEqualTypeOf<{
onMount: undefined | GlobalFormValidationError<FormData>
onChange: undefined | GlobalFormValidationError<FormData>
onBlur: undefined | GlobalFormValidationError<FormData>
onSubmit: undefined | GlobalFormValidationError<FormData>
onDynamic: undefined | GlobalFormValidationError<FormData>
onServer: undefined
}>
})
it('should only allow array fields for array-specific methods', () => {
type FormValues = {
name: string
age: number
startDate: Date
title: string | null | undefined
relatives: { name: string }[]
counts: (number | null | undefined)[]
}
const defaultValues: FormValues = {
name: '',
age: 0,
startDate: new Date(),
title: null,
relatives: [{ name: '' }],
counts: [5, null, undefined, 3],
}
const form = new FormApi({
defaultValues,
})
form.mount()
type AllKeys = DeepKeys<FormValues>
type OnlyArrayKeys = Extract<AllKeys, 'counts' | 'relatives'>
type RandomKeys = Extract<AllKeys, 'counts' | 'relatives' | 'title'>
const push1 = form.pushFieldValue<OnlyArrayKeys>
// @ts-expect-error too wide!
const push2 = form.pushFieldValue<AllKeys>
// @ts-expect-error too wide!
const push3 = form.pushFieldValue<RandomKeys>
const insert1 = form.insertFieldValue<OnlyArrayKeys>
// @ts-expect-error too wide!
const insert2 = form.insertFieldValue<AllKeys>
// @ts-expect-error too wide!
const insert3 = form.insertFieldValue<RandomKeys>
const replace1 = form.replaceFieldValue<OnlyArrayKeys>
// @ts-expect-error too wide!
const replace2 = form.replaceFieldValue<AllKeys>
// @ts-expect-error too wide!
const replace3 = form.replaceFieldValue<RandomKeys>
const remove1 = form.removeFieldValue<OnlyArrayKeys>
// @ts-expect-error too wide!
const remove2 = form.removeFieldValue<AllKeys>
// @ts-expect-error too wide!
const remove3 = form.removeFieldValue<RandomKeys>
const swap1 = form.swapFieldValues<OnlyArrayKeys>
// @ts-expect-error too wide!
const swap2 = form.swapFieldValues<AllKeys>
// @ts-expect-error too wide!
const swap3 = form.swapFieldValues<RandomKeys>
const move1 = form.moveFieldValues<OnlyArrayKeys>
// @ts-expect-error too wide!
const move2 = form.moveFieldValues<AllKeys>
// @ts-expect-error too wide!
const move3 = form.moveFieldValues<RandomKeys>
const validate1 = form.validateArrayFieldsStartingFrom<OnlyArrayKeys>
// @ts-expect-error too wide!
const validate2 = form.validateArrayFieldsStartingFrom<AllKeys>
// @ts-expect-error too wide!
const validate3 = form.validateArrayFieldsStartingFrom<RandomKeys>
const validate4 = form.validateArrayFields<OnlyArrayKeys>
// @ts-expect-error too wide!
const validate5 = form.validateArrayFields<AllKeys>
// @ts-expect-error too wide!
const validate6 = form.validateArrayFields<RandomKeys>
})
it('should infer full field name union for form.resetField parameters', () => {
type FormData = {
shallow: string
nested: {
field: {
name: string
}
}
}
const defaultValue = {
shallow: '',
nested: {
field: {
name: '',
},
},
}
const form = new FormApi({
defaultValues: defaultValue as FormData,
})
expectTypeOf(form.resetField)
.parameter(0)
.toEqualTypeOf<
'shallow' | 'nested' | 'nested.field' | 'nested.field.name'
>()
})
it('should extract the form error type from a global form error', () => {
type FormData = {
firstName: string
lastName: string
}
const form = new FormApi({
defaultValues: {} as FormData,
validators: {
onMount: () => {
return {
form: 'onMount' as const,
fields: {},
}
},
onChange: () => {
return {
form: 'onChange' as const,
fields: {},
}
},
onChangeAsync: () => {
return Promise.resolve({
form: 'onChangeAsync' as const,
fields: {},
})
},
onBlur: () => {
return {
form: 'onBlur' as const,
fields: {},
}
},
onBlurAsync: () => {
return Promise.resolve('onBlurAsync' as const)
},
},
})
expectTypeOf(form.state.errorMap.onChange).toEqualTypeOf<
'onChange' | 'onChangeAsync' | undefined
>()
expectTypeOf(form.state.errorMap.onMount).toEqualTypeOf<
'onMount' | undefined
>()
expectTypeOf(form.state.errorMap.onBlur).toEqualTypeOf<
'onBlur' | 'onBlurAsync' | undefined
>()
expectTypeOf(form.state.errors).toEqualTypeOf<
(
| 'onMount'
| 'onChange'
| 'onChangeAsync'
| 'onBlur'
| 'onBlurAsync'
| undefined
)[]
>
})
it('listeners should be typed correctly', () => {
type FormData = {
firstName: string
lastName: string
}
const form = new FormApi({
defaultValues: {
firstName: '',
lastName: '',
} as FormData,
listeners: {
onSubmit: ({ formApi }) => {
expectTypeOf(formApi.state.values).toEqualTypeOf<FormData>()
},
},
})
form.handleSubmit()
})
it('listeners should be typed correctly when using formOptions', () => {
type FormData = {
firstName: string
lastName: string
}
const formOpts = formOptions({
defaultValues: {
firstName: 'FirstName',
lastName: 'LastName',
} as FormData,
validators: {
onSubmit: () => {
return {
test: 'test',
}
},
},
listeners: {
onSubmit: ({ formApi }) => {
expectTypeOf(formApi.state.values).toEqualTypeOf<FormData>()
},
},
})
const form = new FormApi({
...formOpts,
listeners: {
// this doesn't error since listeners return void
onSubmit: ({ formApi }) => {
console.log(formApi.state.values)
},
},
validators: {
// this errors becuase the return type is not the same as the validator return type
// onSubmit: () => 'custom on submit',
onSubmit: () => {
return {
test: 'can change the value!',
}
},
onChange: () => {
return 'onChange'
},
},
})
form.handleSubmit()
})