Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50655 views
1
2
var simpleEvents = require('nodeunit').testCase;
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
});
20
callback();
21
},
22
23
tearDown: function (callback) {
24
//clean up?
25
callback();
26
},
27
28
'1. Add a single listener on a single event.': function (test) {
29
30
var emitter = this.emitter;
31
var type = 'some.listener.bar';
32
33
emitter.on(type, function () {
34
test.ok(true, 'The event was raised');
35
});
36
37
test.equal(emitter.listeners(type).length, 1, 'There are three emitters');
38
39
test.expect(1);
40
test.done();
41
42
},
43
44
'1a. Add a single listener on a single event (using an array).': function (test) {
45
46
var emitter = this.emitter;
47
var type = ['some', 'listener', 'bar'];
48
49
emitter.on(type, function () {
50
test.ok(true, 'The event was raised');
51
});
52
53
test.equal(emitter.listeners(type).length, 1, 'There are three emitters');
54
55
test.expect(1);
56
test.done();
57
58
},
59
60
'2. Add two listeners on a single event.': function (test) {
61
62
var emitter = this.emitter;
63
var type = 'some.listener.bar';
64
65
emitter.on(type, function () {
66
test.ok(true, 'The event was raised');
67
});
68
69
emitter.on(type, function () {
70
test.ok(true, 'The event was raised');
71
});
72
73
test.equal(emitter.listeners(type).length, 2, 'There are three emitters');
74
75
test.expect(1);
76
test.done();
77
78
},
79
80
'2a. Add two listeners on a single event (using an array).': function (test) {
81
82
var emitter = this.emitter;
83
var type = ['some', 'listener', 'bar'];
84
85
emitter.on(type, function () {
86
test.ok(true, 'The event was raised');
87
});
88
89
emitter.on(type, function () {
90
test.ok(true, 'The event was raised');
91
});
92
93
test.equal(emitter.listeners(type).length, 2, 'There are three emitters');
94
95
test.expect(1);
96
test.done();
97
98
},
99
100
'3. Add three listeners on a single event.': function (test) {
101
102
var emitter = this.emitter;
103
var type = 'some.listener.bar';
104
105
emitter.on(type, function () {
106
test.ok(true, 'The event was raised');
107
});
108
109
emitter.on(type, function () {
110
test.ok(true, 'The event was raised');
111
});
112
113
emitter.on(type, function () {
114
test.ok(true, 'The event was raised');
115
});
116
117
test.equal(emitter.listeners(type).length, 3, 'There are three emitters');
118
119
test.expect(1);
120
test.done();
121
122
},
123
124
'4. Add two listeners to two different events.': function (test) {
125
126
var emitter = this.emitter;
127
var type = 'some.listener.bar';
128
129
emitter.on(type, function () {
130
test.ok(true, 'The event was raised');
131
});
132
133
emitter.on(type, function () {
134
test.ok(true, 'The event was raised');
135
});
136
137
emitter.on('test2', function () {
138
test.ok(true, 'The event was raised');
139
});
140
141
emitter.on('test2', function () {
142
test.ok(true, 'The event was raised');
143
});
144
145
test.equal(emitter.listeners(type).length, 2, 'There are two emitters');
146
test.equal(emitter.listeners('test2').length, 2, 'There are two emitters');
147
148
test.expect(2);
149
test.done();
150
},
151
152
'5. Never adding any listeners should yield a listeners array with the length of 0.': function (test) {
153
var emitter = this.emitter;
154
var type = 'some.listener.bar';
155
156
emitter.on(type, function () {
157
test.ok(true, 'The event was raised');
158
});
159
160
test.equal(emitter.listeners('test2').length, 0, 'There are no emitters');
161
162
test.expect(1);
163
test.done();
164
},
165
166
'6. the listener added should be the right listener.': function (test) {
167
var emitter = this.emitter;
168
var type = 'some.listener.bar';
169
var f = function () {};
170
171
emitter.on(type, f);
172
test.equal(emitter.listeners(type).length, 1, 'There are is one emitters');
173
test.equal(emitter.listeners(type)[0], f, 'The function should be f');
174
175
test.expect(2);
176
test.done();
177
178
},
179
180
'7. Listeners on `*`, `*.*`, `*.test` with emissions from `foo.test` and `other.emit`': function (test) {
181
var emitter = this.emitter;
182
var f = function () {
183
test.ok(true, 'the event was fired')
184
};
185
186
emitter.on('*.test', f);
187
emitter.on('*.*', f);
188
emitter.on('*', f);
189
190
emitter.emit('other.emit');
191
emitter.emit('foo.test');
192
193
test.expect(3);
194
test.done();
195
},
196
197
'8. Listeners on `*`, `*.*`, foo.test with emissions from `*`, `*.*` and `foo.test`': function (test) {
198
var emitter = this.emitter;
199
var f = function () {
200
test.ok(true, 'the event was fired')
201
};
202
203
emitter.on('foo.test', f);
204
emitter.on('*.*', f);
205
emitter.on('*', f);
206
207
emitter.emit('*.*');
208
emitter.emit('foo.test');
209
emitter.emit('*')
210
211
test.expect(5);
212
test.done();
213
},
214
215
'9. Listeners on `*`. (using an array)': function (test) {
216
217
var emitter = this.emitter;
218
var f = function () {
219
test.ok(true, 'the event was fired')
220
};
221
222
emitter.on(['*'], f);
223
emitter.emit('*')
224
225
test.expect(1);
226
test.done();
227
},
228
229
'10. actual event name': function(test) {
230
231
var emitter = this.emitter;
232
233
emitter.on('foo', function() {
234
emitter.emit('bar'); // changes the current event, passes the old one in as a parameter.
235
});
236
237
emitter.on('*', function() {
238
console.log(this.event);
239
});
240
241
242
243
emitter.emit('foo');
244
245
test.done();
246
},
247
248
'11. Listeners with multi-level wildcards': function (test) {
249
var emitter = this.emitter;
250
var i = 0;
251
var f = function (n) {
252
return function() {
253
//console.log('Event', n, 'fired by', this.event);
254
test.ok(true, 'the event was fired');
255
};
256
};
257
258
emitter.on('**.test', f(i++)); // 0: 0 + 1 + 0 + 0 + 1 + 1 + 1 + 1 + 1 + 1
259
emitter.on('**.bar.**', f(i++)); // 1: 0 + 1 + 1 + 1 + 1 + 0 + 0 + 1 + 1 + 1
260
emitter.on('**.*', f(i++)); // 2: 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
261
emitter.on('*.**', f(i++)); // 3: 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
262
emitter.on('**', f(i++)); // 4: 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
263
emitter.on('other.**', f(i++)); // 5: 1 + 0 + 0 + 0 + 1 + 0 + 0 + 0 + 1 + 1
264
emitter.on('foo.**.test', f(i++)); // 6: 0 + 1 + 0 + 0 + 1 + 0 + 1 + 1 + 1 + 1
265
emitter.on('test.**', f(i++)); // 7: 0 + 0 + 0 + 0 + 1 + 1 + 0 + 0 + 1 + 1
266
// Add forbidden patterns for safety purpose.
267
emitter.on('**.**', f(i++));
268
emitter.on('a.b.**.**', f(i++));
269
emitter.on('**.**.a.b', f(i++));
270
emitter.on('a.b.**.**.a.b', f(i++));
271
272
emitter.emit('other.emit'); // 4
273
emitter.emit('foo.bar.test'); // 6
274
emitter.emit('foo.bar.test.bar.foo.test.foo'); // 4
275
emitter.emit('bar.bar.bar.bar.bar.bar'); // 4
276
emitter.emit('**.*'); // 8
277
emitter.emit('test'); // 5
278
emitter.emit('foo.test'); // 5
279
emitter.emit('foo.**.*'); // 6
280
emitter.emit('**.test'); // 8
281
emitter.emit('**.test.**'); // 8
282
//emitter.emit('*.**.test.**.a'); // 0
283
284
test.expect(58);
285
test.done();
286
},
287
288
'12. Check return values of emit for wildcard emitter.': function (test) {
289
290
var emitter = this.emitter;
291
292
emitter.on('foo.*', function () {
293
test.ok(true, 'The event was raised');
294
});
295
296
emitter.onAny(function () {
297
test.ok(true, 'The event was raised');
298
});
299
300
test.ok(emitter.emit('foo.blah'), 'emit should return true after calling a listener');
301
test.ok(emitter.emit('bar'), 'emit should return true after calling a listener');
302
303
test.expect(5);
304
test.done();
305
}
306
307
});
308
309