-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvue-tiny-validator.test.ts
159 lines (133 loc) · 3.49 KB
/
vue-tiny-validator.test.ts
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
import { createApp, defineComponent, ref, h } from 'vue'
import { useField, useForm } from './vue-tiny-validator'
function mount<V>(Comp: V) {
const el = document.createElement('div')
const app = createApp(Comp)
const unmount = () => app.unmount()
const comp = app.mount(el) as any
comp.unmount = unmount
return comp
}
describe('Tiny Validator', () => {
it('validates fields without a parent form', () => {
const input = ref('hello')
const isRequired = (v: string) => !!v || 'Required'
const vm = mount(
defineComponent({
setup() {
return useField({
value: input,
rules: [isRequired],
})
},
render() {},
})
)
expect(vm.validate()).toBeTruthy()
expect(vm.error).toBeFalsy()
input.value = ''
expect(vm.validate()).toBeFalsy()
expect(vm.error).toEqual('Required')
vm.reset()
expect(vm.error).toBeFalsy()
})
it('validates forms with multiple children', () => {
const input1 = ref('')
const input2 = ref('')
const isRequired = (v: string) => !!v || 'Required'
const child = defineComponent({
setup() {
const field1 = useField({
value: input1,
rules: [isRequired],
identifier: 'i1',
})
const field2 = useField({
value: input2,
rules: [isRequired],
identifier: 'i2',
})
return { field1, field2 }
},
render() {},
})
const vm = mount(
defineComponent({
components: { child },
setup() {
return useForm()
},
render() {
return h(child)
},
})
)
expect(vm.errors).toHaveLength(0)
vm.validate()
expect(vm.errors).toHaveLength(2)
expect(vm.errors).toEqual(
expect.arrayContaining([
{ message: 'Required', value: '', identifier: 'i1' },
{ message: 'Required', value: '', identifier: 'i2' },
])
)
vm.reset()
expect(vm.errors).toHaveLength(0)
expect(vm.isValid).toBeTruthy()
input1.value = 'hello'
vm.validate()
expect(vm.errors).toHaveLength(1)
})
it('validates forms asynchronously', async () => {
const input1 = ref('')
const input2 = ref('')
const isRequired = (v: string) =>
new Promise((resolve) =>
setTimeout(() => resolve(!!v || 'Required'), 100)
)
const child = defineComponent({
setup() {
const field1 = useField({
value: input1,
rules: [isRequired],
identifier: 'i1',
})
const field2 = useField({
value: input2,
rules: [isRequired],
identifier: 'i2',
})
return { field1, field2 }
},
render() {},
})
const vm = mount(
defineComponent({
components: { child },
setup() {
return useForm()
},
render() {
return h(child)
},
})
)
expect(vm.errors).toHaveLength(0)
const promise = vm.validateAsync()
expect(vm.errors).toHaveLength(0)
await promise
expect(vm.errors).toHaveLength(2)
expect(vm.errors).toEqual(
expect.arrayContaining([
{ message: 'Required', value: '', identifier: 'i1' },
{ message: 'Required', value: '', identifier: 'i2' },
])
)
vm.reset()
expect(vm.errors).toHaveLength(0)
expect(vm.isValid).toBeTruthy()
input1.value = 'hello'
await vm.validateAsync()
expect(vm.errors).toHaveLength(1)
})
})