-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
58 lines (51 loc) · 1.3 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
var assert = require('assert')
var Aggregate = require('./')
var noop = function () {}
//
// Test simple event
//
var a = {}
var evt = {foo: 'bar'}
Aggregate.setup(a, noop)
assert.equal(Aggregate.drain(a).length, 0)
Aggregate.event(a, noop, evt)
var evts = Aggregate.drain(a)
assert.equal(evts.length, 1)
assert.equal(evts[0], evt)
//
// Test reducer, this binding
//
var reducer = function (s, evt) { s.calls += 1 }
var obj = {calls: 0, args: []}
Aggregate.setup(obj, reducer)
assert.equal(Object.keys(obj).length, 2)
assert.equal(obj.calls, 0)
var first = {foo: 'bar'}
// queues objects
Aggregate.event(obj, reducer, first)
assert.equal(obj.calls, 1)
// queues strings
Aggregate.event(obj, reducer, 'second')
assert.equal(obj.calls, 2)
// queues undefined
Aggregate.event(obj, reducer)
assert.equal(obj.calls, 3)
// .drain returns all queued events
var events2 = Aggregate.drain(obj)
assert.equal(Array.isArray(events2), true)
assert.equal(events2[0], first)
assert.equal(events2[1], 'second')
assert.equal(events2[2], undefined)
// reducer arguments
var foo = {}
var event = {foo: 'bar'}
var called = 0
function reducer2 (s, evt) {
called += 1
assert.equal(arguments.length, 2)
assert.equal(evt, event)
assert.equal(s, foo)
}
Aggregate.setup(foo)
Aggregate.event(foo, reducer2, event)
assert.equal(called, 1)