forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapasync_test.js
75 lines (72 loc) · 2.44 KB
/
wrapasync_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
var asyncFunction1 = function (x, cb) {
setTimeout(function () { cb(null, x); }, 5);
};
var asyncFunction2 = function (x, opt, cb) {
if (! cb && opt instanceof Function) {
cb = opt;
opt = null;
}
asyncFunction1(x, cb);
};
var asyncFunction3 = function (opt, cb) {
if (! cb && opt instanceof Function) {
cb = opt;
opt = null;
}
asyncFunction1(3, cb);
};
var asyncFunction4 = function (cb) {
asyncFunction1(3, cb);
};
var wrapped1 = Meteor._wrapAsync(asyncFunction1);
var wrapped2 = Meteor._wrapAsync(asyncFunction2);
var wrapped3 = Meteor._wrapAsync(asyncFunction3);
var wrapped4 = Meteor._wrapAsync(asyncFunction4);
Tinytest.add("environment - wrapAsync sync", function (test) {
// one required arg and callback
test.equal(wrapped1(3), 3);
test.equal(wrapped1(3, undefined), 3);
// one required arg, optional second arg, callback
test.equal(wrapped2(3), 3);
test.equal(wrapped2(3, {foo: "bar"}), 3);
test.equal(wrapped2(3, undefined, undefined), 3);
test.equal(wrapped2(3, {foo: "bar"}, undefined), 3);
// optional first arg, callback
test.equal(wrapped3(3), 3);
test.equal(wrapped3(3, undefined), 3);
test.equal(wrapped3(), 3);
test.equal(wrapped3(undefined), 3);
// only callback
test.equal(wrapped4(), 3);
test.equal(wrapped4(undefined), 3);
});
testAsyncMulti("environment - wrapAsync async", [
function (test, expect) {
var cb = function (result) {
return expect(null, result);
};
// one required arg and callback
test.equal(wrapped1(3, cb(3)), undefined);
// one required arg, optional second arg, callback
test.equal(wrapped2(3, cb(3)), undefined);
test.equal(wrapped2(3, {foo: "bar"}, cb(3)), undefined);
test.equal(wrapped2(3, undefined, cb(3)), undefined);
// optional first arg, callback
test.equal(wrapped3(3, cb(3)), undefined);
test.equal(wrapped3(cb(3)), undefined);
test.equal(wrapped3(undefined, cb(3)), undefined);
// only callback
test.equal(wrapped4(cb(3)), undefined);
}
]);
Tinytest.addAsync("environment - wrapAsync callback is "
"in fiber", function (test, onComplete) {
var cb = function (err, result) {
if (Meteor.isServer) {
var Fiber = Npm.require('fibers');
test.isTrue(Fiber.current);
}
onComplete();
};
wrapped1(3, cb);
});