-
Notifications
You must be signed in to change notification settings - Fork 668
/
Copy pathtrigger.spec.js
284 lines (251 loc) · 7.92 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import ComponentWithEvents from '~resources/components/component-with-events.vue'
import ComponentWithScopedSlots from '~resources/components/component-with-scoped-slots.vue'
import {
describeWithShallowAndMount,
scopedSlotsSupported,
isRunningPhantomJS
} from '~resources/utils'
import Vue from 'vue'
import { itDoNotRunIf } from 'conditional-specs'
describeWithShallowAndMount('trigger', mountingMethod => {
const sandbox = sinon.createSandbox()
afterEach(() => {
sandbox.reset()
sandbox.restore()
})
it('causes click handler to fire when wrapper.trigger("click") is called on a Component', () => {
const clickHandler = sandbox.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 = sandbox.stub()
const wrapper = mountingMethod(ComponentWithEvents, {
propsData: { keydownHandler }
})
wrapper.find('.keydown').trigger('keydown')
expect(keydownHandler.calledOnce).to.equal(true)
})
describe('causes keydown handler to fire with the appropriate keyCode when wrapper.trigger("keydown", { keyCode: 65 }) is fired on a Component', () => {
const keydownHandler = sandbox.stub()
const wrapper = mountingMethod(ComponentWithEvents, {
propsData: { keydownHandler }
})
wrapper.find('.keydown').trigger('keydown', { keyCode: 65 })
const keyboardEvent = keydownHandler.getCall(0).args[0]
// Unfortunately, JSDom will give different types than PhantomJS for keyCodes (string vs number), so we have to use parseInt to normalize the types.
it('contains the keyCode', () => {
expect(parseInt(keyboardEvent.keyCode, 10)).to.equal(65)
})
itDoNotRunIf(isRunningPhantomJS, 'contains the code', () => {
expect(parseInt(keyboardEvent.code, 10)).to.equal(65)
})
})
it('causes keydown handler to fire when wrapper.trigger("keydown.enter") is fired on a Component', () => {
const keydownHandler = sandbox.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 = sandbox.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', async () => {
const wrapper = mountingMethod(ComponentWithEvents)
const toggle = wrapper.find('.toggle')
expect(toggle.classes()).not.to.contain('active')
toggle.trigger('click')
await Vue.nextTick()
expect(toggle.classes()).to.contain('active')
})
it('adds options to event', () => {
const clickHandler = sandbox.stub()
const wrapper = mountingMethod(ComponentWithEvents, {
propsData: { clickHandler }
})
const button = wrapper.find('.left-click')
button.trigger('mousedown', {
button: 1
})
button.trigger('mousedown', {
button: 0
})
expect(clickHandler.calledOnce).to.equal(true)
})
it('adds custom data to events', () => {
const stub = sandbox.stub()
const TestComponent = {
template: '<div @update="callStub" />',
methods: {
callStub(event) {
stub(event.customData)
}
}
}
const wrapper = mountingMethod(TestComponent)
wrapper.trigger('update', {
customData: 123
})
expect(stub).calledWith(123)
})
it('does not fire on valid disabled elements', () => {
const clickHandler = sandbox.stub()
const ButtonComponent = {
template: '<button disabled @click="clickHandler">Button</button>',
props: ['clickHandler']
}
const buttonWrapper = mountingMethod(ButtonComponent, {
propsData: {
clickHandler
}
})
buttonWrapper.trigger('click')
expect(clickHandler.called).to.equal(false)
const changeHandler = sandbox.stub()
const InputComponent = {
template: '<input disabled @change="changeHandler"/>',
props: ['changeHandler']
}
const inputWrapper = mountingMethod(InputComponent, {
propsData: {
changeHandler
}
})
inputWrapper.trigger('change')
expect(changeHandler.called).to.equal(false)
})
it('fires on invalid disabled elements', () => {
const clickHandler = sandbox.stub()
const LinkComponent = {
template: '<a disabled href="#" @click="clickHandler">Link</a>',
props: ['clickHandler']
}
const linkWrapper = mountingMethod(LinkComponent, {
propsData: {
clickHandler
}
})
linkWrapper.trigger('click')
expect(clickHandler.called).to.equal(true)
})
it('handles .prevent', () => {
const TestComponent = {
template: '<input @keydown.enter.prevent="enter">'
}
const wrapper = mountingMethod(TestComponent)
wrapper.trigger('keydown')
})
itDoNotRunIf(
!scopedSlotsSupported || mountingMethod.name === 'shallowMount',
'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 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)
})
})
itDoNotRunIf(
isRunningPhantomJS,
'trigger should create events with correct interface',
() => {
let lastEvent
const TestComponent = {
template: `
<div @click="updateLastEvent" />
`,
methods: {
updateLastEvent(event) {
lastEvent = event
}
}
}
const wrapper = mountingMethod(TestComponent)
wrapper.trigger('click')
expect(lastEvent).to.be.an.instanceof(window.MouseEvent)
}
)
it('falls back to supported event if not supported by browser', () => {
const TestComponent = {
template: '<div />'
}
const wrapper = mountingMethod(TestComponent)
wrapper.trigger('gamepadconnected')
})
})