-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
/
on.spec.js
443 lines (414 loc) · 11.6 KB
/
on.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import Vue from 'vue'
describe('Directive v-on', () => {
let vm, spy, spy2, el
beforeEach(() => {
spy = jasmine.createSpy()
spy2 = jasmine.createSpy()
el = document.createElement('div')
document.body.appendChild(el)
})
afterEach(() => {
document.body.removeChild(vm.$el)
})
it('should bind event to a method', () => {
vm = new Vue({
el,
template: '<div v-on:click="foo"></div>',
methods: { foo: spy }
})
triggerEvent(vm.$el, 'click')
expect(spy.calls.count()).toBe(1)
const args = spy.calls.allArgs()
const event = args[0] && args[0][0] || {}
expect(event.type).toBe('click')
})
it('should bind event to a inline statement', () => {
vm = new Vue({
el,
template: '<div v-on:click="foo(1,2,3,$event)"></div>',
methods: { foo: spy }
})
triggerEvent(vm.$el, 'click')
expect(spy.calls.count()).toBe(1)
const args = spy.calls.allArgs()
const firstArgs = args[0]
expect(firstArgs.length).toBe(4)
expect(firstArgs[0]).toBe(1)
expect(firstArgs[1]).toBe(2)
expect(firstArgs[2]).toBe(3)
expect(firstArgs[3].type).toBe('click')
})
it('should support inline function expression', () => {
const spy = jasmine.createSpy()
vm = new Vue({
el,
template: `<div class="test" @click="function (e) { log(e.target.className) }"></div>`,
methods: {
log: spy
}
}).$mount()
triggerEvent(vm.$el, 'click')
expect(spy).toHaveBeenCalledWith('test')
})
it('should support shorthand', () => {
vm = new Vue({
el,
template: '<a href="#test" @click.prevent="foo"></a>',
methods: { foo: spy }
})
triggerEvent(vm.$el, 'click')
expect(spy.calls.count()).toBe(1)
})
it('should support stop propagation', () => {
vm = new Vue({
el,
template: `
<div @click.stop="foo"></div>
`,
methods: { foo: spy }
})
const hash = window.location.hash
triggerEvent(vm.$el, 'click')
expect(window.location.hash).toBe(hash)
})
it('should support prevent default', () => {
vm = new Vue({
el,
template: `
<div @click="bar">
<div @click.stop="foo"></div>
</div>
`,
methods: { foo: spy, bar: spy2 }
})
triggerEvent(vm.$el.firstChild, 'click')
expect(spy).toHaveBeenCalled()
expect(spy2).not.toHaveBeenCalled()
})
it('should support capture', () => {
const callOrder = []
vm = new Vue({
el,
template: `
<div @click.capture="foo">
<div @click="bar"></div>
</div>
`,
methods: {
foo () { callOrder.push(1) },
bar () { callOrder.push(2) }
}
})
triggerEvent(vm.$el.firstChild, 'click')
expect(callOrder.toString()).toBe('1,2')
})
it('should support once', () => {
vm = new Vue({
el,
template: `
<div @click.once="foo">
</div>
`,
methods: { foo: spy }
})
triggerEvent(vm.$el, 'click')
expect(spy.calls.count()).toBe(1)
triggerEvent(vm.$el, 'click')
expect(spy.calls.count()).toBe(1) // should no longer trigger
})
// #4655
it('should handle .once on multiple elements properly', () => {
vm = new Vue({
el,
template: `
<div>
<button ref="one" @click.once="foo">one</button>
<button ref="two" @click.once="foo">two</button>
</div>
`,
methods: { foo: spy }
})
triggerEvent(vm.$refs.one, 'click')
expect(spy.calls.count()).toBe(1)
triggerEvent(vm.$refs.one, 'click')
expect(spy.calls.count()).toBe(1)
triggerEvent(vm.$refs.two, 'click')
expect(spy.calls.count()).toBe(2)
triggerEvent(vm.$refs.one, 'click')
triggerEvent(vm.$refs.two, 'click')
expect(spy.calls.count()).toBe(2)
})
it('should support capture and once', () => {
const callOrder = []
vm = new Vue({
el,
template: `
<div @click.capture.once="foo">
<div @click="bar"></div>
</div>
`,
methods: {
foo () { callOrder.push(1) },
bar () { callOrder.push(2) }
}
})
triggerEvent(vm.$el.firstChild, 'click')
expect(callOrder.toString()).toBe('1,2')
triggerEvent(vm.$el.firstChild, 'click')
expect(callOrder.toString()).toBe('1,2,2')
})
// #4846
it('should support once and other modifiers', () => {
vm = new Vue({
el,
template: `<div @click.once.self="foo"><span/></div>`,
methods: { foo: spy }
})
triggerEvent(vm.$el.firstChild, 'click')
expect(spy).not.toHaveBeenCalled()
triggerEvent(vm.$el, 'click')
expect(spy).toHaveBeenCalled()
triggerEvent(vm.$el, 'click')
expect(spy.calls.count()).toBe(1)
})
it('should support keyCode', () => {
vm = new Vue({
el,
template: `<input @keyup.enter="foo">`,
methods: { foo: spy }
})
triggerEvent(vm.$el, 'keyup', e => {
e.keyCode = 13
})
expect(spy).toHaveBeenCalled()
})
it('should support number keyCode', () => {
vm = new Vue({
el,
template: `<input @keyup.13="foo">`,
methods: { foo: spy }
})
triggerEvent(vm.$el, 'keyup', e => {
e.keyCode = 13
})
expect(spy).toHaveBeenCalled()
})
it('should support custom keyCode', () => {
Vue.config.keyCodes.test = 1
vm = new Vue({
el,
template: `<input @keyup.test="foo">`,
methods: { foo: spy }
})
triggerEvent(vm.$el, 'keyup', e => {
e.keyCode = 1
})
expect(spy).toHaveBeenCalled()
})
it('should override build-in keyCode', () => {
Vue.config.keyCodes.up = [1, 87]
vm = new Vue({
el,
template: `<input @keyup.up="foo" @keyup.down="foo">`,
methods: { foo: spy }
})
triggerEvent(vm.$el, 'keyup', e => {
e.keyCode = 87
})
expect(spy).toHaveBeenCalled()
triggerEvent(vm.$el, 'keyup', e => {
e.keyCode = 1
})
expect(spy).toHaveBeenCalledTimes(2)
// should not affect build-in down keycode
triggerEvent(vm.$el, 'keyup', e => {
e.keyCode = 40
})
expect(spy).toHaveBeenCalledTimes(3)
})
it('should bind to a child component', () => {
Vue.component('bar', {
template: '<span>Hello</span>'
})
vm = new Vue({
el,
template: '<bar @custom="foo"></bar>',
methods: { foo: spy }
})
vm.$children[0].$emit('custom', 'foo', 'bar')
expect(spy).toHaveBeenCalledWith('foo', 'bar')
})
it('should be able to bind native events for a child component', () => {
Vue.component('bar', {
template: '<span>Hello</span>'
})
vm = new Vue({
el,
template: '<bar @click.native="foo"></bar>',
methods: { foo: spy }
})
vm.$children[0].$emit('click')
expect(spy).not.toHaveBeenCalled()
triggerEvent(vm.$children[0].$el, 'click')
expect(spy).toHaveBeenCalled()
})
it('.once modifier should work with child components', () => {
Vue.component('bar', {
template: '<span>Hello</span>'
})
vm = new Vue({
el,
template: '<bar @custom.once="foo"></bar>',
methods: { foo: spy }
})
vm.$children[0].$emit('custom')
expect(spy.calls.count()).toBe(1)
vm.$children[0].$emit('custom')
expect(spy.calls.count()).toBe(1) // should not be called again
})
it('remove listener', done => {
const spy2 = jasmine.createSpy('remove listener')
vm = new Vue({
el,
methods: { foo: spy, bar: spy2 },
data: {
ok: true
},
render (h) {
return this.ok
? h('input', { on: { click: this.foo }})
: h('input', { on: { input: this.bar }})
}
})
triggerEvent(vm.$el, 'click')
expect(spy.calls.count()).toBe(1)
expect(spy2.calls.count()).toBe(0)
vm.ok = false
waitForUpdate(() => {
triggerEvent(vm.$el, 'click')
expect(spy.calls.count()).toBe(1) // should no longer trigger
triggerEvent(vm.$el, 'input')
expect(spy2.calls.count()).toBe(1)
}).then(done)
})
it('remove capturing listener', done => {
const spy2 = jasmine.createSpy('remove listener')
vm = new Vue({
el,
methods: { foo: spy, bar: spy2, stopped (ev) { ev.stopPropagation() } },
data: {
ok: true
},
render (h) {
return this.ok
? h('div', { on: { '!click': this.foo }}, [h('div', { on: { click: this.stopped }})])
: h('div', { on: { mouseOver: this.bar }}, [h('div')])
}
})
triggerEvent(vm.$el.firstChild, 'click')
expect(spy.calls.count()).toBe(1)
expect(spy2.calls.count()).toBe(0)
vm.ok = false
waitForUpdate(() => {
triggerEvent(vm.$el.firstChild, 'click')
expect(spy.calls.count()).toBe(1) // should no longer trigger
triggerEvent(vm.$el, 'mouseOver')
expect(spy2.calls.count()).toBe(1)
}).then(done)
})
it('remove once listener', done => {
const spy2 = jasmine.createSpy('remove listener')
vm = new Vue({
el,
methods: { foo: spy, bar: spy2 },
data: {
ok: true
},
render (h) {
return this.ok
? h('input', { on: { '~click': this.foo }})
: h('input', { on: { input: this.bar }})
}
})
triggerEvent(vm.$el, 'click')
expect(spy.calls.count()).toBe(1)
triggerEvent(vm.$el, 'click')
expect(spy.calls.count()).toBe(1) // should no longer trigger
expect(spy2.calls.count()).toBe(0)
vm.ok = false
waitForUpdate(() => {
triggerEvent(vm.$el, 'click')
expect(spy.calls.count()).toBe(1) // should no longer trigger
triggerEvent(vm.$el, 'input')
expect(spy2.calls.count()).toBe(1)
}).then(done)
})
it('remove capturing and once listener', done => {
const spy2 = jasmine.createSpy('remove listener')
vm = new Vue({
el,
methods: { foo: spy, bar: spy2, stopped (ev) { ev.stopPropagation() } },
data: {
ok: true
},
render (h) {
return this.ok
? h('div', { on: { '~!click': this.foo }}, [h('div', { on: { click: this.stopped }})])
: h('div', { on: { mouseOver: this.bar }}, [h('div')])
}
})
triggerEvent(vm.$el.firstChild, 'click')
expect(spy.calls.count()).toBe(1)
triggerEvent(vm.$el.firstChild, 'click')
expect(spy.calls.count()).toBe(1) // should no longer trigger
expect(spy2.calls.count()).toBe(0)
vm.ok = false
waitForUpdate(() => {
triggerEvent(vm.$el.firstChild, 'click')
expect(spy.calls.count()).toBe(1) // should no longer trigger
triggerEvent(vm.$el, 'mouseOver')
expect(spy2.calls.count()).toBe(1)
}).then(done)
})
it('remove listener on child component', done => {
const spy2 = jasmine.createSpy('remove listener')
vm = new Vue({
el,
methods: { foo: spy, bar: spy2 },
data: {
ok: true
},
components: {
test: {
template: '<div></div>'
}
},
render (h) {
return this.ok
? h('test', { on: { foo: this.foo }})
: h('test', { on: { bar: this.bar }})
}
})
vm.$children[0].$emit('foo')
expect(spy.calls.count()).toBe(1)
expect(spy2.calls.count()).toBe(0)
vm.ok = false
waitForUpdate(() => {
vm.$children[0].$emit('foo')
expect(spy.calls.count()).toBe(1) // should no longer trigger
vm.$children[0].$emit('bar')
expect(spy2.calls.count()).toBe(1)
}).then(done)
})
it('warn missing handlers', () => {
vm = new Vue({
el,
data: { none: null },
template: `<div @click="none"></div>`
})
expect(`Invalid handler for event "click": got null`).toHaveBeenWarned()
expect(() => {
triggerEvent(vm.$el, 'click')
}).not.toThrow()
})
})