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

useForm.test-d.tsx

152 lines3.6 KB

1import { assertType, describe, it } from 'vitest'
2import { formOptions, useForm } from '../src/index'
3import type { FormAsyncValidateOrFn, FormValidateOrFn } from '../src/index'
4import type { ReactFormExtendedApi } from '../src/useForm'
5​
6describe('useForm', () => {
7 it('should type onSubmit properly', () => {
8 function Comp() {
9 const form = useForm({
10 defaultValues: {
11 firstName: 'test',
12 age: 84,
13 // as const is required here
14 } as const,
15 onSubmit({ value }) {
16 assertType<84>(value.age)
17 },
18 })
19 }
20 })
21​
22 it('should type a validator properly', () => {
23 function Comp() {
24 const form = useForm({
25 defaultValues: {
26 firstName: 'test',
27 age: 84,
28 // as const is required here
29 } as const,
30 validators: {
31 onChange({ value }) {
32 assertType<84>(value.age)
33 return undefined
34 },
35 },
36 })
37 }
38 })
39​
40 it('should not have recursion problems and type register properly', () => {
41 const register = <
42 TFormData,
43 TOnMount extends undefined | FormValidateOrFn<TFormData>,
44 TOnChange extends undefined | FormValidateOrFn<TFormData>,
45 TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
46 TOnBlur extends undefined | FormValidateOrFn<TFormData>,
47 TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
48 TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
49 TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
50 TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
51 TSubmitMeta,
52 >(
53 f: ReactFormExtendedApi<
54 TFormData,
55 TOnMount,
56 TOnChange,
57 TOnChangeAsync,
58 TOnBlur,
59 TOnBlurAsync,
60 TOnSubmit,
61 TOnSubmitAsync,
62 TOnServer,
63 TSubmitMeta
64 >,
65 ) => f
66​
67 function Comp() {
68 const form = useForm({
69 defaultValues: {
70 name: '',
71 title: '',
72 },
73 })
74​
75 const x = register(form)
76​
77 return null
78 }
79 })
80​
81 it('types should be properly inferred when using formOptions', () => {
82 type Person = {
83 firstName: string
84 lastName: string
85 }
86​
87 const formOpts = formOptions({
88 defaultValues: {
89 firstName: 'FirstName',
90 lastName: 'LastName',
91 } as Person,
92 })
93​
94 const form = useForm(formOpts)
95​
96 assertType<Person>(form.state.values)
97 })
98​
99 it('types should be properly inferred when passing args alongside formOptions', () => {
100 type Person = {
101 firstName: string
102 lastName: string
103 }
104​
105 const formOpts = formOptions({
106 defaultValues: {
107 firstName: 'FirstName',
108 lastName: 'LastName',
109 } as Person,
110 })
111​
112 const form = useForm({
113 ...formOpts,
114 onSubmitMeta: {
115 test: 'test',
116 },
117 })
118​
119 assertType<(submitMeta: { test: string }) => Promise<void>>(
120 form.handleSubmit,
121 )
122 })
123​
124 it('types should be properly inferred when formOptions are being overridden', () => {
125 type Person = {
126 firstName: string
127 lastName: string
128 }
129​
130 type PersonWithAge = Person & {
131 age: number
132 }
133​
134 const formOpts = formOptions({
135 defaultValues: {
136 firstName: 'FirstName',
137 lastName: 'LastName',
138 } as Person,
139 })
140​
141 const form = useForm({
142 ...formOpts,
143 defaultValues: {
144 firstName: 'FirstName',
145 lastName: 'LastName',
146 age: 10,
147 } as PersonWithAge,
148 })
149​
150 assertType<Person>(form.state.values)
151 })
152})