summaryrefslogtreecommitdiff
path: root/mocha_test/applyEach.js
blob: ca5ada3d70f4666ca10778316a1a0db5db2c2920 (plain)
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
var async = require('../lib');
var expect = require('chai').expect;
var assert = require('assert');

describe('applyEach', function () {

    it('applyEach', function (done) {
        var call_order = [];
        var one = function (val, cb) {
            expect(val).to.equal(5);
            setTimeout(function () {
                call_order.push('one');
                cb(null, 1);
            }, 12);
        };
        var two = function (val, cb) {
            expect(val).to.equal(5);
            setTimeout(function () {
                call_order.push('two');
                cb(null, 2);
            }, 2);
        };
        var three = function (val, cb) {
            expect(val).to.equal(5);
            setTimeout(function () {
                call_order.push('three');
                cb(null, 3);
            }, 18);
        };
        async.applyEach([one, two, three], 5, function (err, results) {
            assert(err === null, err + " passed instead of 'null'");
            expect(call_order).to.eql(['two', 'one', 'three']);
            expect(results).to.eql([1, 2, 3]);
            done();
        });
    });

    it('applyEachSeries', function (done) {
        var call_order = [];
        var one = function (val, cb) {
            expect(val).to.equal(5);
            setTimeout(function () {
                call_order.push('one');
                cb(null, 1);
            }, 10);
        };
        var two = function (val, cb) {
            expect(val).to.equal(5);
            setTimeout(function () {
                call_order.push('two');
                cb(null, 2);
            }, 5);
        };
        var three = function (val, cb) {
            expect(val).to.equal(5);
            setTimeout(function () {
                call_order.push('three');
                cb(null, 3);
            }, 15);
        };
        async.applyEachSeries([one, two, three], 5, function (err, results) {
            assert(err === null, err + " passed instead of 'null'");
            expect(call_order).to.eql(['one', 'two', 'three']);
            expect(results).to.eql([1, 2, 3]);
            done();
        });
    });

    it('applyEach partial application', function (done) {
        var call_order = [];
        var one = function (val, cb) {
            expect(val).to.equal(5);
            setTimeout(function () {
                call_order.push('one');
                cb(null, 1);
            }, 10);
        };
        var two = function (val, cb) {
            expect(val).to.equal(5);
            setTimeout(function () {
                call_order.push('two');
                cb(null, 2);
            }, 5);
        };
        var three = function (val, cb) {
            expect(val).to.equal(5);
            setTimeout(function () {
                call_order.push('three');
                cb(null, 3);
            }, 15);
        };
        async.applyEach([one, two, three])(5, function (err, results) {
            if (err) throw err;
            expect(call_order).to.eql(['two', 'one', 'three']);
            expect(results).to.eql([1, 2, 3]);
            done();
        });
    });
});