-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
166 lines (125 loc) · 3.71 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
/* istanbul ignore next */
describe('emits', function () {
'use strict';
var EventEmitter = require('events').EventEmitter
, assume = require('assume')
, emits = require('./')
, example;
function Example() {
EventEmitter.call(this);
}
require('util').inherits(Example, EventEmitter);
Example.prototype.emits = emits;
beforeEach(function () {
example = new Example();
});
it('is exported as a function', function () {
assume(emits).is.a('function');
});
it('has the context of the prototype', function (next) {
var fn = example.emits('data');
example.on('data', function () {
assume(this).equals(example);
next();
});
assume(fn()).is.true();
});
it('merges the arguments', function (next) {
var fn = example.emits('data', 'foo');
example.on('data', function (foo, bar) {
assume(foo).equals('foo');
assume(bar).equals('bar');
next();
});
assume(fn('bar')).is.true();
});
it('returns false when there are no listeners', function () {
var fn = example.emits('data');
assume(fn()).is.false();
});
it('returns true when there are listeners', function () {
var fn = example.emits('data');
example.on('data', function () {});
assume(fn()).is.true();
});
it('calls the parser function even when there are no listeners', function (next) {
var fn = example.emits('data', function (done) {
done();
next();
});
assume(fn()).is.true();
});
it('can prevent the event from being fired', function (next) {
var fn = example.emits('data', function () {
next();
});
example.on('data', function () {
throw new Error('I should never be called');
});
assume(fn()).is.true();
});
it('returns only the supplied arguments when null is returned', function (next) {
var fn = example.emits('data', 'bar', function (done) {
done(undefined, null);
});
example.on('data', function (bar, foo) {
assume(foo).equals(undefined);
assume(bar).equals('bar');
next();
});
assume(fn('foo')).is.true();
});
it('returns all received arguments when undefined is returned', function (next) {
var fn = example.emits('data', 'sup', function (done) {
done();
});
example.on('data', function (sup, foo, bar) {
assume(arguments).has.length(3);
assume(sup).equals('sup');
assume(foo).equals('foo');
assume(bar).equals('bar');
next();
});
assume(fn('foo', 'bar')).is.true();
});
it('can modify the data', function (next) {
var fn = example.emits('data', 'sup', function (done) {
done(undefined, 'bar');
});
example.on('data', function (sup, foo) {
assume(sup).equals('sup');
assume(foo).equals('bar');
next();
});
assume(fn('foo')).is.true();
});
it('supports async execution', function (next) {
var fn = example.emits('data', 'sup', function (done) {
setTimeout(function () {
done(undefined, 'bar');
}, 100);
});
example.on('data', function (sup, foo, bar) {
assume(bar).equals(undefined);
assume(sup).equals('sup');
assume(foo).equals('bar');
next();
});
assume(fn('foo')).is.true();
});
it('emits an error when async execution fails with an error', function (next) {
var fn = example.emits('data', 'sup', function (done) {
setTimeout(function () {
done(new Error('lol failure'), 'bar');
}, 100);
});
example.on('data', function () {
throw new Error('I should never be called');
});
example.on('error', function (err) {
assume(err.message).equals('lol failure');
next();
});
assume(fn('foo')).is.true();
});
});