async.js
1.6 KB
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
/* jshint expr:true */
/* global describe:true, beforeEach:true, afterEach:true, it:true */
var async = require('../lib/async')
var should = require('should')
var sinon = require('sinon')
describe('Async helper', function () {
var err = {}
var val = {}
var ch
var fn
beforeEach(function () {
ch = sinon.stub().returns(function (cb) { cb() })
fn = sinon.stub().yields(err, val)
})
it(
'should return a function with an arity of 1',
function () {
var thunk = async(ch, fn)
thunk.should.be.a.Function
thunk.length.should.be.exactly(1)
}
)
it(
'should call fn with args plus a callback',
function () {
async(ch, fn, 1, 2, 3, 'foo')
var argsWithoutCb = fn.firstCall.args.slice(0, -1)
argsWithoutCb.should.eql([1, 2, 3, 'foo'])
}
)
it(
'should call a method of an object with the third argument as the name',
function () {
var ob = { foo: fn }
async(ch, ob, 'foo', 1, 2, 3)
var argsWithoutCb = fn.firstCall.args.slice(0, -1)
argsWithoutCb.should.eql([1, 2, 3])
fn.firstCall.calledOn(ob).should.be.true
}
)
it(
'should call channel with arguments of the async function callback',
function () {
async(ch, fn)
ch.firstCall.args.length.should.be.exactly(2)
ch.firstCall.args[0].should.be.exactly(err)
ch.firstCall.args[1].should.be.exactly(val)
}
)
it(
'should call callback given to returned function',
function (done) {
var cb = sinon.spy()
async(ch, fn)(cb)
setImmediate(function () {
cb.callCount.should.be.exactly(1)
done()
})
}
)
})