read.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict'
  2. const assert = require('chai').assert
  3. const proxyquire = require('proxyquire')
  4. const spooks = require('spooks')
  5. const modulePath = '../../src/read'
  6. suite('read:', () => {
  7. test('require does not throw', () => {
  8. assert.doesNotThrow(() => {
  9. require(modulePath)
  10. })
  11. })
  12. test('require returns function', () => {
  13. assert.isFunction(require(modulePath))
  14. })
  15. suite('require:', () => {
  16. let log, results, read
  17. setup(() => {
  18. log = {}
  19. results = {
  20. parse: [ {} ],
  21. createReadStream: [ {} ]
  22. }
  23. read = proxyquire(modulePath, {
  24. fs: {
  25. createReadStream: spooks.fn({
  26. name: 'createReadStream',
  27. log: log,
  28. results: results.createReadStream
  29. })
  30. },
  31. './parse': spooks.fn({
  32. name: 'parse',
  33. log: log,
  34. results: results.parse
  35. })
  36. })
  37. })
  38. test('read expects two arguments', () => {
  39. assert.lengthOf(read, 2)
  40. })
  41. test('read does not throw', () => {
  42. assert.doesNotThrow(() => {
  43. read()
  44. })
  45. })
  46. test('parse was not called', () => {
  47. assert.strictEqual(log.counts.parse, 0)
  48. })
  49. test('fs.createReadStream was not called', () => {
  50. assert.strictEqual(log.counts.createReadStream, 0)
  51. })
  52. suite('read:', () => {
  53. let path, options, result
  54. setup(() => {
  55. path = {}
  56. options = { foo: 'bar', ndjson: true }
  57. result = read(path, options)
  58. })
  59. test('fs.createReadStream was called once', () => {
  60. assert.strictEqual(log.counts.createReadStream, 1)
  61. })
  62. test('fs.createReadStream was called correctly', () => {
  63. assert.lengthOf(log.args.createReadStream[0], 2)
  64. assert.strictEqual(log.args.createReadStream[0][0], path)
  65. assert.lengthOf(Object.keys(log.args.createReadStream[0][0]), 0)
  66. assert.strictEqual(log.args.createReadStream[0][1], options)
  67. assert.lengthOf(Object.keys(log.args.createReadStream[0][1]), 2)
  68. })
  69. test('parse was called once', () => {
  70. assert.strictEqual(log.counts.parse, 1)
  71. })
  72. test('parse was called correctly', () => {
  73. assert.isUndefined(log.these.parse[0])
  74. assert.lengthOf(log.args.parse[0], 2)
  75. assert.strictEqual(log.args.parse[0][0], results.createReadStream[0])
  76. assert.lengthOf(Object.keys(log.args.parse[0][0]), 0)
  77. assert.notStrictEqual(log.args.parse[0][1], options)
  78. assert.deepEqual(log.args.parse[0][1], { foo: 'bar', ndjson: false })
  79. })
  80. test('parse result was returned', () => {
  81. assert.strictEqual(result, results.parse[0])
  82. })
  83. })
  84. })
  85. })