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

describe('ensureAsync', function() {
    var passContext = function(cb) {
        cb(this);
    };

    it('defer sync functions', function(done) {
        var sync = true;
        async.ensureAsync(function (arg1, arg2, cb) {
            expect(arg1).to.equal(1);
            expect(arg2).to.equal(2);
            cb(null, 4, 5);
        })(1, 2, function (err, arg4, arg5) {
            expect(err).to.equal(null);
            expect(arg4).to.equal(4);
            expect(arg5).to.equal(5);
            assert(!sync, 'callback called on same tick');
            done();
        });
        sync = false;
    });

    it('do not defer async functions', function(done) {
        var sync = false;
        async.ensureAsync(function (arg1, arg2, cb) {
            expect(arg1).to.equal(1);
            expect(arg2).to.equal(2);
            async.setImmediate(function () {
                sync = true;
                cb(null, 4, 5);
                sync = false;
            });
        })(1, 2, function (err, arg4, arg5) {
            expect(err).to.equal(null);
            expect(arg4).to.equal(4);
            expect(arg5).to.equal(5);
            assert(sync, 'callback called on next tick');
            done();
        });
    });

    it('double wrapping', function(done) {
        var sync = true;
        async.ensureAsync(async.ensureAsync(function (arg1, arg2, cb) {
            expect(arg1).to.equal(1);
            expect(arg2).to.equal(2);
            cb(null, 4, 5);
        }))(1, 2, function (err, arg4, arg5) {
            expect(err).to.equal(null);
            expect(arg4).to.equal(4);
            expect(arg5).to.equal(5);
            assert(!sync, 'callback called on same tick');
            done();
        });
        sync = false;
    });


    it('should propely bind context to the wrapped function', function(done) {

        // call bind after wrapping with ensureAsync
        var context = {context: "post"};
        var postBind = async.ensureAsync(passContext);
        postBind = postBind.bind(context);
        postBind(function(ref) {
            expect(ref).to.equal(context);
            done();
        });
    });

    it('should not override the bound context of a function when wrapping', function(done) {

        // call bind before wrapping with ensureAsync
        var context = {context: "pre"};
        var preBind = passContext.bind(context);
        preBind = async.ensureAsync(preBind);
        preBind(function(ref) {
            expect(ref).to.equal(context);
            done();
        });
    });
});