-
-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathuseForm.test.tsx
129 lines (113 loc) · 3.58 KB
/
useForm.test.tsx
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
/// <reference lib="dom" />
import { h, defineComponent, ref } from 'vue-demi'
import { render, waitFor } from '@testing-library/vue'
import '@testing-library/jest-dom'
import {
createFormFactory,
type FieldApi,
provideFormContext,
useForm,
} from '../index'
import userEvent from '@testing-library/user-event'
import * as React from 'react'
const user = userEvent.setup()
describe('useForm', () => {
it('preserved field state', async () => {
type Person = {
firstName: string
lastName: string
}
const formFactory = createFormFactory<Person>()
const Comp = defineComponent(() => {
const form = formFactory.useForm()
provideFormContext({ formApi: form })
return () => (
<form.Field name="firstName" defaultValue="">
{(field: FieldApi<string, Person>) => (
<input
data-testid={'fieldinput'}
value={field.state.value}
onBlur={field.handleBlur}
onInput={(e) =>
field.handleChange((e.target as HTMLInputElement).value)
}
/>
)}
</form.Field>
)
})
const { getByTestId, queryByText } = render(Comp)
const input = getByTestId('fieldinput')
expect(queryByText('FirstName')).not.toBeInTheDocument()
await user.type(input, 'FirstName')
expect(input).toHaveValue('FirstName')
})
it('should allow default values to be set', async () => {
type Person = {
firstName: string
lastName: string
}
const formFactory = createFormFactory<Person>()
const Comp = defineComponent(() => {
const form = formFactory.useForm({
defaultValues: {
firstName: 'FirstName',
lastName: 'LastName',
},
})
form.provideFormContext()
return () => (
<form.Field name="firstName" defaultValue="">
{(field: FieldApi<string, Person>) => <p>{field.state.value}</p>}
</form.Field>
)
})
const { findByText, queryByText } = render(Comp)
expect(await findByText('FirstName')).toBeInTheDocument()
expect(queryByText('LastName')).not.toBeInTheDocument()
})
it('should handle submitting properly', async () => {
const Comp = defineComponent(() => {
const submittedData = ref<{ firstName: string }>()
const form = useForm({
defaultValues: {
firstName: 'FirstName',
},
onSubmit: (data) => {
submittedData.value = data
},
})
form.provideFormContext()
return () => (
<form.Provider>
<form.Field name="firstName">
{(field: FieldApi<string, { firstName: string }>) => {
return (
<input
value={field.state.value}
onBlur={field.handleBlur}
onChange={(e) =>
field.handleChange((e.target as HTMLInputElement).value)
}
placeholder={'First name'}
/>
)
}}
</form.Field>
<button onClick={form.handleSubmit}>Submit</button>
{submittedData.value && (
<p>Submitted data: {submittedData.value.firstName}</p>
)}
</form.Provider>
)
})
const { findByPlaceholderText, getByText } = render(Comp)
const input = await findByPlaceholderText('First name')
await user.clear(input)
await user.type(input, 'OtherName')
await user.click(getByText('Submit'))
await waitFor(() =>
expect(getByText('Submitted data: OtherName')).toBeInTheDocument(),
)
})
})