|
| 1 | +var async = require('../lib'); |
| 2 | +var expect = require('chai').expect; |
| 3 | +var assert = require('assert'); |
| 4 | + |
| 5 | +describe('try', function () { |
| 6 | + it('no callback', function () { |
| 7 | + async.tryEach([]); |
| 8 | + }); |
| 9 | + it('empty', function (done) { |
| 10 | + async.tryEach([], function (err, results) { |
| 11 | + expect(err).to.equal(null); |
| 12 | + expect(results).to.eql(undefined); |
| 13 | + done(); |
| 14 | + }); |
| 15 | + }); |
| 16 | + it('one task, multiple results', function (done) { |
| 17 | + var RESULTS = ['something', 'something2']; |
| 18 | + async.tryEach([ |
| 19 | + function (callback) { |
| 20 | + callback(null, RESULTS[0], RESULTS[1]); |
| 21 | + } |
| 22 | + ], function (err, results) { |
| 23 | + expect(err).to.equal(null); |
| 24 | + expect(results).to.eql(RESULTS); |
| 25 | + done(); |
| 26 | + }); |
| 27 | + }); |
| 28 | + it('one task', function (done) { |
| 29 | + var RESULT = 'something'; |
| 30 | + async.tryEach([ |
| 31 | + function (callback) { |
| 32 | + callback(null, RESULT); |
| 33 | + } |
| 34 | + ], function (err, results) { |
| 35 | + expect(err).to.equal(null); |
| 36 | + expect(results).to.eql(RESULT); |
| 37 | + done(); |
| 38 | + }); |
| 39 | + }); |
| 40 | + it('two tasks, one failing', function (done) { |
| 41 | + var RESULT = 'something'; |
| 42 | + async.tryEach([ |
| 43 | + function (callback) { |
| 44 | + callback(new Error('Failure')); |
| 45 | + }, |
| 46 | + function (callback) { |
| 47 | + callback(null, RESULT); |
| 48 | + } |
| 49 | + ], function (err, results) { |
| 50 | + expect(err).to.equal(null); |
| 51 | + expect(results).to.eql(RESULT); |
| 52 | + done(); |
| 53 | + }); |
| 54 | + }); |
| 55 | + it('two tasks, both failing', function (done) { |
| 56 | + var ERROR_RESULT = new Error('Failure2'); |
| 57 | + async.tryEach([ |
| 58 | + function (callback) { |
| 59 | + callback(new Error('Should not stop here')); |
| 60 | + }, |
| 61 | + function (callback) { |
| 62 | + callback(ERROR_RESULT); |
| 63 | + } |
| 64 | + ], function (err, results) { |
| 65 | + expect(err).to.equal(ERROR_RESULT); |
| 66 | + expect(results).to.eql(undefined); |
| 67 | + done(); |
| 68 | + }); |
| 69 | + }); |
| 70 | + it('two tasks, non failing', function (done) { |
| 71 | + var RESULT = 'something'; |
| 72 | + async.tryEach([ |
| 73 | + function (callback) { |
| 74 | + callback(null, RESULT); |
| 75 | + }, |
| 76 | + function () { |
| 77 | + assert.fail('Should not been called'); |
| 78 | + }, |
| 79 | + ], function (err, results) { |
| 80 | + expect(err).to.equal(null); |
| 81 | + expect(results).to.eql(RESULT); |
| 82 | + done(); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
| 86 | + |
0 commit comments