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

createFormHook.test-d.tsx

252 lines6.2 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { formOptions } from '@tanstack/form-core'
3import { createFormHook, createFormHookContexts } from '../src'
4import type { JSX } from 'solid-js/jsx-runtime'
5​
6const { fieldContext, useFieldContext, formContext, useFormContext } =
7 createFormHookContexts()
8​
9function Test() {
10 return null
12​
13const { useAppForm, withForm } = createFormHook({
14 fieldComponents: {
15 Test,
16 },
17 formComponents: {
18 Test,
19 },
20 fieldContext,
21 formContext,
22})
23​
24describe('createFormHook', () => {
25 it('should not break with an infinite type on large schemas', () => {
26 const ActivityKind0_Names = ['Work', 'Rest', 'OnCall'] as const
27 type ActivityKind0 = (typeof ActivityKind0_Names)[number]
28​
29 enum DayOfWeek {
30 Monday = 1,
31 Tuesday,
32 Wednesday,
33 Thursday,
34 Friday,
35 Saturday,
36 Sunday,
37 }
38​
39 interface Branding<Brand> {
40 __type?: Brand
41 }
42 type Branded<T, Brand> = T & Branding<Brand>
43 type ActivityId = Branded<string, 'ActivityId'>
44 interface ActivitySelectorFormData {
45 includeAll: boolean
46 includeActivityIds: ActivityId[]
47 includeActivityKinds: Set<ActivityKind0>
48 excludeActivityIds: ActivityId[]
49 }
50​
51 const GeneratedTypes0Visibility_Names = [
52 'Normal',
53 'Advanced',
54 'Hidden',
55 ] as const
56 type GeneratedTypes0Visibility =
57 (typeof GeneratedTypes0Visibility_Names)[number]
58 interface FormValuesBase {
59 key: string
60 visibility: GeneratedTypes0Visibility
61 }
62​
63 interface ActivityCountFormValues extends FormValuesBase {
64 _type: 'ActivityCount'
65 activitySelector: ActivitySelectorFormData
66 daysOfWeek: DayOfWeek[]
67 label: string
68 }
69​
70 interface PlanningTimesFormValues extends FormValuesBase {
71 _type: 'PlanningTimes'
72 showTarget: boolean
73 showPlanned: boolean
74 showDiff: boolean
75 }
76​
77 type EditorValues = ActivityCountFormValues | PlanningTimesFormValues
78 interface EditorFormValues<EditorType extends EditorValues = EditorValues> {
79 editors: Record<string, EditorType>
80 ordering: string[]
81 }
82​
83 const ExampleUsage = withForm({
84 props: {
85 initialValues: '' as keyof EditorFormValues['editors'],
86 },
87 defaultValues: {} as EditorFormValues,
88 render: ({ form, initialValues }) => {
89 return (
90 <div class="m-3">
91 <form.AppField name={`editors.${initialValues}.key` as const}>
92 {(field) => {
93 expectTypeOf(field().state.value).toExtend<string>()
94 return null
95 }}
96 </form.AppField>
97 <form.AppForm>
98 <form.Test />
99 </form.AppForm>
100 </div>
101 )
102 },
103 })
104 })
105​
106 it('types should be properly inferred when using formOptions', () => {
107 type Person = {
108 firstName: string
109 lastName: string
110 }
111​
112 const formOpts = formOptions({
113 defaultValues: {
114 firstName: 'FirstName',
115 lastName: 'LastName',
116 } as Person,
117 })
118​
119 const WithFormComponent = withForm({
120 ...formOpts,
121 render: ({ form }) => {
122 expectTypeOf(form.state.values).toEqualTypeOf<Person>()
123 return <form.Test />
124 },
125 })
126 })
127​
128 it('types should be properly inferred when passing args alongside formOptions', () => {
129 type Person = {
130 firstName: string
131 lastName: string
132 }
133​
134 const formOpts = formOptions({
135 defaultValues: {
136 firstName: 'FirstName',
137 lastName: 'LastName',
138 } as Person,
139 })
140​
141 const WithFormComponent = withForm({
142 ...formOpts,
143 onSubmitMeta: {
144 test: 'test',
145 },
146 render: ({ form }) => {
147 expectTypeOf(form.handleSubmit).toEqualTypeOf<{
148 (): Promise<void>
149 (submitMeta: { test: string }): Promise<void>
150 }>
151 return <form.Test />
152 },
153 })
154 })
155​
156 it('types should be properly inferred when formOptions are being overridden', () => {
157 type Person = {
158 firstName: string
159 lastName: string
160 }
161​
162 type PersonWithAge = Person & {
163 age: number
164 }
165​
166 const formOpts = formOptions({
167 defaultValues: {
168 firstName: 'FirstName',
169 lastName: 'LastName',
170 } as Person,
171 })
172​
173 const WithFormComponent = withForm({
174 ...formOpts,
175 defaultValues: {
176 firstName: 'FirstName',
177 lastName: 'LastName',
178 age: 10,
179 },
180 render: ({ form }) => {
181 expectTypeOf(form.state.values).toExtend<PersonWithAge>()
182 return <form.Test />
183 },
184 })
185 })
186​
187 it('withForm props should be properly inferred', () => {
188 const WithFormComponent = withForm({
189 props: {
190 prop1: 'test',
191 prop2: 10,
192 },
193 render: ({ form, ...props }) => {
194 expectTypeOf(props).toEqualTypeOf<{
195 prop1: string
196 prop2: number
197 children?: JSX.Element
198 }>()
199 return <form.Test />
200 },
201 })
202 })
203​
204 it("component made from withForm should have it's props properly typed", () => {
205 const formOpts = formOptions({
206 defaultValues: {
207 firstName: 'FirstName',
208 lastName: 'LastName',
209 },
210 })
211​
212 const appForm = useAppForm(() => formOpts)
213​
214 const WithFormComponent = withForm({
215 ...formOpts,
216 props: {
217 prop1: 'test',
218 prop2: 10,
219 },
220 render: ({ form, children, ...props }) => {
221 expectTypeOf(props).toEqualTypeOf<{
222 prop1: string
223 prop2: number
224 }>()
225 return <form.Test />
226 },
227 })
228​
229 const CorrectComponent = (
230 <WithFormComponent form={appForm} prop1="test" prop2={10} />
231 )
232​
233 // @ts-expect-error Missing required props prop1 and prop2
234 const MissingPropsComponent = <WithFormComponent form={appForm} />
235​
236 const incorrectFormOpts = formOptions({
237 defaultValues: {
238 firstName: 'FirstName',
239 lastName: 'LastName',
240 firstNameWrong: 'FirstName',
241 lastNameWrong: 'LastName',
242 },
243 })
244​
245 const incorrectAppForm = useAppForm(() => incorrectFormOpts)
246​
247 const IncorrectFormOptsComponent = (
248 // @ts-expect-error Incorrect form opts
249 <WithFormComponent form={incorrectAppForm} prop1="test" prop2={10} />
250 )
251 })
252})