-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
108 lines (108 loc) · 3.26 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
const sinon = require('sinon');
const assert = require('better-assert');
const equal = require('deep-eql');
const inspect = require('util').inspect;
const format = require('util').format;
const debug = false;
const log = debug ? console.log.bind(console) : function () {
};
const EventEmiter = require('./after-events.js');
describe('After Event Emitter', function () {
var EE;
beforeEach(function () {
log();
EE = EventEmiter();
});
it('works as an event emitter.', function (done) {
EE.on('x', function (arg1, arg2) {
assert(arg1 === true);
assert(arg2 === false);
done();
});
EE.emit('x', true, false);
});
it('does not throw on non-existent events.', function (done) {
EE.emit('y');
done();
});
describe('#after', function () {
it('takes a function, which it calls after the listener returns.', function (done) {
EE.on('x', function () {
return true;
});
EE.after(function (err, ret, emitted, arg1, arg2) {
assert(err === undefined);
assert(ret === true);
assert(emitted === 'x');
assert(arg1 === true);
assert(arg2 === false);
done();
});
EE.emit('x', true, false);
});
it('passes the error to err if an error is thrown', function (done) {
const error = new Error();
EE.on('x', function () {
throw error;
});
EE.after(function (err, ret, emitted) {
assert(err === error);
assert(ret === undefined);
done();
});
EE.emit('x');
});
it('can take multiple functions, and call them in order', function (done) {
var callCount = 0;
EE.on('x', function () {
return true;
});
EE.after(function (err, ret, emitted) {
try {
assert(callCount === 0);
callCount += 1;
} catch (e) {
done(e);
}
});
EE.after(function (err, ret, emitted) {
try {
assert(callCount === 1);
done();
} catch (e) {
done(e);
}
});
EE.emit('x');
});
});
it('performs each callback in isolation', function (done) {
var error = new Error();
var result = {};
EE.on('x', function () {
throw error;
});
EE.on('x', function () {
return result;
});
var callCount = 0;
var errCount = 0;
var resCount = 0;
EE.after(function state(err, ret, emitted) {
callCount += 1;
if (err) {
errCount += 1;
}
if (ret) {
resCount += 1;
}
if (callCount === 2) {
assert(errCount === 1);
assert(resCount === 1);
done();
}
});
EE.emit('x');
});
});
//# sourceMappingURL=../sourcemaps/test.js.map