-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.js
320 lines (275 loc) · 7.03 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
/*!
* dush <https://github.com/tunnckoCore/dush>
*
* Copyright (c) Charlike Mike Reagent <@tunnckoCore> (https://i.am.charlike.online)
* Released under the MIT license.
*/
/* jshint asi:true */
'use strict'
var test = require('mukla')
var dush = require('./dist/dush.common')
var app = dush()
test('should return an instance with methods and `._allEvents` object', function (
done
) {
test.strictEqual(typeof app._allEvents, 'object')
test.strictEqual(typeof app.use, 'function')
test.strictEqual(typeof app.on, 'function')
test.strictEqual(typeof app.off, 'function')
test.strictEqual(typeof app.once, 'function')
test.strictEqual(typeof app.emit, 'function')
done()
})
test('should instace has ._allEvents object that contains all handlers', function (
done
) {
var fn = function () {}
app.on('aaa', fn)
app.on('aaa', fn)
app.on('bbb', fn)
app.on('ccc', fn)
app.on('ccc', fn)
app.on('ccc', fn)
test.deepStrictEqual(Object.keys(app._allEvents), ['aaa', 'bbb', 'ccc'])
test.strictEqual(app._allEvents.aaa.length, 2)
test.strictEqual(app._allEvents.bbb.length, 1)
test.strictEqual(app._allEvents.ccc.length, 3)
app.emit('zzz')
done()
})
test('should register handlers for any type of string', function (done) {
var app = dush()
app.on('constructor', function (a) {
test.ok(a === 2)
})
app.emit('constructor', 2)
done()
})
test('should .emit with multiple params (maximum 3)', function (done) {
var emitter = dush()
emitter.on('foo', function (a, b) {
test.strictEqual(a, 'aaa')
test.strictEqual(b, 'bbb')
})
emitter.emit('foo', 'aaa', 'bbb')
done()
})
test('should .on register multiple handlers', function (done) {
var called = 0
var fn = function (a) {
called++
test.strictEqual(a, 123)
}
app.on('foo', fn)
app.on('foo', fn)
app.emit('foo', 123)
test.strictEqual(called, 2)
done()
})
test('should handlers added with .once be called one time only', function (
done
) {
var called = 0
app.once('bar', function (xx) {
called++
})
app.emit('bar')
app.emit('bar')
app.emit('bar')
test.strictEqual(called, 1)
done()
})
test('should .off("foo", fn) remove the handler', function (done) {
var called = 0
var second = 0
var fn = function () {
called++
}
app.on('qux', fn)
app.on('qux', function () {
second = 1
})
app.off('qux', fn)
app.emit('qux')
test.strictEqual(called, 0)
test.strictEqual(second, 1)
test.strictEqual(app._allEvents.qux.length, 1)
done()
})
test('should .off("foo") remove all "foo" handlers', function (done) {
app.on('zzz', function () {}).on('zzz', function () {}).off('zzz')
test.strictEqual(app._allEvents.zzz.length, 0)
done()
})
test('should all methods be chainable', function (done) {
var called = 0
var foo = app.on('foo', function () {})
test.ok(foo.once)
var bar = foo.once('bar', function () {
called++
})
test.ok(bar.emit)
var qux = bar.emit('bar')
test.ok(qux.off)
qux.off('bar').emit('bar')
test.strictEqual(called, 1)
done()
})
test('should have wildcard event', function (done) {
var app = dush()
app.on('*', function (name, nume) {
test.strictEqual(name, 'haha')
test.strictEqual(nume, 444444)
})
app.emit('haha', 444444)
done()
})
test('should return app if .use(plugin) dont', function (done) {
var app = dush()
app
.use(function (app) {
app.foo = 'bar'
})
.use(function (app) {
app.baz = 12345
return app
})
.use(function (app) {
app.qux = 'zzz'
})
test.strictEqual(app.foo, 'bar')
test.strictEqual(app.baz, 12345)
test.strictEqual(app.qux, 'zzz')
done()
})
test('should not allow emitting the wildcard (issue#5)', function (done) {
var emitter = dush()
// eslint-disable-next-line max-params
emitter.on('*', function (name, a, b, c) {
test.strictEqual(name, 'foo')
test.strictEqual(a, 1)
test.strictEqual(b, 2)
test.strictEqual(c, 3)
})
emitter.on('foo', function (a, b, c) {
test.strictEqual(a, 1)
test.strictEqual(b, 2)
test.strictEqual(c, 3)
})
emitter.emit('*', 4, 5, 6)
emitter.emit('foo', 1, 2, 3)
emitter.emit('*', 555)
done()
})
test('should not add additional arguments when emit', function (done) {
var app = dush()
app.on('foo', function () {
// prev versions was calling the handler
// with `undefined, undefined, undefined` event if
// `.emit` don't pass any arguments...
test.strictEqual(arguments.length, 1)
done()
})
app.emit('foo', 1)
})
test('should support to emit any number of arguments', function (done) {
dush()
// eslint-disable-next-line max-params
.on('zazzie', function (aa, bb, cc, dd, ee) {
test.strictEqual(aa, 1)
test.strictEqual(bb, 2)
test.strictEqual(cc, 3)
test.strictEqual(dd, 4)
test.strictEqual(ee, 5)
done()
})
.emit('zazzie', 1, 2, 3, 4, 5)
})
test('should be able to pass context to listener', function (done) {
function listener (hi) {
test.strictEqual(hi, 'hello world')
test.strictEqual(this.aaa, 'bbb')
done()
}
var ctx = { aaa: 'bbb' }
var app = dush()
app.on('ctx', listener.bind(ctx))
app.once('ctx', listener.bind(ctx))
app.emit('ctx', 'hello world')
})
test('should context of listener be the listener', function (done) {
function fnc () {
test.strictEqual(typeof this, 'function')
done()
}
app.on('func', fnc)
app.emit('func')
})
test('should be able to `.off` the `.once` listeners (issue #7)', function (
done
) {
var emitter = dush()
var called = 0
function hello () {
called++
}
emitter.once('test', hello)
emitter.emit('test')
emitter.emit('test')
test.strictEqual(called, 1)
emitter.off('test', hello)
emitter.emit('test')
test.strictEqual(called, 1)
done()
})
test('should `.on` work as `.once` if third argument is true', function (done) {
var emitter = dush()
var calls = 0
function fn () {
calls++
}
emitter.on('onetime', fn, true)
emitter.emit('onetime')
test.strictEqual(calls, 1)
done()
})
test('should `.off()` remove all listeners', function (done) {
var app = dush()
var fixture = function () {}
app.on('a', fixture)
app.once('a', fixture)
app.on('a', fixture)
app.on('b', fixture)
app.once('b', fixture)
app.on('c', fixture)
var evts = Object.keys(app._allEvents)
test.strictEqual(evts.length, 3)
test.strictEqual(app._allEvents.a.length, 3)
test.strictEqual(app._allEvents.b.length, 2)
test.strictEqual(app._allEvents.c.length, 1)
app.off()
var allEvents = Object.keys(app._allEvents)
test.strictEqual(allEvents.length, 0)
done()
})
test('should `.off()` be able to differentiate between similar handler functions', function (done) {
var emitter = dush()
var calls = 0
var funcA = function () {
calls++
}
var funcB = function () {
calls++
}
emitter.on('a', funcA)
emitter.on('a', funcB)
emitter.emit('a')
test.strictEqual(calls, 2)
emitter.off('a', funcB)
emitter.emit('a')
test.strictEqual(calls, 3)
emitter.off('a', funcA)
emitter.emit('a')
test.strictEqual(calls, 3)
done()
})