datastream.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict'
  2. const assert = require('chai').assert
  3. const spooks = require('spooks')
  4. const modulePath = '../../src/datastream'
  5. suite('datastream:', () => {
  6. let log
  7. setup(() => {
  8. log = {}
  9. })
  10. test('require does not throw', () => {
  11. assert.doesNotThrow(() => {
  12. require(modulePath)
  13. })
  14. })
  15. test('require returns function', () => {
  16. assert.isFunction(require(modulePath))
  17. })
  18. suite('require:', () => {
  19. let Stream
  20. setup(() => {
  21. Stream = require(modulePath)
  22. })
  23. test('Stream expects two arguments', () => {
  24. assert.lengthOf(Stream, 2)
  25. })
  26. test('calling Stream with function argument doesNotThrow', () => {
  27. assert.doesNotThrow(() => {
  28. Stream(() => {})
  29. })
  30. })
  31. test('calling Stream with object argument throws', () => {
  32. assert.throws(() => {
  33. Stream({ read: () => {} })
  34. })
  35. })
  36. test('calling Stream with new returns Stream instance', () => {
  37. assert.instanceOf(new Stream(() => {}), Stream)
  38. })
  39. test('calling Stream with new returns Readable instance', () => {
  40. assert.instanceOf(new Stream(() => {}), require('stream').Readable)
  41. })
  42. test('calling Stream without new returns Stream instance', () => {
  43. assert.instanceOf(Stream(() => {}), Stream)
  44. })
  45. suite('instantiate:', () => {
  46. let datastream
  47. setup(() => {
  48. datastream = new Stream(spooks.fn({ name: 'read', log: log }))
  49. })
  50. test('datastream has _read method', () => {
  51. assert.isFunction(datastream._read)
  52. })
  53. test('_read expects no arguments', () => {
  54. assert.lengthOf(datastream._read, 0)
  55. })
  56. test('read was not called', () => {
  57. assert.strictEqual(log.counts.read, 0)
  58. })
  59. suite('datastream._read:', () => {
  60. setup(() => {
  61. datastream._read()
  62. })
  63. test('read was called once', () => {
  64. assert.strictEqual(log.counts.read, 1)
  65. assert.isUndefined(log.these.read[0])
  66. })
  67. test('read was called correctly', () => {
  68. assert.lengthOf(log.args.read[0], 0)
  69. })
  70. })
  71. })
  72. })
  73. })