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

describe('during', function() {

    it('during', function(done) {
        var call_order = [];

        var count = 0;
        async.during(
            function (cb) {
                call_order.push(['test', count]);
                cb(null, count < 5);
            },
            function (cb) {
                call_order.push(['iteratee', count]);
                count++;
                cb();
            },
            function (err) {
                assert(err === null, err + " passed instead of 'null'");
                expect(call_order).to.eql([
                    ['test', 0],
                    ['iteratee', 0], ['test', 1],
                    ['iteratee', 1], ['test', 2],
                    ['iteratee', 2], ['test', 3],
                    ['iteratee', 3], ['test', 4],
                    ['iteratee', 4], ['test', 5],
                ]);
                expect(count).to.equal(5);
                done();
            }
        );
    });

    it('doDuring', function(done) {
        var call_order = [];

        var count = 0;
        async.doDuring(
            function (cb) {
                call_order.push(['iteratee', count]);
                count++;
                cb();
            },
            function (cb) {
                call_order.push(['test', count]);
                cb(null, count < 5);
            },
            function (err) {
                assert(err === null, err + " passed instead of 'null'");
                expect(call_order).to.eql([
                    ['iteratee', 0], ['test', 1],
                    ['iteratee', 1], ['test', 2],
                    ['iteratee', 2], ['test', 3],
                    ['iteratee', 3], ['test', 4],
                    ['iteratee', 4], ['test', 5],
                ]);
                expect(count).to.equal(5);
                done();
            }
        );
    });

    it('doDuring - error test', function(done) {
        var error = new Error('asdas');

        async.doDuring(
            function (cb) {
                cb(error);
            },
            function () {},
            function (err) {
                expect(err).to.equal(error);
                done();
            }
        );
    });

    it('doDuring - error iteratee', function(done) {
        var error = new Error('asdas');

        async.doDuring(
            function (cb) {
                cb(null);
            },
            function (cb) {
                cb(error);
            },
            function (err) {
                expect(err).to.equal(error);
                done();
            }
        );
    });
});