forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrigger.spec.js
341 lines (298 loc) · 9.72 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import ComponentWithEvents from '~resources/components/component-with-events.vue'
import ComponentWithScopedSlots from '~resources/components/component-with-scoped-slots.vue'
import {
describeWithShallowAndMount,
scopedSlotsSupported,
isRunningChrome,
isPromise,
vueVersion
} from '~resources/utils'
import Vue from 'vue'
import { itDoNotRunIf } from 'conditional-specs'
describeWithShallowAndMount('trigger', mountingMethod => {
it('returns a promise that when resolved, the component is updated', async () => {
const wrapper = mountingMethod(ComponentWithEvents)
const toggle = wrapper.find('.toggle')
expect(toggle.classes()).not.toContain('active')
const response = toggle.trigger('click')
expect(toggle.classes()).not.toContain('active')
expect(isPromise(response)).toEqual(true)
await response
expect(toggle.classes()).toContain('active')
})
it('causes click handler to fire when wrapper.trigger("click") is called on a Component', async () => {
const clickHandler = jest.fn()
const wrapper = mountingMethod(ComponentWithEvents, {
propsData: { clickHandler }
})
const button = wrapper.find('.click')
await button.trigger('click')
expect(clickHandler).toHaveBeenCalled()
})
it('causes keydown handler to fire when wrapper.trigger("keydown") is fired on a Component', async () => {
const keydownHandler = jest.fn()
const wrapper = mountingMethod(ComponentWithEvents, {
propsData: { keydownHandler }
})
await wrapper.find('.keydown').trigger('keydown')
expect(keydownHandler).toHaveBeenCalled()
})
describe('causes keydown handler to fire with the appropriate keyCode when wrapper.trigger("keydown", { keyCode: 46 }) is fired on a Component', async () => {
const keydownHandler = jest.fn()
const wrapper = mountingMethod(ComponentWithEvents, {
propsData: { keydownHandler }
})
await wrapper.find('.keydown').trigger('keydown', { keyCode: 46 })
const keyboardEvent = keydownHandler.mock.calls[0][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)).toEqual(46)
})
it('contains the key', () => {
expect(keyboardEvent.key).toEqual('Delete')
})
itDoNotRunIf(isRunningChrome, 'contains the code', () => {
expect(parseInt(keyboardEvent.code, 10)).toEqual(46)
})
})
describe('causes keydown handler to fire with the appropriate key when wrapper.trigger("keydown", { key: "k" }) is fired on a Component', async () => {
const keydownHandler = jest.fn()
const wrapper = mountingMethod(ComponentWithEvents, {
propsData: { keydownHandler }
})
await wrapper.find('.keydown').trigger('keydown', { key: 'k' })
const keyboardEvent = keydownHandler.mock.calls[0][0]
it('contains the key', () => {
expect(keyboardEvent.key).toEqual('k')
})
})
it('causes keydown handler to fire when wrapper.trigger("keydown.enter") is fired on a Component', async () => {
const keydownHandler = jest.fn()
const wrapper = mountingMethod(ComponentWithEvents, {
propsData: { keydownHandler }
})
await wrapper.find('.keydown-enter').trigger('keydown.enter')
expect(keydownHandler).toHaveBeenCalled()
})
it('convert a registered key name to a key code and key', async () => {
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
}
// get from https://github.com/ashubham/w3c-keys/blob/master/index.ts
const w3cKeys = {
enter: 'Enter',
tab: 'Tab',
delete: 'Delete',
esc: 'Esc',
escape: 'Escape',
space: ' ',
up: 'ArrowUp',
left: 'ArrowLeft',
right: 'ArrowRight',
down: 'ArrowDown',
end: 'End',
home: 'Home',
backspace: 'Backspace',
insert: 'Insert',
pageup: 'PageUp',
pagedown: 'PageDown'
}
const codeToKeyNameMap = Object.entries(modifiers).reduce(
(acc, [key, value]) => Object.assign(acc, { [value]: w3cKeys[key] }),
{}
)
const modifiersArray = Object.entries(modifiers)
const keyupHandler = jest.fn()
const wrapper = mountingMethod(ComponentWithEvents, {
propsData: { keyupHandler }
})
for (let index = 0; index < modifiersArray.length; index++) {
const [keyName, keyCode] = modifiersArray[index]
await wrapper.find('.keydown').trigger(`keyup.${keyName}`)
expect(keyupHandler.mock.calls[index][0].keyCode).toEqual(keyCode)
expect(keyupHandler.mock.calls[index][0].key).toEqual(
codeToKeyNameMap[keyCode]
)
}
})
itDoNotRunIf(
vueVersion < 2.2,
'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.toContain('active')
await toggle.trigger('click')
expect(toggle.classes()).toContain('active')
}
)
it('adds options to event', async () => {
const clickHandler = jest.fn()
const wrapper = mountingMethod(ComponentWithEvents, {
propsData: { clickHandler }
})
const button = wrapper.find('.left-click')
await button.trigger('mousedown', {
button: 1
})
await button.trigger('mousedown', {
button: 0
})
expect(clickHandler).toHaveBeenCalled()
})
it('adds custom data to events', async () => {
const stub = jest.fn()
const TestComponent = {
template: '<div @update="callStub" />',
methods: {
callStub(event) {
stub(event.customData)
}
}
}
const wrapper = mountingMethod(TestComponent)
await wrapper.trigger('update', {
customData: 123
})
expect(stub).toHaveBeenCalledWith(123)
})
it('does not fire on valid disabled elements', async () => {
const clickHandler = jest.fn()
const ButtonComponent = {
template: '<button disabled @click="clickHandler">Button</button>',
props: ['clickHandler']
}
const buttonWrapper = mountingMethod(ButtonComponent, {
propsData: {
clickHandler
}
})
await buttonWrapper.trigger('click')
expect(clickHandler).not.toHaveBeenCalled()
const changeHandler = jest.fn()
const InputComponent = {
template: '<input disabled @change="changeHandler"/>',
props: ['changeHandler']
}
const inputWrapper = mountingMethod(InputComponent, {
propsData: {
changeHandler
}
})
await inputWrapper.trigger('change')
expect(changeHandler).not.toHaveBeenCalled()
})
it('fires on invalid disabled elements', async () => {
const clickHandler = jest.fn()
const LinkComponent = {
template: '<a disabled href="#" @click="clickHandler">Link</a>',
props: ['clickHandler']
}
const linkWrapper = mountingMethod(LinkComponent, {
propsData: {
clickHandler
}
})
await linkWrapper.trigger('click')
expect(clickHandler).toHaveBeenCalled()
})
it('handles .prevent', async () => {
const TestComponent = {
template: '<input @keydown.enter.prevent="enter">'
}
const wrapper = mountingMethod(TestComponent)
await wrapper.trigger('keydown')
})
itDoNotRunIf(
!scopedSlotsSupported || mountingMethod.name === 'shallowMount',
'handles instances without update watchers',
async () => {
const vm = new Vue()
const item = () => vm.$createElement('button')
const TestComponent = {
render(h) {
return h(ComponentWithScopedSlots, {
scopedSlots: {
noProps: item
}
})
}
}
const wrapper = mountingMethod(TestComponent)
await 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).toThrow(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).toThrow(message)
})
})
itDoNotRunIf(
isRunningChrome,
'trigger should create events with correct interface',
async () => {
let lastEvent
const TestComponent = {
template: `
<div @click="updateLastEvent" />
`,
methods: {
updateLastEvent(event) {
lastEvent = event
}
}
}
const wrapper = mountingMethod(TestComponent)
await wrapper.trigger('click')
expect(lastEvent).toBeInstanceOf(window.MouseEvent)
}
)
it('falls back to supported event if not supported by browser', async () => {
const TestComponent = {
template: '<div />'
}
const wrapper = mountingMethod(TestComponent)
await wrapper.trigger('gamepadconnected')
})
})