forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetSelected.spec.js
73 lines (56 loc) · 2.73 KB
/
setSelected.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
import ComponentWithInput from '~resources/components/component-with-input.vue'
import { describeWithShallowAndMount } from '~resources/utils'
describeWithShallowAndMount('setSelected', (mountingMethod) => {
it('sets element selected true', () => {
const wrapper = mountingMethod(ComponentWithInput)
const options = wrapper.find('select').findAll('option')
options.at(1).setSelected()
expect(options.at(1).element.selected).to.equal(true)
})
it('updates dom with select v-model', () => {
const wrapper = mountingMethod(ComponentWithInput)
const options = wrapper.find('select').findAll('option')
options.at(1).setSelected()
expect(wrapper.text()).to.contain('selectB')
options.at(0).setSelected()
expect(wrapper.text()).to.contain('selectA')
})
it('updates dom with select v-model for select with optgroups', () => {
const wrapper = mountingMethod(ComponentWithInput)
const options = wrapper.find('select.with-optgroups').findAll('option')
options.at(1).setSelected()
expect(wrapper.text()).to.contain('selectB')
options.at(0).setSelected()
expect(wrapper.text()).to.contain('selectA')
})
it('throws error if wrapper does not contain element', () => {
const wrapper = mountingMethod({ render: (h) => h('div') })
const div = wrapper.find('div')
div.element = null
const fn = () => div.setSelected()
const message = '[vue-test-utils]: cannot call wrapper.setSelected() on a wrapper without an element'
expect(fn).to.throw().with.property('message', message)
})
it('throws error if element is radio', () => {
const message = 'wrapper.setSelected() cannot be called on a <input type="radio" /> element. Use wrapper.setChecked() instead'
shouldThrowErrorOnElement('input[type="radio"]', message)
})
it('throws error if element is radio', () => {
const message = 'wrapper.setSelected() cannot be called on a <input type="checkbox" /> element. Use wrapper.setChecked() instead'
shouldThrowErrorOnElement('input[type="checkbox"]', message)
})
it('throws error if element is text like', () => {
const message = 'wrapper.setSelected() cannot be called on "text" inputs. Use wrapper.setValue() instead'
shouldThrowErrorOnElement('input[type="text"]', message)
})
it('throws error if element is not valid', () => {
const message = 'wrapper.setSelected() cannot be called on this element'
shouldThrowErrorOnElement('#label-el', message)
})
function shouldThrowErrorOnElement (selector, message, value) {
const wrapper = mountingMethod(ComponentWithInput)
const input = wrapper.find(selector)
const fn = () => input.setSelected(value)
expect(fn).to.throw().with.property('message', '[vue-test-utils]: ' + message)
}
})