-
Notifications
You must be signed in to change notification settings - Fork 24
/
test.js
335 lines (260 loc) Β· 7.01 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
var tape = require('tape')
var nanobus = require('./')
tape('nanobus', function (t) {
t.test('should assert input types', function (t) {
t.plan(11)
var bus = nanobus()
t.throws(bus.emit.bind(bus), /string/)
t.throws(bus.on.bind(bus), /string/)
t.throws(bus.on.bind(bus, 'foo'), /function/)
t.throws(bus.once.bind(bus), /string/)
t.throws(bus.once.bind(bus, 'foo'), /function/)
t.throws(bus.removeListener.bind(bus), /string/)
t.throws(bus.removeListener.bind(bus, 'foo'), /function/)
var s = Symbol('event')
var fn = function () {}
t.doesNotThrow(bus.emit.bind(bus, s))
t.doesNotThrow(bus.on.bind(bus, s, fn))
t.doesNotThrow(bus.once.bind(bus, s, fn))
t.doesNotThrow(bus.removeListener.bind(bus, s, fn))
})
t.test('should emit messages', function (t) {
t.plan(4)
var bus = nanobus()
var obj = { bin: 'baz' }
bus.on('foo:bar', function (data) {
t.equal(data, obj, 'data was same')
})
bus.emit('foo:bar', obj)
bus.on('beep:boop', function (data) {
t.equal(data, undefined)
})
bus.emit('beep:boop')
bus.on('baz:floop', function (arg1, arg2) {
t.equal(arg1, 'arg1', 'data was same')
t.equal(arg2, 'arg2', 'data was same')
})
bus.emit('baz:floop', 'arg1', 'arg2')
})
t.test('should prepend listeners', function (t) {
t.plan(4)
var i = 0
var bus = nanobus()
bus.on('foo:bar', function (data) {
t.equal(i, 1)
})
bus.prependListener('foo:bar', function (data) {
t.equal(i, 0)
i++
})
bus.emit('foo:bar')
bus.on('*', function (data) {
t.equal(i, 1)
})
bus.prependListener('*', function (eventName, data) {
t.equal(i, data)
i++
})
i = 0
bus.emit('bar:baz', i)
})
t.test('should prepend once listeners', function (t) {
t.plan(3)
var i = 0
var bus = nanobus()
bus.on('foo:bar', function (data) {
t.equal(i, 1)
})
bus.prependOnceListener('foo:bar', function (data) {
t.equal(i, 0)
i++
})
bus.emit('foo:bar')
bus.emit('foo:bar')
})
t.test('should emit messages once', function (t) {
t.plan(1)
var bus = nanobus()
bus.once('foo:bar', function (data) {
t.pass('called')
})
bus.emit('foo:bar')
bus.emit('foo:bar')
})
t.test('should properly emit messages when using both once() and on() ', function (t) {
t.plan(2)
var bus = nanobus()
var i = 0
bus.once('foo:bar', function onceIncrement () {
i++
})
bus.on('foo:bar', function onIncrement () {
i++
})
bus.once('*', function wildcardOnceIncrement () {
i++
})
bus.on('*', function wildcardOnIncrement () {
i++
})
bus.emit('foo:bar')
t.equal(i, 4, 'incremented by once() and on()')
bus.emit('foo:bar')
t.equal(i, 6, 'incremented by on() only')
})
t.test('should trigger wildcard once', function (t) {
t.plan(3)
var bus = nanobus()
bus.once('*', function (data) {
t.pass('called')
})
bus.on('foo:bar', function (data) {
t.pass('called foo:bar')
})
bus.on('foo:baz', function (data) {
t.pass('called foo:baz')
})
bus.emit('foo:bar')
bus.emit('foo:baz')
})
t.test('should be able to remove listeners', function (t) {
t.plan(3)
var bus = nanobus()
bus.on('foo:bar', goodHandler)
bus.on('foo:bar', badHandler)
bus.removeListener('foo:bar', badHandler)
bus.emit('foo:bar')
bus.once('foo:bar', goodHandler)
bus.removeListener('foo:bar', onceHandler)
bus.emit('foo:bar')
function goodHandler (data) {
t.pass('called')
}
function onceHandler (data) {
t.pass('called')
}
function badHandler (data) {
t.fail('oh no!')
}
})
t.test('should be able to remove all listeners', function (t) {
t.plan(1)
var bus = nanobus()
var i = 0
bus.on('foo:bar', handler)
bus.on('bin:baz', handler)
bus.removeAllListeners()
bus.emit('foo:bar')
bus.emit('bin:baz')
t.equal(i, 0, 'no events called')
function handler (data) {
i++
}
})
t.test('should be able to remove all listeners for an event', function (t) {
t.plan(1)
var bus = nanobus()
var i = 0
bus.on('foo:bar', handler)
bus.on('bin:baz', handler)
bus.removeAllListeners('bin:baz')
bus.emit('foo:bar')
bus.emit('bin:baz')
t.equal(i, 1, '1 event called')
function handler (data) {
i++
}
})
t.test('should be able to have * listeners', function (t) {
t.plan(12)
var bus = nanobus()
var i = 0
bus.on('foo:bar', handler)
bus.on('bin:baz', handler)
bus.on('*', handler)
bus.emit('foo:bar')
t.equal(i, 2, 'count 2')
bus.emit('bin:baz')
t.equal(i, 4, 'count 4')
bus.removeAllListeners('bin:baz')
bus.emit('bin:baz')
t.equal(i, 5, 'count 5')
bus.removeListener('*', handler)
bus.emit('foo:bar')
t.equal(i, 6, 'count 6')
bus.on('*', handler)
bus.emit('foo:bar')
t.equal(i, 8, 'count 8')
bus.removeAllListeners('*')
bus.emit('foo:bar')
t.equal(i, 9, 'count 9')
bus.on('*', handler)
bus.emit('foo:bar')
t.equal(i, 11, 'count 11')
bus.removeAllListeners()
t.equal(i, 11, 'count 11')
bus.once('*', handler)
bus.emit('foo:bar')
t.equal(i, 12, 'count 12')
bus.emit('foo:bar')
t.equal(i, 12, 'count 12')
bus.removeAllListeners()
t.equal(i, 12, 'count 12')
bus.on('*', starHandler)
bus.emit('star:event', i)
bus.removeAllListeners()
function handler (data) {
i++
}
function starHandler (eventName, data) {
t.equal(data, i, 'data was same')
}
})
t.test('should be able to remove listeners that have not been attached', function (t) {
var bus = nanobus()
t.doesNotThrow(function () {
bus.removeListener('yay', handler)
}, 'removes unattched "yay" event')
t.doesNotThrow(function () {
bus.removeListener('*', handler)
}, 'removes unattached "*" event')
t.end()
function handler () {}
})
t.test('should be able to get an array of listeners', function (t) {
t.plan(2)
var bus = nanobus()
bus.on('foo', bar)
bus.on('foo', baz)
t.deepEqual(bus.listeners('foo'), [bar, baz])
bus.on('*', bar)
bus.on('*', baz)
t.deepEqual(bus.listeners('foo'), [bar, baz])
function bar (data) {}
function baz (data) {}
})
t.test('should be able to trigger multiple listeners with same args', function (t) {
t.plan(6)
var obj = {foo: 'bar'}
var bus = nanobus()
bus.on('foo', function (data) {
t.deepEqual(data, obj)
})
bus.on('foo', function (data) {
t.deepEqual(data, obj)
})
bus.on('foo', function (data) {
t.deepEqual(data, obj)
})
bus.on('*', function (name, data) {
t.deepEqual(data, obj)
})
bus.on('*', function (name, data) {
t.deepEqual(data, obj)
})
bus.on('*', function (name, data) {
t.deepEqual(data, obj)
})
bus.emit('foo', obj)
})
})