Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50655 views
1
var simpleEvents = require('nodeunit').testCase;
2
3
var file = '../../lib/eventemitter2';
4
5
module.exports = simpleEvents({
6
7
setUp: function (callback) {
8
var EventEmitter2;
9
10
if(typeof require !== 'undefined') {
11
EventEmitter2 = require(file).EventEmitter2;
12
}
13
else {
14
EventEmitter2 = window.EventEmitter2;
15
}
16
17
this.emitter = new EventEmitter2({
18
wildcard : true,
19
verbose : true
20
});
21
callback();
22
},
23
24
tearDown: function (callback) {
25
//clean up?
26
callback();
27
},
28
29
'1. A listener added with `once` should only listen once and then be removed.': function (test) {
30
31
var emitter = this.emitter;
32
var type = 'test1.foo.bar';
33
34
emitter.once(type, function () {
35
test.ok(true, 'The event was raised once');
36
});
37
38
emitter.emit(type);
39
emitter.emit(type);
40
41
test.expect(1);
42
test.done();
43
44
},
45
'2. A listener with a TTL of 4 should only listen 4 times.': function (test) {
46
47
var emitter = this.emitter;
48
var type = 'test1.foo.bar';
49
50
emitter.many(type, 4, function (value1) {
51
test.ok(true, 'The event was raised 4 times.');
52
});
53
54
emitter.emit(type, 1);
55
emitter.emit(type, 2);
56
emitter.emit(type, 3);
57
emitter.emit(type, 4);
58
emitter.emit(type, 5);
59
60
test.expect(4);
61
test.done();
62
63
},
64
'3. A listener with a TTL of 4 should only listen 4 times and pass parameters.': function (test) {
65
66
var emitter = this.emitter;
67
var type = 'test1.foo.bar';
68
69
emitter.many(type, 4, function (value1, value2, value3) {
70
test.ok(typeof value1 !== 'undefined', 'got value 1');
71
test.ok(typeof value2 !== 'undefined', 'got value 2');
72
test.ok(typeof value3 !== 'undefined', 'got value 3');
73
});
74
75
emitter.emit(type, 1, 'A', false);
76
emitter.emit(type, 2, 'A', false);
77
emitter.emit(type, 3, 'A', false);
78
emitter.emit(type, 4, 'A', false);
79
emitter.emit(type, 5, 'A', false);
80
81
test.done();
82
83
},
84
'4. Remove an event listener by signature.': function (test) {
85
86
var emitter = this.emitter;
87
var type = 'test1.foo.bar';
88
var count = 0;
89
90
function f1(event) {
91
"event A";
92
test.ok(true, 'The event was raised less than 3 times.');
93
}
94
95
emitter.on(type, f1);
96
97
function f2(event) {
98
"event B";
99
test.ok(true, 'The event was raised less than 3 times.');
100
}
101
102
emitter.on(type, f2);
103
104
function f3(event) {
105
"event C";
106
test.ok(true, 'The event was raised less than 3 times.');
107
}
108
109
emitter.on(type, f3);
110
111
emitter.removeListener(type, f2);
112
113
emitter.emit(type);
114
115
test.expect(2);
116
test.done();
117
118
},
119
'5. `removeListener` and `once`': function(test) {
120
121
var emitter = this.emitter;
122
var type = 'test1.foo.bar';
123
var functionA = function() { test.ok(true, 'Event was fired'); };
124
125
emitter.once(type, functionA);
126
emitter.removeListener(type, functionA);
127
128
emitter.emit(type);
129
130
test.expect(0);
131
test.done();
132
},
133
134
'6. Listening with a wildcard on once' : function (test) {
135
var emitter = this.emitter;
136
var type = 'test1.foo.*';
137
var functionA = function() { test.ok(true, 'Event was fired'); };
138
139
emitter.once(type, functionA);
140
emitter.on(type,functionA);
141
142
emitter.emit(type); //2
143
emitter.emit(type); //1
144
145
test.expect(3);
146
test.done();
147
},
148
149
'7. Emitting with a wildcard targeted at once' : function (test) {
150
var emitter = this.emitter;
151
var type = 'test1.foo.bar';
152
var type2 = 'test1.foo.*';
153
var functionA = function() { test.ok(true, 'Event was fired'); };
154
155
emitter.once(type, functionA);
156
emitter.emit(type2);
157
emitter.emit(type2);
158
159
test.expect(1);
160
test.done();
161
},
162
163
'8. Emitting with a multi-level wildcard on once': function(test) {
164
var emitter = this.emitter, i = 0;
165
var type = 'test1.**';
166
var functionA = function(n) {
167
return function() {
168
//console.log(n, this.event);
169
test.ok(true, 'Event was fired');
170
};
171
}
172
173
emitter.once(type, functionA(i++));
174
emitter.on(type,functionA(i++));
175
emitter.emit(type); //2
176
emitter.emit(type); //1
177
178
test.expect(3);
179
test.done();
180
},
181
182
'9. Emitting with a multi-level wildcard targeted at once' : function (test) {
183
var emitter = this.emitter;
184
var type = 'test1.foo.bar';
185
var type2 = 'test1.**';
186
var functionA = function() { test.ok(true, 'Event was fired'); };
187
188
emitter.once(type, functionA);
189
emitter.emit(type2);
190
emitter.emit(type2);
191
192
test.expect(1);
193
test.done();
194
}
195
196
});
197
198