| 1 |
"use strict"; |
| 2 |
|
| 3 |
var deferred = require("../../../deferred") |
| 4 |
, reject = deferred.reject; |
| 5 |
|
| 6 |
module.exports = function (t) { |
| 7 |
var x = {}, y = {}, z = {}, e = new Error("Error"); |
| 8 |
|
| 9 |
return { |
| 10 |
"Empty": { |
| 11 |
"": function (a, d) { |
| 12 |
t.call([])(function (result) { a.deep(result, []); }, a.never).done(d, d); |
| 13 |
}, |
| 14 |
"Callback": function (a, d) { |
| 15 |
t.call([], a.never)(function (result) { a.deep(result, []); }, a.never).done(d, d); |
| 16 |
} |
| 17 |
}, |
| 18 |
"One": { |
| 19 |
Value: { |
| 20 |
"": function (a, d) { |
| 21 |
t.call([x])(function (result) { a.deep(result, [x]); }, a.never).done(d, d); |
| 22 |
}, |
| 23 |
"Callback": function (a, d) { |
| 24 |
var list = [x]; |
| 25 |
t.call( |
| 26 |
list, |
| 27 |
function (arg, index, target) { |
| 28 |
a(arg, x, "Argument"); |
| 29 |
a(index, 0, "Index"); |
| 30 |
a(target, list, "Target"); |
| 31 |
a(this, x, "Context"); |
| 32 |
return y; |
| 33 |
}, |
| 34 |
x |
| 35 |
)(function (result) { a.deep(result, [y]); }, a.never).done(d, d); |
| 36 |
} |
| 37 |
}, |
| 38 |
Promise: { |
| 39 |
"": function (a, d) { |
| 40 |
t.call([deferred(x)])(function (result) { a.deep(result, [x]); }, a.never).done( |
| 41 |
d, d |
| 42 |
); |
| 43 |
}, |
| 44 |
"Callback": function (a, d) { |
| 45 |
t.call([deferred(x)], function (arg) { |
| 46 |
a(arg, x, "Argument"); |
| 47 |
return y; |
| 48 |
})(function (result) { a.deep(result, [y]); }, a.never).done(d, d); |
| 49 |
} |
| 50 |
} |
| 51 |
}, |
| 52 |
"Many": { |
| 53 |
"No callback": { |
| 54 |
"Error": function (a, d) { |
| 55 |
t.call([x, y, deferred(x), reject(e), z])(a.never, function (res) { |
| 56 |
a(res, e); |
| 57 |
d(); |
| 58 |
}); |
| 59 |
}, |
| 60 |
"Values & Promises": function (a, d) { |
| 61 |
t.call([x, y, deferred(x), z, deferred(y)])(function (res) { |
| 62 |
a.deep(res, [x, y, x, z, y]); |
| 63 |
}, a.never).done(d, d); |
| 64 |
}, |
| 65 |
"Error promise": function (a, d) { |
| 66 |
t.call([x, y, deferred(e), z, deferred(y)])( |
| 67 |
a.never, function (res) { a(res, e); }, a.never |
| 68 |
).done(d, d); |
| 69 |
} |
| 70 |
}, |
| 71 |
"Callback": { |
| 72 |
"Error": function (a, d) { |
| 73 |
var count = 0; |
| 74 |
t.call([x, y, deferred(x), z], function () { |
| 75 |
if (count++) { |
| 76 |
a.never(); |
| 77 |
} |
| 78 |
throw e; |
| 79 |
})(a.never, function (res) { a(res, e); }).done(d, d); |
| 80 |
}, |
| 81 |
"Error via input": function (a, d) { |
| 82 |
t.call([x, y, deferred(e), z], function () { return x; })(a.never, function ( |
| 83 |
res |
| 84 |
) { a(res, e); }).done(d, d); |
| 85 |
}, |
| 86 |
"Values & Promises": function (a, d) { |
| 87 |
t.call([1, deferred(2), 3, deferred(4), 5], function (val) { |
| 88 |
return val * val; |
| 89 |
})(function (res) { a.deep(res, [1, 4, 9, 16, 25]); }, a.never).done(d, d); |
| 90 |
}, |
| 91 |
"Values & Promises, through promise": function (a, d) { |
| 92 |
t.call([1, deferred(2), 3, deferred(4), 5], function (val) { |
| 93 |
return deferred(val * val); |
| 94 |
})(function (res) { a.deep(res, [1, 4, 9, 16, 25]); }, a.never).done(d, d); |
| 95 |
} |
| 96 |
} |
| 97 |
}, |
| 98 |
"Resolve not via then": function (a) { |
| 99 |
// With v0.3.0 we introduced a bug - resolve of map in some cases was |
| 100 |
// called within callback passed to then, therefore any following errors |
| 101 |
// in given event loop were silent - this test makes sure it's not the |
| 102 |
// case anymore |
| 103 |
|
| 104 |
var d = deferred(); |
| 105 |
t.call([d.promise]).done(function () { throw new Error("ERROR"); }); |
| 106 |
a.throws(d.resolve); |
| 107 |
}, |
| 108 |
"Return rejected promise in callback": function (a) { |
| 109 |
var e = new Error("Some error"); |
| 110 |
t.call([1, 2], function () { return reject(e); }).done(a.never, function (err) { |
| 111 |
a(err, e); |
| 112 |
}); |
| 113 |
} |
| 114 |
}; |
| 115 |
}; |