-
Notifications
You must be signed in to change notification settings - Fork 9
/
test.js
232 lines (201 loc) · 5.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
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
'use strict';
var assert = require('assert');
var runAsync = require('./index');
describe('runAsync', function () {
it('run synchronous method', function (done) {
var ranAsync = false;
var aFunc = function () {
return 'pass1';
};
runAsync(aFunc, function (err, val) {
assert.ifError(err);
assert(ranAsync);
assert.equal(val, 'pass1');
done();
})();
ranAsync = true;
});
it('run asynchronous method', function (done) {
var aFunc = function () {
var returns = this.async();
setImmediate(returns.bind(null, null, 'pass2'));
};
runAsync(aFunc, function (err, val) {
assert.ifError(err);
assert.equal(val, 'pass2');
done();
})();
});
it('pass arguments', function (done) {
var aFunc = function (a, b) {
assert.equal(a, 1);
assert.equal(b, 'bar');
return 'pass1';
};
runAsync(aFunc, function (err, val) {
assert.ifError(err);
done();
})(1, 'bar');
});
it('allow only callback once', function (done) {
var aFunc = function () {
var returns = this.async();
returns();
returns();
};
runAsync(aFunc, function (err, val) {
assert.ifError(err);
done();
})();
});
it('handles promises', function (done) {
var fn = function () {
return new Promise(function (resolve, reject) {
setImmediate(function () {
resolve('as promised!');
});
});
};
runAsync(fn, function (err, val) {
assert.ifError(err);
assert.equal('as promised!', val);
done();
})();
});
it('throwing synchronously passes error to callback', function (done) {
var throws = function () {
throw new Error('sync error');
};
runAsync(throws, function (err, val) {
assert(err);
assert.equal(err.message, 'sync error');
done();
})();
});
it('rejecting a promise passes error to callback', function (done) {
var rejects = function () {
return new Promise(function (resolve, reject) {
setImmediate(function () {
reject(new Error('broken promise'));
});
});
};
runAsync(rejects, function (err, val) {
assert(err);
assert.equal(err.message, 'broken promise');
done();
})();
});
it('returns a promise that is resolved', function (done) {
var returns = function () {
return 'hello';
};
runAsync(returns)().then(function (result) {
assert.equal(result, 'hello');
done();
});
});
it('returns a promise that is rejected', function (done) {
var throws = function () {
throw new Error('sync error');
};
runAsync(throws)().catch(function (reason) {
assert.equal(reason.message, 'sync error');
done();
});
});
it('handles async functions', function (done) {
var fn = async function () {
return 'as promised!';
};
runAsync(fn, function (err, val) {
assert.ifError(err);
assert.equal('as promised!', val);
done();
})();
});
it('ignores async callback outside original function context', function (done) {
var outsideContext = false;
var outsideContextCallback = async function () {
outsideContext = true;
this.async()(undefined, 'not as promised!');
};
var fn = async function () {
var self = this;
setTimeout(function () {
outsideContextCallback.call(self);
}, 100);
return new Promise(function (resolve, reject) {
setTimeout(function () {
outsideContext = false;
resolve('as promised!');
}, 500);
});
};
runAsync(fn, function (err, val) {
assert.equal(false, outsideContext);
assert.ifError(err);
assert.equal('as promised!', val);
done();
})();
});
it('handles custom done factory with not bound function', function (done) {
var fn = function () {
const cb = this.customAsync();
setImmediate(function () {
cb(null, 'value');
});
};
runAsync(fn, 'customAsync')().then(() => done());
});
it('handles bound function', function (done) {
var fn = function () {
const cb = this.async();
if (this.bar === 'bar') {
setImmediate(function () {
cb(null, 'value');
});
} else {
cb(new Error('not bount'));
}
};
runAsync(fn).call({ bar: 'bar' }).then(() => done());
});
});
describe('runAsync.cb', function () {
it('handles callback parameter', function (done) {
var fn = function (cb) {
setImmediate(function () {
cb(null, 'value');
});
};
runAsync.cb(fn, function (err, val) {
assert.ifError(err);
assert.equal('value', val);
done();
})();
});
it('run synchronous method', function (done) {
var ranAsync = false;
var aFunc = function () {
return 'pass1';
};
runAsync.cb(aFunc, function (err, val) {
assert.ifError(err);
assert(ranAsync);
assert.equal(val, 'pass1');
done();
})();
ranAsync = true;
});
it('handles a returned promise', function (done) {
var aFunc = function (a) {
return Promise.resolve('foo' + a);
};
runAsync.cb(aFunc, function(err, result) {
assert.ifError(err);
assert.equal(result, 'foobar');
done();
})('bar');
});
});