-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents_spec.js
134 lines (121 loc) · 3.46 KB
/
events_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
var Vue = require('../../../../src/vue')
describe('Events API', function () {
var vm, spy
beforeEach(function () {
vm = new Vue()
spy = jasmine.createSpy('emitter')
})
it('$on', function () {
vm.$on('test', function () {
// expect correct context
expect(this).toBe(vm)
spy.apply(this, arguments)
})
vm.$emit('test', 1, 2 ,3, 4)
expect(spy.calls.count()).toBe(1)
expect(spy).toHaveBeenCalledWith(1, 2, 3, 4)
})
it('$once', function () {
vm.$once('test', spy)
vm.$emit('test', 1, 2 ,3)
vm.$emit('test', 2, 3, 4)
expect(spy.calls.count()).toBe(1)
expect(spy).toHaveBeenCalledWith(1, 2, 3)
})
it('$off', function () {
vm.$on('test1', spy)
vm.$on('test2', spy)
vm.$off()
vm.$emit('test1')
vm.$emit('test2')
expect(spy).not.toHaveBeenCalled()
})
it('$off event', function () {
vm.$on('test1', spy)
vm.$on('test2', spy)
vm.$off('test1')
vm.$off('test1') // test off something that's already off
vm.$emit('test1', 1)
vm.$emit('test2', 2)
expect(spy.calls.count()).toBe(1)
expect(spy).toHaveBeenCalledWith(2)
})
it('$off event + fn', function () {
var spy2 = jasmine.createSpy('emitter')
vm.$on('test', spy)
vm.$on('test', spy2)
vm.$off('test', spy)
vm.$emit('test', 1, 2, 3)
expect(spy).not.toHaveBeenCalled()
expect(spy2.calls.count()).toBe(1)
expect(spy2).toHaveBeenCalledWith(1, 2, 3)
})
it('$broadcast', function () {
var child1 = vm.$addChild()
var child2 = vm.$addChild()
var child3 = child1.$addChild()
child1.$on('test', spy)
child2.$on('test', spy)
child3.$on('test', spy)
vm.$broadcast('test')
expect(spy.calls.count()).toBe(3)
})
it('$broadcast optimization', function () {
var child = vm.$addChild()
var child2 = child.$addChild()
// hooks should not incurr the bookkeeping cost
child.$on('hook:created', function () {})
expect(vm._eventsCount['hook:created']).toBeUndefined()
child.$on('test', spy)
expect(vm._eventsCount['test']).toBe(1)
// child2's $emit & $broadcast
// shouldn't get called if no child listens to the event
child2.$emit = spy
child2.$broadcast = spy
vm.$broadcast('test')
expect(spy.calls.count()).toBe(1)
// check $off bookkeeping
child.$off('test', spy)
expect(vm._eventsCount['test']).toBe(0)
function noop () {}
child.$on('test', noop)
child2.$on('test', noop)
expect(vm._eventsCount['test']).toBe(2)
child.$off('test')
expect(vm._eventsCount['test']).toBe(1)
child.$on('test', noop)
child2.$on('test', noop)
expect(vm._eventsCount['test']).toBe(3)
child.$off()
child2.$off()
expect(vm._eventsCount['test']).toBe(0)
})
it('$broadcast cancel', function () {
var child = vm.$addChild()
var child2 = child.$addChild()
child.$on('test', function () {
return false
})
child2.$on('test', spy)
vm.$broadcast('test')
expect(spy).not.toHaveBeenCalled()
})
it('$dispatch', function () {
var child = vm.$addChild()
var child2 = child.$addChild()
child.$on('test', spy)
vm.$on('test', spy)
child2.$dispatch('test')
expect(spy.calls.count()).toBe(2)
})
it('$dispatch cancel', function () {
var child = vm.$addChild()
var child2 = child.$addChild()
child.$on('test', function () {
return false
})
vm.$on('test', spy)
child2.$dispatch('test')
expect(spy).not.toHaveBeenCalled()
})
})