async.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* jshint expr:true */
  2. /* global describe:true, beforeEach:true, afterEach:true, it:true */
  3. var async = require('../lib/async')
  4. var should = require('should')
  5. var sinon = require('sinon')
  6. describe('Async helper', function () {
  7. var err = {}
  8. var val = {}
  9. var ch
  10. var fn
  11. beforeEach(function () {
  12. ch = sinon.stub().returns(function (cb) { cb() })
  13. fn = sinon.stub().yields(err, val)
  14. })
  15. it(
  16. 'should return a function with an arity of 1',
  17. function () {
  18. var thunk = async(ch, fn)
  19. thunk.should.be.a.Function
  20. thunk.length.should.be.exactly(1)
  21. }
  22. )
  23. it(
  24. 'should call fn with args plus a callback',
  25. function () {
  26. async(ch, fn, 1, 2, 3, 'foo')
  27. var argsWithoutCb = fn.firstCall.args.slice(0, -1)
  28. argsWithoutCb.should.eql([1, 2, 3, 'foo'])
  29. }
  30. )
  31. it(
  32. 'should call a method of an object with the third argument as the name',
  33. function () {
  34. var ob = { foo: fn }
  35. async(ch, ob, 'foo', 1, 2, 3)
  36. var argsWithoutCb = fn.firstCall.args.slice(0, -1)
  37. argsWithoutCb.should.eql([1, 2, 3])
  38. fn.firstCall.calledOn(ob).should.be.true
  39. }
  40. )
  41. it(
  42. 'should call channel with arguments of the async function callback',
  43. function () {
  44. async(ch, fn)
  45. ch.firstCall.args.length.should.be.exactly(2)
  46. ch.firstCall.args[0].should.be.exactly(err)
  47. ch.firstCall.args[1].should.be.exactly(val)
  48. }
  49. )
  50. it(
  51. 'should call callback given to returned function',
  52. function (done) {
  53. var cb = sinon.spy()
  54. async(ch, fn)(cb)
  55. setImmediate(function () {
  56. cb.callCount.should.be.exactly(1)
  57. done()
  58. })
  59. }
  60. )
  61. })