-
Notifications
You must be signed in to change notification settings - Fork 36
/
test.js
214 lines (181 loc) · 5.99 KB
/
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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var assert = require('assert'),
Events = require('./dist/minivents.commonjs.js');
describe('Events "new" Constructor', function () {
it('should return an object containing the methods used by minivents if called with new', function () {
var bus = new Events();
assert.equal('object', typeof bus);
});
it('should contain an `emit` function', function () {
var bus = new Events();
assert.equal('function', typeof bus.emit);
});
it('should contain an `off` function', function () {
var bus = new Events();
assert.equal('function', typeof bus.off);
});
it('should contain an `on` function', function () {
var bus = new Events();
assert.equal('function', typeof bus.on);
});
});
describe('Events "mixin" Constructor', function () {
it('should return undefined if called with an empty object', function () {
var bus = {},
ret = Events(bus);
assert.equal(undefined, ret);
});
it('should contain an `emit` function', function () {
var bus = {};
Events(bus);
assert.equal('function', typeof bus.emit);
});
it('should contain an `off` function', function () {
var bus = {};
Events(bus);
assert.equal('function', typeof bus.off);
});
it('should contain an `on` function', function () {
var bus = {};
Events(bus);
assert.equal('function', typeof bus.on);
});
});
describe('`on` function', function () {
it('should not throw any exceptions when called with a string and a function', function () {
var bus = new Events();
try {
bus.on('ping', function () { });
} catch (e) {
assert.fail(undefined, e, e.toString());
}
});
it('should not throw any exceptions when called with a string, a function and an object', function () {
var bus = new Events();
try {
bus.on('ping', function () { }, { });
} catch (e) {
assert.fail(undefined, e, e.toString());
}
});
it('should return target', function () {
var bus = new Events();
assert.equal(bus.on('ping', function () { }), bus);
});
});
describe('`off` function', function () {
it('should not throw any exceptions when called with a string and a function', function () {
var bus = new Events(),
f = function () { };
bus.on('ping', f);
try {
bus.off('ping', f);
} catch (e) {
assert.fail(undefined, e, e.toString());
}
});
it('should not throw any exceptions when called with a function that is not registered as a callback for the specified event', function () {
var bus = new Events();
bus.on('ping', function () { });
try {
bus.off('ping', function () { });
} catch (e) {
assert.fail(undefined, e, e.toString());
}
});
it('should not throw any exceptions when called with the name of an event that does not exist', function () {
var bus = new Events(),
f = function () { };
bus.on('ping', f);
try {
bus.off('pong', f);
} catch (e) {
assert.fail(undefined, e, e.toString());
}
});
it('should result in the given callback no longer being triggered', function () {
var bus = new Events(),
f = function () { assert.fail(undefined, undefined, 'This function must not be executed.'); };
bus.on('ping', f);
bus.off('ping', f);
bus.emit('ping');
});
it('should return target', function () {
var bus = new Events(),
f = function () { };
bus.on('ping', f);
assert.equal(bus.off('ping'), bus);
});
});
describe('`emit` function', function () {
it('should result in registered callbacks being invoked', function () {
var bus = new Events(),
called = 0,
f1 = function () { called++ },
f2 = function () { called++ },
f3 = function () { called++ };
bus.on('ping', f1);
bus.on('ping', f2);
bus.on('ping', f3);
bus.emit('ping');
assert.equal(called, 3);
});
it('should pass all of its additional arguments to the callback', function () {
var bus = new Events(),
f = function (arg0, arg1) {
assert.equal('foo', arg0);
assert.equal('bar', arg1);
};
bus.on('ping', f);
bus.emit('ping', 'foo', 'bar');
});
it('should inject a callback\'s context', function () {
var bus = new Events(),
ctx = { }
f = function () { assert.equal(ctx, this); };
bus.on('ping', f, ctx);
bus.emit('ping');
});
it('should result in registered callbacks being invoked with cascade', function () {
var bus = new Events(),
called = 0,
cascade = false,
f1 = function () { called++ },
f2 = function () { called++ },
f3 = function () { called++ },
f4 = function () { bus.emit('cascade'); },
f5 = function () { called++ },
f6 = function () { cascade = true; };
bus.on('ping', f1);
bus.on('ping', f2);
bus.on('ping', f3);
bus.on('ping', f4);
bus.on('ping', f5);
bus.on('cascade', f6);
bus.emit('ping');
assert.equal(called, 4);
assert.equal(cascade, true);
});
it('should result in registered callbacks being invoked event if one callback remove himself', function () {
var bus = new Events(),
called = 0,
cascade = false,
f1 = function () { called++ },
f2 = function () { called++ },
f3 = function () { called++ },
f4 = function () { bus.off('ping', f4); },
f5 = function () { called++ };
bus.on('ping', f1);
bus.on('ping', f2);
bus.on('ping', f3);
bus.on('ping', f4);
bus.on('ping', f5);
bus.emit('ping');
assert.equal(called, 4);
});
it('should return target', function () {
var bus = new Events(),
f = function () { };
bus.on('ping', f);
assert.equal(bus.emit('ping'), bus);
});
});