summaryrefslogtreecommitdiff
path: root/test/transform.js
blob: a02b54bad1480291c6ba46a2c2dc6f0a7d1851db (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
var async = require('../lib');
var expect = require('chai').expect;

describe('transform', function() {

    it('transform implictly determines memo if not provided', function(done) {
        async.transform([1,2,3], function(memo, x, v, callback){
            memo.push(x + 1);
            callback();
        }, function(err, result){
            expect(result).to.eql([2, 3, 4]);
            done();
        });
    });

    it('transform async with object memo', function(done) {
        async.transform([1,3,2], {}, function(memo, v, k, callback){
            setTimeout(function() {
                memo[k] = v;
                callback();
            });
        }, function(err, result) {
            expect(err).to.equal(null);
            expect(result).to.eql({
                0: 1,
                1: 3,
                2: 2
            });
            done();
        });
    });

    it('transform iterating object', function(done) {
        async.transform({a: 1, b: 3, c: 2}, function(memo, v, k, callback){
            setTimeout(function() {
                memo[k] = v + 1;
                callback();
            });
        }, function(err, result) {
            expect(err).to.equal(null);
            expect(result).to.eql({a: 2, b: 4, c: 3});
            done();
        });
    });

    it('transform error', function(done) {
        async.transform([1,2,3], function(a, v, k, callback){
            callback('error');
        }, function(err){
            expect(err).to.equal('error');
            done();
        });
    });

    it('transform with two arguments', function(done) {
        try {
            async.transform([1, 2, 3], function (a, v, k, callback) {
                callback();
            });
            done();
        } catch (e) {
            expect.fail();
        }
    });
});