/
useFieldValue.spec.ts
121 lines (106 loc) · 2.99 KB
/
useFieldValue.spec.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
import flushPromises from 'flush-promises';
import { useField, useFieldValue, useForm } from '@/vee-validate';
import { mountWithHoc, setValue } from './helpers';
import { defineComponent } from 'vue';
describe('useFieldValue()', () => {
const REQUIRED_MESSAGE = 'Field is required';
const validate = (val: any) => (val ? true : REQUIRED_MESSAGE);
test('gives access to a single field value', async () => {
mountWithHoc({
setup() {
useForm();
const { value } = useField('test', validate);
const { value: currValue, setValue } = useFieldValue('test');
return {
value,
currValue,
setValue,
};
},
template: `
<input name="field" v-model="value" />
<span>{{ currValue }}</span>
<button @click="setValue('5')"></button>
`,
});
await flushPromises();
const input = document.querySelector('input');
const valueSpan = document.querySelector('span');
const inputValue = '1234';
setValue(input as any, inputValue);
await flushPromises();
expect(valueSpan?.textContent).toBe(inputValue);
// test value setting
const btn = document.querySelector('button');
btn?.click();
await flushPromises();
expect(input?.value).toBe('5');
});
test('gives access to a single field value in a child component without specifying a path', async () => {
const CustomErrorComponent = defineComponent({
template: '<span>{{ value }}</span>',
setup() {
const { value } = useFieldValue();
return {
value,
};
},
});
mountWithHoc({
components: {
CustomErrorComponent,
},
setup() {
useForm();
const { value } = useField('test', validate);
return {
value,
};
},
template: `
<input name="field" v-model="value" />
<CustomErrorComponent />
`,
});
await flushPromises();
const input = document.querySelector('input');
const valueSpan = document.querySelector('span');
const inputValue = '1234';
setValue(input as any, inputValue);
await flushPromises();
expect(valueSpan?.textContent).toBe(inputValue);
});
test('returns undefined if field not found', async () => {
mountWithHoc({
setup() {
useForm();
const { value } = useFieldValue('something');
return {
value,
};
},
template: `
<span>{{ value }}</span>
`,
});
await flushPromises();
const error = document.querySelector('span');
expect(error?.textContent).toBe('');
});
test('returns undefined if form is not found', async () => {
mountWithHoc({
setup() {
const { value } = useFieldValue('something');
return {
value,
};
},
template: `
<span>{{ value }}</span>
`,
});
await flushPromises();
const error = document.querySelector('span');
expect(error?.textContent).toBe('');
});
});