| OLD | NEW |
| (Empty) | |
| 1 var async = require('../lib/async'); |
| 2 var expect = require('chai').expect; |
| 3 |
| 4 describe('compose', function(){ |
| 5 context('all functions succeed', function(){ |
| 6 it('yields the result of the composition of the functions', function(don
e){ |
| 7 var add2 = function (n, cb) { |
| 8 setTimeout(function () { |
| 9 cb(null, n + 2); |
| 10 }); |
| 11 }; |
| 12 var mul3 = function (n, cb) { |
| 13 setTimeout(function () { |
| 14 cb(null, n * 3); |
| 15 }); |
| 16 }; |
| 17 var add1 = function (n, cb) { |
| 18 setTimeout(function () { |
| 19 cb(null, n + 1); |
| 20 }); |
| 21 }; |
| 22 var add2mul3add1 = async.compose(add1, mul3, add2); |
| 23 add2mul3add1(3, function (err, result) { |
| 24 expect(err).to.not.exist; |
| 25 expect(result).to.eql(16); |
| 26 done(); |
| 27 }); |
| 28 }); |
| 29 }); |
| 30 |
| 31 context('a function errors', function(){ |
| 32 it('yields the error and does not call later functions', function(done){ |
| 33 var add1called = false; |
| 34 var mul3error = new Error('mul3 error'); |
| 35 var add2 = function (n, cb) { |
| 36 setTimeout(function () { |
| 37 cb(null, n + 2); |
| 38 }); |
| 39 }; |
| 40 var mul3 = function (n, cb) { |
| 41 setTimeout(function () { |
| 42 cb(mul3error); |
| 43 }); |
| 44 }; |
| 45 var add1 = function (n, cb) { |
| 46 add1called = true; |
| 47 setTimeout(function () { |
| 48 cb(null, n + 1); |
| 49 }); |
| 50 }; |
| 51 var add2mul3add1 = async.compose(add1, mul3, add2); |
| 52 add2mul3add1(3, function (err, result) { |
| 53 expect(err).to.eql(mul3error); |
| 54 expect(result).to.not.exist; |
| 55 expect(add1called).to.be.false; |
| 56 done(); |
| 57 }); |
| 58 }); |
| 59 }); |
| 60 |
| 61 it('calls each function with the binding of the composed function', function
(done){ |
| 62 var context = {}; |
| 63 var add2Context = null; |
| 64 var mul3Context = null; |
| 65 var add2 = function (n, cb) { |
| 66 add2Context = this; |
| 67 setTimeout(function () { |
| 68 cb(null, n + 2); |
| 69 }); |
| 70 }; |
| 71 var mul3 = function (n, cb) { |
| 72 mul3Context = this; |
| 73 setTimeout(function () { |
| 74 cb(null, n * 3); |
| 75 }); |
| 76 }; |
| 77 var add2mul3 = async.compose(mul3, add2); |
| 78 add2mul3.call(context, 3, function (err, result) { |
| 79 expect(err).to.not.exist; |
| 80 expect(result).to.eql(15); |
| 81 expect(add2Context).to.equal(context); |
| 82 expect(mul3Context).to.equal(context); |
| 83 done(); |
| 84 }); |
| 85 }); |
| 86 }); |
| OLD | NEW |