summaryrefslogtreecommitdiff
path: root/test/reduce.js
blob: 1184b525dc0b996257c938f27ba155d625c2d923 (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
var async = require('../lib');
var {expect} = require('chai');
var assert = require('assert');

describe('reduce', () => {

    it('reduce', (done) => {
        var call_order = [];
        async.reduce([1,2,3], 0, (a, x, callback) => {
            call_order.push(x);
            callback(null, a + x);
        }, (err, result) => {
            assert(err === null, err + " passed instead of 'null'");
            expect(result).to.equal(6);
            expect(call_order).to.eql([1,2,3]);
            done();
        });
    });

    it('reduce async with non-reference memo', (done) => {
        async.reduce([1,3,2], 0, (a, x, callback) => {
            setTimeout(() => {callback(null, a + x);}, Math.random()*100);
        }, (err, result) => {
            expect(result).to.equal(6);
            done();
        });
    });

    it('reduce error', (done) => {
        async.reduce([1,2,3], 0, (a, x, callback) => {
            callback('error');
        }, (err) => {
            expect(err).to.equal('error');
        });
        setTimeout(done, 50);
    });

    it('inject alias', (done) => {
        expect(async.inject).to.equal(async.reduce);
        done();
    });

    it('foldl alias', (done) => {
        expect(async.foldl).to.equal(async.reduce);
        done();
    });

    it('reduceRight', (done) => {
        var call_order = [];
        var arr = [1,2,3];
        async.reduceRight(arr, 0, (a, x, callback) => {
            call_order.push(x);
            callback(null, a + x);
        }, (err, result) => {
            expect(result).to.equal(6);
            expect(call_order).to.eql([3,2,1]);
            expect(arr).to.eql([1,2,3]);
            done();
        });
    });

    it('foldr alias', (done) => {
        expect(async.foldr).to.equal(async.reduceRight);
        done();
    });
});