summaryrefslogtreecommitdiff
path: root/mocha_test/autoInject.js
blob: 9c2d1fbe4e3308dd08a5519a20ee2fa3e89750ad (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
var async = require('../lib');
var expect = require('chai').expect;

describe('autoInject', function () {

    it("basics", function (done) {
        var callOrder = [];
        async.autoInject({
            task1: function(task2, callback){
                expect(task2).to.equal(2);
                setTimeout(function(){
                    callOrder.push('task1');
                    callback(null, 1);
                }, 25);
            },
            task2: function(callback){
                setTimeout(function(){
                    callOrder.push('task2');
                    callback(null, 2);
                }, 50);
            },
            task3: function(task2, callback){
                expect(task2).to.equal(2);
                callOrder.push('task3');
                callback(null, 3);
            },
            task4: function(task1, task2, callback){
                expect(task1).to.equal(1);
                expect(task2).to.equal(2);
                callOrder.push('task4');
                callback(null, 4);
            },
            task5: function(task2, callback){
                expect(task2).to.equal(2);
                setTimeout(function(){
                    callOrder.push('task5');
                    callback(null, 5);
                }, 0);
            },
            task6: function(task2, callback){
                expect(task2).to.equal(2);
                callOrder.push('task6');
                callback(null, 6);
            }
        },
        function(err, results){
            expect(results.task6).to.equal(6);
            expect(callOrder).to.eql(['task2','task3','task6','task5','task1','task4']);
            done();
        });
    });

    it('should work with array tasks', function (done) {
        var callOrder = [];

        async.autoInject({
            task1: function (cb) {
                callOrder.push('task1');
                cb(null, 1);
            },
            task2: ['task3', function ( task3 , cb ) {
                expect(task3).to.equal(3);
                callOrder.push('task2');
                cb(null, 2);
            }],
            task3: function (cb) {
                callOrder.push('task3');
                cb(null, 3);
            }
        }, function () {
            expect(callOrder).to.eql(['task1','task3','task2']);
            done();
        });
    });

    it('should handle array tasks with just a function', function (done) {
        async.autoInject({
            a: [function (cb) {
                cb(null, 1);
            }],
            b: ["a", function (a, cb) {
                expect(a).to.equal(1);
                cb();
            }]
        }, done);
    });

    it('should throw error for function without explicit parameters', function (done) {
        try {
            async.autoInject({
                a: function (){}
            });
        } catch (e) {
            // It's ok. It detected a void function
            return done();
        }

        // If didn't catch error, then it's a failed test
        done(true)
    });

    var arrowSupport = true;
    try {
        new Function('x => x');
    } catch (e) {
        arrowSupport = false;
    }

    if (arrowSupport) {
        // Needs to be run on ES6 only

        /* eslint {no-eval: 0}*/
        eval("(function() {                                                 " +
             "    it('should work with es6 arrow syntax', function (done) { " +
             "        async.autoInject({                                    " +
             "            task1: (cb)           => cb(null, 1),             " +
             "            task2: ( task3 , cb ) => cb(null, 2),             " +
             "            task3: cb             => cb(null, 3)              " +
             "        }, (err, results) => {                                " +
             "            expect(results.task1).to.equal(1);                " +
             "            expect(results.task3).to.equal(3);                " +
             "            done();                                           " +
             "        });                                                   " +
             "    });                                                       " +
             "})                                                            "
        )();
    }


    var defaultSupport = true;
    try {
        eval('function x(y = 1){ return y }');
    }catch (e){
        defaultSupport = false;
    }

    if(arrowSupport && defaultSupport){
        // Needs to be run on ES6 only

        /* eslint {no-eval: 0}*/
        eval("(function() {                                                 " +
             "    it('should work with es6 obj method syntax', function (done) { " +
             "        async.autoInject({                                    " +
             "            task1 (cb){             cb(null, 1) },            " +
             "            task2 ( task3 , cb ) {  cb(null, 2) },            " +
             "            task3 (cb) {            cb(null, 3) },            " +
             "            task4 ( task2 , cb ) {  cb(null) },               " +
             "            task5 ( task4 = 4 , cb ) { cb(null, task4 + 1) }  " +
             "        }, (err, results) => {                                " +
             "            expect(results.task1).to.equal(1);                " +
             "            expect(results.task3).to.equal(3);                " +
             "            expect(results.task4).to.equal(undefined);        " +
             "            expect(results.task5).to.equal(5);                " +
             "            done();                                           " +
             "        });                                                   " +
             "    });                                                       " +
             "})                                                            "
        )();
    }
});