-
Notifications
You must be signed in to change notification settings - Fork 668
/
Copy pathsetValue.spec.js
131 lines (107 loc) · 4.42 KB
/
setValue.spec.js
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
import ComponentWithInput from '~resources/components/component-with-input.vue'
import { describeWithShallowAndMount, isPromise } from '~resources/utils'
import { itDoNotRunIf } from 'conditional-specs'
import { vueVersion } from '~resources/utils'
describeWithShallowAndMount('setValue', mountingMethod => {
it('returns a promise', async () => {
const wrapper = mountingMethod(ComponentWithInput)
const input = wrapper.find('input[type="text"]')
const response = input.setValue('foo')
expect(isPromise(response)).to.eql(true)
expect(wrapper.text()).not.to.contain('foo')
await response
expect(wrapper.text()).to.contain('foo')
})
it('sets element of input value', () => {
const wrapper = mountingMethod(ComponentWithInput)
const input = wrapper.find('input[type="text"]')
input.setValue('foo')
expect(input.element.value).to.equal('foo')
})
it('sets element of textarea value', () => {
const wrapper = mountingMethod(ComponentWithInput)
const textarea = wrapper.find('textarea')
textarea.setValue('foo')
expect(textarea.element.value).to.equal('foo')
})
it('updates dom with input v-model', async () => {
const wrapper = mountingMethod(ComponentWithInput)
const input = wrapper.find('input[type="text"]')
await input.setValue('input text awesome binding')
expect(wrapper.text()).to.contain('input text awesome binding')
})
itDoNotRunIf(
vueVersion < 2.1,
'updates dom with input v-model.lazy',
async () => {
const wrapper = mountingMethod(ComponentWithInput)
const input = wrapper.find('input#lazy')
await input.setValue('lazy')
expect(wrapper.text()).to.contain('lazy')
}
)
it('sets element of select value', () => {
const wrapper = mountingMethod(ComponentWithInput)
const select = wrapper.find('select')
select.setValue('selectB')
expect(select.element.value).to.equal('selectB')
})
it('updates dom with select v-model', async () => {
const wrapper = mountingMethod(ComponentWithInput)
const select = wrapper.find('select')
await select.setValue('selectB')
expect(wrapper.text()).to.contain('selectB')
})
it('sets element of multiselect value when array', () => {
const wrapper = mountingMethod(ComponentWithInput)
const select = wrapper.find('select.multiselect')
select.setValue(['selectA', 'selectC'])
const selectedOptions = Array.from(select.element.selectedOptions).map(
o => o.value
)
expect(selectedOptions).to.deep.equal(['selectA', 'selectC'])
})
it('sets element of multiselect value when array overrides', () => {
const wrapper = mountingMethod(ComponentWithInput)
const select = wrapper.find('select.multiselect')
select.setValue(['selectA', 'selectC'])
select.setValue(['selectB'])
const selectedOptions = Array.from(select.element.selectedOptions).map(
o => o.value
)
expect(selectedOptions).to.deep.equal(['selectB'])
})
it('updates dom with multiselect v-model when array', async () => {
const wrapper = mountingMethod(ComponentWithInput)
const select = wrapper.find('select.multiselect')
await select.setValue(['selectA', 'selectC'])
expect(wrapper.text()).to.contain('["selectA","selectC"]')
})
it('throws error if element is option', () => {
const message =
'wrapper.setValue() cannot be called on an <option> element. Use wrapper.setSelected() instead'
shouldThrowErrorOnElement('option', message)
})
it('throws error if element is radio', () => {
const message =
'wrapper.setValue() cannot be called on a <input type="radio" /> element. Use wrapper.setChecked() instead'
shouldThrowErrorOnElement('input[type="radio"]', message)
})
it('throws error if element is checkbox', () => {
const message =
'wrapper.setValue() cannot be called on a <input type="checkbox" /> element. Use wrapper.setChecked() instead'
shouldThrowErrorOnElement('input[type="checkbox"]', message)
})
it('throws error if element is not valid', () => {
const message = 'wrapper.setValue() cannot be called on this element'
shouldThrowErrorOnElement('#label-el', message)
})
function shouldThrowErrorOnElement(selector, message) {
const wrapper = mountingMethod(ComponentWithInput)
const input = wrapper.find(selector)
const fn = () => input.setValue('')
expect(fn)
.to.throw()
.with.property('message', '[vue-test-utils]: ' + message)
}
})