forked from EventEmitter2/EventEmitter2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addListener.js
157 lines (112 loc) · 3.58 KB
/
addListener.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var simpleEvents = require('nodeunit').testCase;
var file = '../../lib/eventemitter2';
module.exports = simpleEvents({
setUp: function (callback) {
var EventEmitter2;
if(typeof require !== 'undefined') {
EventEmitter2 = require(file).EventEmitter2;
}
else {
EventEmitter2 = window.EventEmitter2;
}
this.emitter = new EventEmitter2({
wildcard: true,
verbose: true
});
callback();
},
tearDown: function (callback) {
//clean up?
callback();
},
'1. Add a single listener on a single event.': function (test) {
var emitter = this.emitter;
var type = 'some.listener.bar';
emitter.on(type, function () {
test.ok(true, 'The event was raised');
});
test.equal(emitter.listeners(type).length, 1, 'There are three emitters');
test.expect(1);
test.done();
},
'2. Add two listeners on a single event.': function (test) {
var emitter = this.emitter;
var type = 'some.listener.bar';
emitter.on(type, function () {
test.ok(true, 'The event was raised');
});
emitter.on(type, function () {
test.ok(true, 'The event was raised');
});
test.equal(emitter.listeners(type).length, 2, 'There are three emitters');
test.expect(1);
test.done();
},
'3. Add three listeners on a single event.': function (test) {
var emitter = this.emitter;
var type = 'some.listener.bar';
emitter.on(type, function () {
test.ok(true, 'The event was raised');
});
emitter.on(type, function () {
test.ok(true, 'The event was raised');
});
emitter.on(type, function () {
test.ok(true, 'The event was raised');
});
test.equal(emitter.listeners(type).length, 3, 'There are three emitters');
test.expect(1);
test.done();
},
'4. Add two listeners to two different events.': function (test) {
var emitter = this.emitter;
var type = 'some.listener.bar';
emitter.on(type, function () {
test.ok(true, 'The event was raised');
});
emitter.on(type, function () {
test.ok(true, 'The event was raised');
});
emitter.on('test2', function () {
test.ok(true, 'The event was raised');
});
emitter.on('test2', function () {
test.ok(true, 'The event was raised');
});
test.equal(emitter.listeners(type).length, 2, 'There are two emitters');
test.equal(emitter.listeners('test2').length, 2, 'There are two emitters');
test.expect(2);
test.done();
},
'5. Never adding any listeners should yield a listeners array with the length of 0.': function (test) {
var emitter = this.emitter;
var type = 'some.listener.bar';
emitter.on(type, function () {
test.ok(true, 'The event was raised');
});
test.equal(emitter.listeners('test2').length, 0, 'There are no emitters');
test.expect(1);
test.done();
},
'6. the listener added should be the right listener.': function (test) {
var emitter = this.emitter;
var type = 'some.listener.bar';
var f = function () {};
emitter.on(type, f);
test.equal(emitter.listeners(type).length, 1, 'There are is one emitters');
test.equal(emitter.listeners(type)[0], f, 'The function should be f');
test.expect(2);
test.done();
},
'7. A listener should respond to two wildcards seperated by a delimiter.': function (test) {
var emitter = this.emitter;
var type = '*.*';
var f = function () {
test.ok(true, 'the event was fired')
};
emitter.on(type, f);
emitter.emit('foo.foo');
test.expect(1);
test.done();
}
});