| 1 |
|
| 2 |
var EventEmitter = require('events').EventEmitter |
| 3 |
var assert = require('assert') |
| 4 |
|
| 5 |
var first = require('..') |
| 6 |
|
| 7 |
describe('first', function () { |
| 8 |
var ee1 = new EventEmitter() |
| 9 |
var ee2 = new EventEmitter() |
| 10 |
var ee3 = new EventEmitter() |
| 11 |
|
| 12 |
it('should require array argument', function () { |
| 13 |
assert.throws(first.bind()) |
| 14 |
assert.throws(first.bind(null, 'string')) |
| 15 |
assert.throws(first.bind(null, 42)) |
| 16 |
assert.throws(first.bind(null, {})) |
| 17 |
}) |
| 18 |
|
| 19 |
it('should require array of arrays argument', function () { |
| 20 |
assert.throws(first.bind(null, [0])) |
| 21 |
assert.throws(first.bind(null, ['string'])) |
| 22 |
assert.throws(first.bind(null, [[ee1], 'string'])) |
| 23 |
}) |
| 24 |
|
| 25 |
it('should emit the first event', function (done) { |
| 26 |
first([ |
| 27 |
[ee1, 'a', 'b', 'c'], |
| 28 |
[ee2, 'a', 'b', 'c'], |
| 29 |
[ee3, 'a', 'b', 'c'], |
| 30 |
], function (err, ee, event, args) { |
| 31 |
assert.ifError(err) |
| 32 |
assert.equal(ee, ee2) |
| 33 |
assert.equal(event, 'b') |
| 34 |
assert.deepEqual(args, [1, 2, 3]) |
| 35 |
done() |
| 36 |
}) |
| 37 |
|
| 38 |
ee2.emit('b', 1, 2, 3) |
| 39 |
}) |
| 40 |
|
| 41 |
it('it should return an error if event === error', function (done) { |
| 42 |
first([ |
| 43 |
[ee1, 'error', 'b', 'c'], |
| 44 |
[ee2, 'error', 'b', 'c'], |
| 45 |
[ee3, 'error', 'b', 'c'], |
| 46 |
], function (err, ee, event, args) { |
| 47 |
assert.equal(err.message, 'boom') |
| 48 |
assert.equal(ee, ee3) |
| 49 |
assert.equal(event, 'error') |
| 50 |
done() |
| 51 |
}) |
| 52 |
|
| 53 |
ee3.emit('error', new Error('boom')) |
| 54 |
}) |
| 55 |
|
| 56 |
it('should cleanup after itself', function (done) { |
| 57 |
first([ |
| 58 |
[ee1, 'a', 'b', 'c'], |
| 59 |
[ee2, 'a', 'b', 'c'], |
| 60 |
[ee3, 'a', 'b', 'c'], |
| 61 |
], function (err, ee, event, args) { |
| 62 |
assert.ifError(err) |
| 63 |
;[ee1, ee2, ee3].forEach(function (ee) { |
| 64 |
['a', 'b', 'c'].forEach(function (event) { |
| 65 |
assert(!ee.listeners(event).length) |
| 66 |
}) |
| 67 |
}) |
| 68 |
done() |
| 69 |
}) |
| 70 |
|
| 71 |
ee1.emit('a') |
| 72 |
}) |
| 73 |
|
| 74 |
it('should return a thunk', function (done) { |
| 75 |
var thunk = first([ |
| 76 |
[ee1, 'a', 'b', 'c'], |
| 77 |
[ee2, 'a', 'b', 'c'], |
| 78 |
[ee3, 'a', 'b', 'c'], |
| 79 |
]) |
| 80 |
thunk(function (err, ee, event, args) { |
| 81 |
assert.ifError(err) |
| 82 |
assert.equal(ee, ee2) |
| 83 |
assert.equal(event, 'b') |
| 84 |
assert.deepEqual(args, [1, 2, 3]) |
| 85 |
done() |
| 86 |
}) |
| 87 |
|
| 88 |
ee2.emit('b', 1, 2, 3) |
| 89 |
}) |
| 90 |
|
| 91 |
it('should not emit after thunk.cancel()', function (done) { |
| 92 |
var thunk = first([ |
| 93 |
[ee1, 'a', 'b', 'c'], |
| 94 |
[ee2, 'a', 'b', 'c'], |
| 95 |
[ee3, 'a', 'b', 'c'], |
| 96 |
]) |
| 97 |
thunk(function () { |
| 98 |
assert.ok(false) |
| 99 |
}) |
| 100 |
|
| 101 |
thunk.cancel() |
| 102 |
|
| 103 |
ee2.emit('b', 1, 2, 3) |
| 104 |
|
| 105 |
setTimeout(done, 10) |
| 106 |
}) |
| 107 |
|
| 108 |
it('should cleanup after thunk.cancel()', function (done) { |
| 109 |
var thunk = first([ |
| 110 |
[ee1, 'a', 'b', 'c'], |
| 111 |
[ee2, 'a', 'b', 'c'], |
| 112 |
[ee3, 'a', 'b', 'c'], |
| 113 |
]) |
| 114 |
|
| 115 |
thunk.cancel() |
| 116 |
|
| 117 |
;[ee1, ee2, ee3].forEach(function (ee) { |
| 118 |
['a', 'b', 'c'].forEach(function (event) { |
| 119 |
assert(!ee.listeners(event).length) |
| 120 |
}) |
| 121 |
}) |
| 122 |
done() |
| 123 |
}) |
| 124 |
}) |