forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrigger.spec.js
174 lines (155 loc) · 5.36 KB
/
trigger.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import ComponentWithEvents from '~resources/components/component-with-events.vue'
import ComponentWithScopedSlots from '~resources/components/component-with-scoped-slots.vue'
import {
describeWithShallowAndMount,
scopedSlotsSupported
} from '~resources/utils'
import Vue from 'vue'
import { itDoNotRunIf } from 'conditional-specs'
describeWithShallowAndMount('trigger', (mountingMethod) => {
let info
beforeEach(() => {
info = sinon.stub(console, 'info')
})
afterEach(() => {
info.restore()
})
it('causes click handler to fire when wrapper.trigger("click") is called on a Component', () => {
const clickHandler = sinon.stub()
const wrapper = mountingMethod(ComponentWithEvents, {
propsData: { clickHandler }
})
const button = wrapper.find('.click')
button.trigger('click')
expect(clickHandler.calledOnce).to.equal(true)
})
it('causes keydown handler to fire when wrapper.trigger("keydown") is fired on a Component', () => {
const keydownHandler = sinon.stub()
const wrapper = mountingMethod(ComponentWithEvents, {
propsData: { keydownHandler }
})
wrapper.find('.keydown').trigger('keydown')
expect(keydownHandler.calledOnce).to.equal(true)
})
it('causes keydown handler to fire when wrapper.trigger("keydown.enter") is fired on a Component', () => {
const keydownHandler = sinon.stub()
const wrapper = mountingMethod(ComponentWithEvents, {
propsData: { keydownHandler }
})
wrapper.find('.keydown-enter').trigger('keydown.enter')
expect(keydownHandler.calledOnce).to.equal(true)
})
it('convert a registered key name to a key code', () => {
const modifiers = {
enter: 13,
esc: 27,
tab: 9,
space: 32,
delete: 46,
backspace: 8,
insert: 45,
up: 38,
down: 40,
left: 37,
right: 39,
end: 35,
home: 36,
pageup: 33,
pagedown: 34
}
const keyupHandler = sinon.stub()
const wrapper = mountingMethod(ComponentWithEvents, {
propsData: { keyupHandler }
})
for (const keyName in modifiers) {
const keyCode = modifiers[keyName]
wrapper.find('.keydown').trigger(`keyup.${keyName}`)
expect(keyupHandler.lastCall.args[0].keyCode).to.equal(keyCode)
}
})
it('causes DOM to update after clickHandler method that changes components data is called', () => {
const wrapper = mountingMethod(ComponentWithEvents)
const toggle = wrapper.find('.toggle')
expect(toggle.hasClass('active')).to.equal(false)
toggle.trigger('click')
expect(toggle.hasClass('active')).to.equal(true)
})
it('adds options to event', () => {
const clickHandler = sinon.stub()
const wrapper = mountingMethod(ComponentWithEvents, {
propsData: { clickHandler }
})
const button = wrapper.find('.left-click')
button.trigger('mousedown')
button.trigger('mousedown', {
button: 0
})
expect(clickHandler.calledOnce).to.equal(true)
})
it('does not fire on disabled elements', () => {
const clickHandler = sinon.stub()
const TestComponent = {
template: '<button disabled @click="clickHandler"/>',
props: ['clickHandler']
}
const wrapper = mountingMethod(TestComponent, {
propsData: {
clickHandler
}
})
wrapper.trigger('click')
expect(clickHandler.called).to.equal(false)
})
it('handles .prevent', () => {
const TestComponent = {
template: '<input @keydown.enter.prevent="enter">'
}
const wrapper = mountingMethod(TestComponent)
wrapper.trigger('keydown')
})
itDoNotRunIf(
!scopedSlotsSupported,
'handles instances without update watchers', () => {
const vm = new Vue()
const item = () => vm.$createElement('button')
const TestComponent = {
render (h) {
return h(ComponentWithScopedSlots, {
scopedSlots: {
noProps: item
}
})
}
}
const wrapper = mountingMethod(TestComponent)
wrapper.findAll('button').trigger('click')
})
it('throws error if options contains a target value', () => {
const wrapper = mountingMethod({ render: (h) => h('div') })
const div = wrapper.find('div')
const fn = () => div.trigger('click', {
target: {}
})
const message = '[vue-test-utils]: you cannot set the target value of an event. See the notes section of the docs for more details—https://vue-test-utils.vuejs.org/api/wrapper/trigger.html'
expect(fn).to.throw().with.property('message', message)
})
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.trigger('click')
const message = '[vue-test-utils]: cannot call wrapper.trigger() on a wrapper without an element'
expect(fn).to.throw().with.property('message', message)
})
it('throws an error if type is not a string', () => {
const wrapper = mountingMethod(ComponentWithEvents)
const invalidSelectors = [
undefined, null, NaN, 0, 2, true, false, () => {}, {}, []
]
invalidSelectors.forEach((invalidSelector) => {
const message = '[vue-test-utils]: wrapper.trigger() must be passed a string'
const fn = () => wrapper.trigger(invalidSelector)
expect(fn).to.throw().with.property('message', message)
})
})
})