forked from yangmingshan/vue-bus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
90 lines (75 loc) · 1.48 KB
/
index.test.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
const Vue = require('vue');
const VueBus = require('./index');
Vue.use(VueBus);
test('Vue.bus', () => {
const vm = new Vue({
data() {
return { count: 0 };
},
created() {
this.$bus.on('add', num => { this.count += num });
this.$bus.once('addOnce', num => { this.count += num });
},
methods: {
clean() {
this.$bus.off('add');
}
}
});
const obj = {
fire() {
Vue.bus.emit('add', 1);
},
fireOnce() {
Vue.bus.emit('addOnce', 1);
}
};
obj.fire();
expect(vm.count).toBe(1);
obj.fire();
expect(vm.count).toBe(2);
vm.clean();
obj.fire();
expect(vm.count).toBe(2);
obj.fireOnce();
expect(vm.count).toBe(3);
obj.fireOnce();
expect(vm.count).toBe(3);
});
test('this.$bus', () => {
const vm1 = new Vue({
data() {
return { count: 0 };
},
created() {
this.$bus.on('add', num => { this.count += num });
this.$bus.once('addOnce', num => { this.count += num });
},
methods: {
clean() {
this.$bus.off('add');
}
}
});
const vm2 = new Vue({
methods: {
fire() {
this.$bus.emit('add', 1);
},
fireOnce() {
this.$bus.emit('addOnce', 1);
}
}
});
vm2.fire();
expect(vm1.count).toBe(1);
vm2.fire();
expect(vm1.count).toBe(2);
vm1.clean();
vm2.fire();
expect(vm1.count).toBe(2);
vm2.fireOnce();
expect(vm1.count).toBe(3);
vm2.fireOnce();
expect(vm1.count).toBe(3);
});