write.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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/write'
  7. suite('write:', () => {
  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, write
  18. setup(() => {
  19. log = {}
  20. results = {
  21. createWriteStream: [ {} ]
  22. }
  23. write = proxyquire(modulePath, {
  24. 'fs': {
  25. createWriteStream: spooks.fn({
  26. name: 'createWriteStream',
  27. log: log,
  28. results: results.createWriteStream
  29. })
  30. },
  31. './streamify': spooks.fn({
  32. name: 'streamify',
  33. log: log,
  34. results: [
  35. {
  36. pipe: spooks.fn({ name: 'pipe', log: log, chain: true }),
  37. on: spooks.fn({ name: 'on', log: log, chain: true })
  38. }
  39. ]
  40. })
  41. })
  42. })
  43. test('write expects three arguments', () => {
  44. assert.lengthOf(write, 3)
  45. })
  46. test('write does not throw', () => {
  47. assert.doesNotThrow(() => {
  48. write()
  49. })
  50. })
  51. test('streamify was not called', () => {
  52. assert.strictEqual(log.counts.streamify, 0)
  53. })
  54. test('fs.createWriteStream was not called', () => {
  55. assert.strictEqual(log.counts.createWriteStream, 0)
  56. })
  57. test('stream.pipe was not called', () => {
  58. assert.strictEqual(log.counts.pipe, 0)
  59. })
  60. test('stream.on was not called', () => {
  61. assert.strictEqual(log.counts.on, 0)
  62. })
  63. suite('write:', () => {
  64. let path, data, options, result
  65. setup(() => {
  66. path = {}
  67. data = {}
  68. options = {}
  69. result = write(path, data, options)
  70. })
  71. test('streamify was called once', () => {
  72. assert.strictEqual(log.counts.streamify, 1)
  73. assert.isUndefined(log.these.streamify[0])
  74. })
  75. test('streamify was called correctly', () => {
  76. assert.lengthOf(log.args.streamify[0], 2)
  77. assert.strictEqual(log.args.streamify[0][0], data)
  78. assert.lengthOf(Object.keys(log.args.streamify[0][0]), 0)
  79. assert.strictEqual(log.args.streamify[0][1], options)
  80. assert.lengthOf(Object.keys(log.args.streamify[0][1]), 0)
  81. })
  82. test('fs.createWriteStream was called once', () => {
  83. assert.strictEqual(log.counts.createWriteStream, 1)
  84. })
  85. test('fs.createWriteStream was called correctly', () => {
  86. assert.lengthOf(log.args.createWriteStream[0], 2)
  87. assert.strictEqual(log.args.createWriteStream[0][0], path)
  88. assert.lengthOf(Object.keys(log.args.createWriteStream[0][0]), 0)
  89. assert.strictEqual(log.args.createWriteStream[0][1], options)
  90. assert.lengthOf(Object.keys(log.args.createWriteStream[0][1]), 0)
  91. })
  92. test('stream.pipe was called once', () => {
  93. assert.strictEqual(log.counts.pipe, 1)
  94. })
  95. test('stream.pipe was called correctly', () => {
  96. assert.lengthOf(log.args.pipe[0], 1)
  97. assert.strictEqual(log.args.pipe[0][0], results.createWriteStream[0])
  98. assert.lengthOf(Object.keys(log.args.pipe[0][0]), 0)
  99. })
  100. test('stream.on was called three times', () => {
  101. assert.strictEqual(log.counts.on, 3)
  102. })
  103. test('stream.on was called correctly first time', () => {
  104. assert.lengthOf(log.args.on[0], 2)
  105. assert.strictEqual(log.args.on[0][0], 'finish')
  106. assert.isFunction(log.args.on[0][1])
  107. })
  108. test('stream.on was called correctly second time', () => {
  109. assert.lengthOf(log.args.on[1], 2)
  110. assert.strictEqual(log.args.on[1][0], 'error')
  111. assert.isFunction(log.args.on[1][1])
  112. assert.notStrictEqual(log.args.on[1][1], log.args.on[0][1])
  113. })
  114. test('stream.on was called correctly third time', () => {
  115. assert.lengthOf(log.args.on[2], 2)
  116. assert.strictEqual(log.args.on[2][0], 'dataError')
  117. assert.isFunction(log.args.on[2][1])
  118. assert.notStrictEqual(log.args.on[2][1], log.args.on[0][1])
  119. assert.strictEqual(log.args.on[2][1], log.args.on[1][1])
  120. })
  121. test('promise was returned', () => {
  122. assert.instanceOf(result, Promise)
  123. })
  124. suite('dispatch finish event:', () => {
  125. let resolved, error, passed, failed
  126. setup(done => {
  127. passed = failed = false
  128. result.then(res => {
  129. resolved = res
  130. passed = true
  131. done()
  132. }).catch(err => {
  133. error = err
  134. failed = true
  135. done()
  136. })
  137. log.args.on[0][1]('foo')
  138. })
  139. test('promise was resolved', () => {
  140. assert.isTrue(passed)
  141. assert.isFalse(failed)
  142. assert.isUndefined(resolved)
  143. })
  144. })
  145. suite('dispatch error event:', () => {
  146. let resolved, error, passed, failed
  147. setup(done => {
  148. passed = failed = false
  149. result.then(r => {
  150. resolved = r
  151. passed = true
  152. done()
  153. }).catch(e => {
  154. error = e
  155. failed = true
  156. done()
  157. })
  158. log.args.on[1][1]('foo')
  159. })
  160. test('promise was rejected', () => {
  161. assert.isTrue(failed)
  162. assert.isFalse(passed)
  163. assert.strictEqual(error, 'foo')
  164. })
  165. })
  166. suite('dispatch dataError event:', () => {
  167. let resolved, error, passed, failed
  168. setup(done => {
  169. passed = failed = false
  170. result.then(r => {
  171. resolved = r
  172. passed = true
  173. done()
  174. }).catch(e => {
  175. error = e
  176. failed = true
  177. done()
  178. })
  179. log.args.on[2][1]('wibble')
  180. })
  181. test('promise was rejected', () => {
  182. assert.isTrue(failed)
  183. assert.isFalse(passed)
  184. assert.strictEqual(error, 'wibble')
  185. })
  186. })
  187. })
  188. })
  189. })
  190. suite('write with error thrown by fs.createWriteStream:', () => {
  191. let write
  192. setup(() => {
  193. write = proxyquire(modulePath, {
  194. fs: {
  195. createWriteStream () {
  196. throw new Error('foo')
  197. }
  198. },
  199. './streamify': () => ({
  200. pipe: spooks.fn({ name: 'pipe', log: {}, chain: true }),
  201. on: spooks.fn({ name: 'on', log: {}, chain: true })
  202. })
  203. })
  204. })
  205. test('write does not throw', () => {
  206. assert.doesNotThrow(() => {
  207. write().catch(() => {})
  208. })
  209. })
  210. test('write rejects', () => {
  211. write()
  212. .then(() => assert.fail('write should reject'))
  213. .catch(error => {
  214. assert.instanceOf(error, Error)
  215. assert.equal(error.message, 'foo')
  216. })
  217. })
  218. })