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

test.component.spec.ts

365 lines10.3 KB

1import { render } from '@testing-library/angular'
2import { Component } from '@angular/core'
3import { describe, expect, it } from 'vitest'
4import { userEvent } from '@testing-library/user-event'
5import { TanStackField, injectForm } from '../src/index'
6import { sleep } from './utils'
7import type { FieldValidateAsyncFn, FieldValidateFn } from '@tanstack/form-core'
8​
9const user = userEvent.setup()
10​
11describe('TanStackFieldDirective', () => {
12 it('should allow to set default value', async () => {
13 @Component({
14 selector: 'test-component',
15 standalone: true,
16 template: `
17 <ng-container [tanstackField]="form" name="firstName" #f="field">
18 <input
19 data-testid="fieldinput"
20 [value]="f.api.state.value"
21 (blur)="f.api.handleBlur()"
22 (input)="f.api.handleChange($any($event).target.value)"
23 />
24 </ng-container>
25 `,
26 imports: [TanStackField],
27 })
28 class TestComponent {
29 form = injectForm({
30 defaultValues: {
31 firstName: 'FirstName',
32 lastName: 'LastName',
33 },
34 })
35 }
36​
37 const { getByTestId } = await render(TestComponent)
38​
39 const input = getByTestId('fieldinput')
40 expect(input).toHaveValue('FirstName')
41 })
42​
43 it('should use field default value first', async () => {
44 type Person = {
45 firstName: string
46 lastName: string
47 }
48​
49 @Component({
50 selector: 'test-component',
51 standalone: true,
52 template: `
53 <ng-container
54 [tanstackField]="form"
55 name="firstName"
56 defaultValue="otherName"
57 #f="field"
58 >
59 <input
60 data-testid="fieldinput"
61 [value]="f.api.state.value"
62 (blur)="f.api.handleBlur()"
63 (input)="f.api.handleChange($any($event).target.value)"
64 />
65 </ng-container>
66 `,
67 imports: [TanStackField],
68 })
69 class TestComponent {
70 form = injectForm<Person>({
71 defaultValues: {
72 firstName: 'FirstName',
73 lastName: 'LastName',
74 },
75 })
76 }
77​
78 const { getByTestId } = await render(TestComponent)
79​
80 const input = getByTestId('fieldinput')
81 expect(input).toHaveValue('otherName')
82 })
83​
84 it('should not validate on change if isTouched is false', async () => {
85 type Person = {
86 firstName: string
87 lastName: string
88 }
89 const error = 'Please enter a different value'
90​
91 @Component({
92 selector: 'test-component',
93 standalone: true,
94 template: `
95 <ng-container
96 [tanstackField]="form"
97 name="firstName"
98 [validators]="{ onChange: otherValidator }"
99 #f="field"
100 >
101 <input
102 data-testid="fieldinput"
103 [value]="f.api.state.value"
104 (blur)="f.api.handleBlur()"
105 (input)="
106 f.api.setValue($any($event).target.value, {
107 dontUpdateMeta: true
108 })
109 "
110 />
111 @for (error of f.api.getMeta().errors; track error) {
112 <p>{{ error }}</p>
113 }
114 </ng-container>
115 `,
116 imports: [TanStackField],
117 })
118 class TestComponent {
119 otherValidator: FieldValidateFn<Person, 'firstName'> = ({ value }) =>
120 value === 'other' ? error : undefined
121​
122 form = injectForm<Person>({
123 defaultValues: {
124 firstName: 'FirstName',
125 lastName: 'LastName',
126 },
127 })
128 }
129​
130 const { getByTestId, queryByText } = await render(TestComponent)
131 const input = getByTestId('fieldinput')
132 await user.type(input, 'other')
133 expect(queryByText(error)).not.toBeInTheDocument()
134 })
135​
136 it('should validate on change if isTouched is true', async () => {
137 type Person = {
138 firstName: string
139 lastName: string
140 }
141 const error = 'Please enter a different value'
142​
143 @Component({
144 selector: 'test-component',
145 standalone: true,
146 template: `
147 <ng-container
148 [tanstackField]="form"
149 name="firstName"
150 [defaultMeta]="{ isTouched: true }"
151 [validators]="{ onChange: otherValidator }"
152 #f="field"
153 >
154 <input
155 data-testid="fieldinput"
156 [value]="f.api.state.value"
157 (blur)="f.api.handleBlur()"
158 (input)="f.api.handleChange($any($event).target.value)"
159 />
160 <p>{{ f.api.getMeta().errorMap?.onChange }}</p>
161 </ng-container>
162 `,
163 imports: [TanStackField],
164 })
165 class TestComponent {
166 otherValidator: FieldValidateFn<Person, 'firstName'> = ({ value }) =>
167 value === 'other' ? error : undefined
168​
169 form = injectForm<Person>({
170 defaultValues: {
171 firstName: '',
172 lastName: '',
173 },
174 })
175 }
176​
177 const { getByTestId, queryByText, getByText } = await render(TestComponent)
178​
179 const input = getByTestId('fieldinput')
180 expect(queryByText(error)).not.toBeInTheDocument()
181 await user.type(input, 'other')
182 expect(getByText(error)).toBeInTheDocument()
183 })
184​
185 it('should validate on change and on blur', async () => {
186 type Person = {
187 firstName: string
188 lastName: string
189 }
190 const onChangeError = 'Please enter a different value (onChangeError)'
191 const onBlurError = 'Please enter a different value (onBlurError)'
192​
193 @Component({
194 selector: 'test-component',
195 standalone: true,
196 template: `
197 <ng-container
198 [tanstackField]="form"
199 name="firstName"
200 [defaultMeta]="{ isTouched: true }"
201 [validators]="{ onChange: onChange, onBlur: onBlur }"
202 #f="field"
203 >
204 <input
205 data-testid="fieldinput"
206 [value]="f.api.state.value"
207 [name]="f.api.name"
208 (blur)="f.api.handleBlur()"
209 (input)="
210 f.api.setValue($any($event).target.value, {
211 dontUpdateMeta: true
212 })
213 "
214 />
215 <p>{{ f.api.getMeta().errorMap?.onChange }}</p>
216 <p>{{ f.api.getMeta().errorMap?.onBlur }}</p>
217 </ng-container>
218 `,
219 imports: [TanStackField],
220 })
221 class TestComponent {
222 onChange: FieldValidateFn<Person, 'firstName'> = ({ value }) =>
223 value === 'other' ? onChangeError : undefined
224 onBlur: FieldValidateFn<Person, 'firstName'> = ({ value }) =>
225 value === 'other' ? onBlurError : undefined
226​
227 form = injectForm<Person>({
228 defaultValues: {
229 firstName: '',
230 lastName: '',
231 },
232 })
233 }
234​
235 const { getByTestId, getByText, queryByText } = await render(TestComponent)
236 const input = getByTestId('fieldinput')
237 expect(queryByText(onChangeError)).not.toBeInTheDocument()
238 expect(queryByText(onBlurError)).not.toBeInTheDocument()
239 await user.type(input, 'other')
240 expect(getByText(onChangeError)).toBeInTheDocument()
241 await user.click(document.body)
242 expect(queryByText(onBlurError)).toBeInTheDocument()
243 })
244​
245 it('should validate async on change', async () => {
246 type Person = {
247 firstName: string
248 lastName: string
249 }
250 const error = 'Please enter a different value'
251​
252 @Component({
253 selector: 'test-component',
254 standalone: true,
255 template: `
256 <ng-container
257 [tanstackField]="form"
258 name="firstName"
259 [defaultMeta]="{ isTouched: true }"
260 [validators]="{ onChangeAsync: onChangeAsync }"
261 #f="field"
262 >
263 <input
264 data-testid="fieldinput"
265 [name]="f.api.name"
266 [value]="f.api.state.value"
267 (blur)="f.api.handleBlur()"
268 (input)="f.api.handleChange($any($event).target.value)"
269 />
270 <p>{{ f.api.getMeta().errorMap?.onChange }}</p>
271 </ng-container>
272 `,
273 imports: [TanStackField],
274 })
275 class TestComponent {
276 onChangeAsync: FieldValidateAsyncFn<Person, 'firstName'> = async () => {
277 await sleep(10)
278 return error
279 }
280​
281 form = injectForm<Person>({
282 defaultValues: {
283 firstName: '',
284 lastName: '',
285 },
286 })
287 }
288​
289 const { getByTestId, getByText, findByText, queryByText } =
290 await render(TestComponent)
291 const input = getByTestId('fieldinput')
292 expect(queryByText(error)).not.toBeInTheDocument()
293 await user.type(input, 'other')
294 await findByText(error)
295 expect(getByText(error)).toBeInTheDocument()
296 })
297​
298 it('should validate async on change and async on blur', async () => {
299 type Person = {
300 firstName: string
301 lastName: string
302 }
303 const onChangeError = 'Please enter a different value (onChangeError)'
304 const onBlurError = 'Please enter a different value (onBlurError)'
305​
306 @Component({
307 selector: 'test-component',
308 standalone: true,
309 template: `
310 <ng-container
311 [tanstackField]="form"
312 name="firstName"
313 [defaultMeta]="{ isTouched: true }"
314 [validators]="{
315 onChangeAsync: onChangeAsync,
316 onBlurAsync: onBlurAsync
317 }"
318 #f="field"
319 >
320 <input
321 data-testid="fieldinput"
322 [name]="f.api.name"
323 [value]="f.api.state.value"
324 (blur)="f.api.handleBlur()"
325 (input)="f.api.handleChange($any($event).target.value)"
326 />
327 <p>{{ f.api.getMeta().errorMap?.onChange }}</p>
328 <p>{{ f.api.getMeta().errorMap?.onBlur }}</p>
329 </ng-container>
330 `,
331 imports: [TanStackField],
332 })
333 class TestComponent {
334 onChangeAsync: FieldValidateAsyncFn<Person, 'firstName'> = async () => {
335 await sleep(10)
336 return onChangeError
337 }
338​
339 onBlurAsync: FieldValidateAsyncFn<Person, 'firstName'> = async () => {
340 await sleep(10)
341 return onBlurError
342 }
343​
344 form = injectForm<Person>({
345 defaultValues: {
346 firstName: '',
347 lastName: '',
348 },
349 })
350 }
351​
352 const { getByTestId, getByText, findByText, queryByText } =
353 await render(TestComponent)
354 const input = getByTestId('fieldinput')
355​
356 expect(queryByText(onChangeError)).not.toBeInTheDocument()
357 expect(queryByText(onBlurError)).not.toBeInTheDocument()
358 await user.type(input, 'other')
359 await findByText(onChangeError)
360 expect(getByText(onChangeError)).toBeInTheDocument()
361 await user.click(document.body)
362 await findByText(onBlurError)
363 expect(getByText(onBlurError)).toBeInTheDocument()
364 })
365})