Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50660 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
delimiter: '::'
20
});
21
callback();
22
},
23
24
tearDown: function (callback) {
25
//clean up?
26
callback();
27
},
28
29
'1. Add a single listener on a single event.': function (test) {
30
31
var emitter = this.emitter;
32
var type = 'some::listener::bar';
33
34
emitter.on(type, function () {
35
test.ok(true, 'The event was raised');
36
});
37
38
test.equal(emitter.listeners(type).length, 1, 'There are three emitters');
39
40
test.expect(1);
41
test.done();
42
43
},
44
45
'2. Add two listeners on a single event.': function (test) {
46
47
var emitter = this.emitter;
48
var type = 'some::listener::bar';
49
50
emitter.on(type, function () {
51
test.ok(true, 'The event was raised');
52
});
53
54
emitter.on(type, function () {
55
test.ok(true, 'The event was raised');
56
});
57
58
test.equal(emitter.listeners(type).length, 2, 'There are three emitters');
59
60
test.expect(1);
61
test.done();
62
63
},
64
'3. Add three listeners on a single event.': function (test) {
65
66
var emitter = this.emitter;
67
var type = 'some::listener::bar';
68
69
emitter.on(type, function () {
70
test.ok(true, 'The event was raised');
71
});
72
73
emitter.on(type, function () {
74
test.ok(true, 'The event was raised');
75
});
76
77
emitter.on(type, function () {
78
test.ok(true, 'The event was raised');
79
});
80
81
test.equal(emitter.listeners(type).length, 3, 'There are three emitters');
82
83
test.expect(1);
84
test.done();
85
86
},
87
'4. Add two listeners to two different events.': function (test) {
88
89
var emitter = this.emitter;
90
var type = 'some::listener::bar';
91
92
emitter.on(type, function () {
93
test.ok(true, 'The event was raised');
94
});
95
96
emitter.on(type, function () {
97
test.ok(true, 'The event was raised');
98
});
99
100
emitter.on('test2', function () {
101
test.ok(true, 'The event was raised');
102
});
103
104
emitter.on('test2', function () {
105
test.ok(true, 'The event was raised');
106
});
107
108
test.equal(emitter.listeners(type).length, 2, 'There are two emitters');
109
test.equal(emitter.listeners('test2').length, 2, 'There are two emitters');
110
111
test.expect(2);
112
test.done();
113
},
114
115
'5. Never adding any listeners should yield a listeners array with the length of 0.': function (test) {
116
var emitter = this.emitter;
117
var type = 'some::listener::bar';
118
119
emitter.on(type, function () {
120
test.ok(true, 'The event was raised');
121
});
122
123
test.equal(emitter.listeners('test2').length, 0, 'There are no emitters');
124
125
test.expect(1);
126
test.done();
127
},
128
129
'6. the listener added should be the right listener.': function (test) {
130
var emitter = this.emitter;
131
var type = 'some::listener::bar';
132
var f = function () {};
133
134
emitter.on(type, f);
135
test.equal(emitter.listeners(type).length, 1, 'There are is one emitters');
136
test.equal(emitter.listeners(type)[0], f, 'The function should be f');
137
138
test.expect(2);
139
test.done();
140
141
},
142
143
'7. Listeners on *, *::*, *::test with emissions from foo::test and other::emit': function (test) {
144
var emitter = this.emitter;
145
var f = function () {
146
test.ok(true, 'the event was fired')
147
};
148
149
emitter.on('*::test', f);
150
emitter.on('*::*', f);
151
emitter.on('*', f);
152
153
emitter.emit('other::emit');
154
emitter.emit('foo::test');
155
156
test.expect(3);
157
test.done();
158
},
159
160
'8. Listeners on *, *::*, foo.test with emissions from *, *::* and foo.test': function (test) {
161
var emitter = this.emitter;
162
var f = function () {
163
test.ok(true, 'the event was fired')
164
};
165
166
emitter.on('foo::test', f);
167
emitter.on('*::*', f);
168
emitter.on('*', f);
169
170
emitter.emit('*::*');
171
emitter.emit('foo::test');
172
emitter.emit('*')
173
174
test.expect(5);
175
test.done();
176
},
177
178
'9. Listeners on **, **::*, **::test with emissions from foo::test and other::emit': function (test) {
179
var emitter = this.emitter;
180
var f = function () {
181
test.ok(true, 'the event was fired');
182
};
183
184
emitter.on('**::test', f);
185
emitter.on('**::*', f);
186
emitter.on('**', f);
187
188
emitter.emit('other::emit'); // 2
189
emitter.emit('foo::test'); // 3
190
191
test.expect(5);
192
test.done();
193
},
194
195
'10. Listeners on **, **::*, foo.test with emissions from **, **::* and foo.test': function (test) {
196
var emitter = this.emitter, i = 0;
197
var f = function (n) {
198
return function() {
199
//console.log(n, this.event);
200
test.ok(true, 'the event was fired');
201
};
202
};
203
204
emitter.on('foo::test', f(i++));
205
emitter.on('**::*', f(i++));
206
emitter.on('**', f(i++));
207
208
emitter.emit('**::*'); // 3
209
emitter.emit('foo::test'); // 3
210
emitter.emit('**'); // 3
211
212
test.expect(9);
213
test.done();
214
}
215
216
});
217
218