unpipe.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict'
  2. const assert = require('chai').assert
  3. const proxyquire = require('proxyquire')
  4. const spooks = require('spooks')
  5. const Promise = require('bluebird')
  6. const modulePath = '../../src/unpipe'
  7. suite('unpipe:', () => {
  8. test('require does not throw', () => {
  9. assert.doesNotThrow(() => {
  10. require(modulePath)
  11. })
  12. })
  13. test('require returns function', () => {
  14. assert.isFunction(require(modulePath))
  15. })
  16. suite('require:', () => {
  17. let log, results, unpipe
  18. setup(() => {
  19. log = {}
  20. results = {
  21. parse: [ Promise.resolve() ]
  22. }
  23. unpipe = proxyquire(modulePath, {
  24. './parse': spooks.fn({
  25. name: 'parse',
  26. log: log,
  27. results: results.parse
  28. })
  29. })
  30. })
  31. test('unpipe expects two arguments', () => {
  32. assert.lengthOf(unpipe, 2)
  33. })
  34. test('unpipe does not throw', () => {
  35. assert.doesNotThrow(() => {
  36. unpipe(() => {})
  37. })
  38. })
  39. test('unpipe throws if callback is not provided', () => {
  40. assert.throws(() => {
  41. unpipe()
  42. })
  43. })
  44. test('parse was not called', () => {
  45. assert.strictEqual(log.counts.parse, 0)
  46. })
  47. suite('unpipe success:', () => {
  48. let result, error, options
  49. setup(done => {
  50. results.parse[0] = Promise.resolve('foo')
  51. options = { foo: 'bar', ndjson: true }
  52. unpipe((err, res) => {
  53. error = err
  54. result = res
  55. done()
  56. }, options)
  57. })
  58. test('parse was called once', () => {
  59. assert.strictEqual(log.counts.parse, 1)
  60. })
  61. test('parse was called correctly', () => {
  62. assert.isUndefined(log.these.parse[0])
  63. assert.lengthOf(log.args.parse[0], 2)
  64. assert.isObject(log.args.parse[0][0])
  65. assert.isTrue(log.args.parse[0][0].readable)
  66. assert.isTrue(log.args.parse[0][0].writable)
  67. assert.isFunction(log.args.parse[0][0].pipe)
  68. assert.isFunction(log.args.parse[0][0].read)
  69. assert.isFunction(log.args.parse[0][0]._read)
  70. assert.isFunction(log.args.parse[0][0].write)
  71. assert.isFunction(log.args.parse[0][0]._write)
  72. assert.notStrictEqual(log.args.parse[0][1], options)
  73. assert.deepEqual(log.args.parse[0][1], { foo: 'bar', ndjson: false })
  74. })
  75. test('parse result was returned', () => {
  76. assert.strictEqual(result, 'foo')
  77. })
  78. test('did not fail', () => {
  79. assert.isNull(error)
  80. })
  81. })
  82. suite('unpipe error:', () => {
  83. let result, error, options
  84. setup(done => {
  85. results.parse[0] = Promise.reject('bar')
  86. options = {}
  87. unpipe((err, res) => {
  88. error = err
  89. result = res
  90. done()
  91. }, options)
  92. })
  93. test('parse was called once', () => {
  94. assert.strictEqual(log.counts.parse, 1)
  95. })
  96. test('parse result was not returned', () => {
  97. assert.isUndefined(result)
  98. })
  99. test('failed', () => {
  100. assert.strictEqual(error, 'bar')
  101. })
  102. })
  103. })
  104. })