stringify.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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/stringify'
  7. suite('stringify:', () => {
  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, stringify
  18. setup(() => {
  19. log = {}
  20. stringify = proxyquire(modulePath, {
  21. './streamify': spooks.fn({
  22. name: 'streamify',
  23. log: log,
  24. results: [
  25. { on: spooks.fn({ name: 'on', log: log }) }
  26. ]
  27. })
  28. })
  29. })
  30. test('stringify expects two arguments', () => {
  31. assert.lengthOf(stringify, 2)
  32. })
  33. test('stringify does not throw', () => {
  34. assert.doesNotThrow(() => {
  35. stringify()
  36. })
  37. })
  38. test('stringify returns promise', () => {
  39. assert.instanceOf(stringify(), Promise)
  40. })
  41. test('streamify was not called', () => {
  42. assert.strictEqual(log.counts.streamify, 0)
  43. })
  44. suite('stringify:', () => {
  45. let data, options, resolved, rejected, result, done
  46. setup(() => {
  47. data = {}
  48. options = {}
  49. stringify(data, options)
  50. .then(res => {
  51. resolved = res
  52. done()
  53. })
  54. .catch(rej => {
  55. rejected = rej
  56. done()
  57. })
  58. })
  59. teardown(() => {
  60. resolved = rejected = undefined
  61. })
  62. test('streamify was called once', () => {
  63. assert.strictEqual(log.counts.streamify, 1)
  64. assert.isUndefined(log.these.streamify[0])
  65. })
  66. test('streamify was called correctly', () => {
  67. assert.lengthOf(log.args.streamify[0], 2)
  68. assert.strictEqual(log.args.streamify[0][0], data)
  69. assert.lengthOf(Object.keys(log.args.streamify[0][0]), 0)
  70. assert.strictEqual(log.args.streamify[0][1], options)
  71. assert.lengthOf(Object.keys(log.args.streamify[0][1]), 0)
  72. })
  73. test('stream.on was called four times', () => {
  74. assert.strictEqual(log.counts.on, 4)
  75. })
  76. test('stream.on was called correctly first time', () => {
  77. assert.lengthOf(log.args.on[0], 2)
  78. assert.strictEqual(log.args.on[0][0], 'data')
  79. assert.isFunction(log.args.on[0][1])
  80. })
  81. test('stream.on was called correctly second time', () => {
  82. assert.strictEqual(log.args.on[1][0], 'end')
  83. assert.isFunction(log.args.on[1][1])
  84. assert.notStrictEqual(log.args.on[1][1], log.args.on[0][1])
  85. })
  86. test('stream.on was called correctly third time', () => {
  87. assert.strictEqual(log.args.on[2][0], 'error')
  88. assert.isFunction(log.args.on[2][1])
  89. assert.notStrictEqual(log.args.on[2][1], log.args.on[0][1])
  90. assert.notStrictEqual(log.args.on[2][1], log.args.on[1][1])
  91. })
  92. test('stream.on was called correctly fourth time', () => {
  93. assert.strictEqual(log.args.on[3][0], 'dataError')
  94. assert.isFunction(log.args.on[3][1])
  95. assert.strictEqual(log.args.on[3][1], log.args.on[2][1])
  96. })
  97. test('promise is unfulfilled', () => {
  98. assert.isUndefined(resolved)
  99. assert.isUndefined(rejected)
  100. })
  101. suite('data event:', () => {
  102. setup(() => {
  103. log.args.on[0][1]('foo')
  104. })
  105. test('promise is unfulfilled', () => {
  106. assert.isUndefined(resolved)
  107. assert.isUndefined(rejected)
  108. })
  109. suite('end event:', () => {
  110. setup(d => {
  111. done = d
  112. log.args.on[1][1]()
  113. })
  114. test('promise is resolved', () => {
  115. assert.strictEqual(resolved, 'foo')
  116. })
  117. test('promise is not rejected', () => {
  118. assert.isUndefined(rejected)
  119. })
  120. })
  121. suite('data event:', () => {
  122. setup(() => {
  123. log.args.on[0][1]('bar')
  124. })
  125. test('promise is unfulfilled', () => {
  126. assert.isUndefined(resolved)
  127. assert.isUndefined(rejected)
  128. })
  129. suite('end event:', () => {
  130. setup(d => {
  131. done = d
  132. log.args.on[1][1]()
  133. })
  134. test('promise is resolved', () => {
  135. assert.strictEqual(resolved, 'foobar')
  136. })
  137. })
  138. suite('error event:', () => {
  139. setup(d => {
  140. done = d
  141. log.args.on[2][1]('wibble')
  142. })
  143. test('promise is rejected', () => {
  144. assert.strictEqual(rejected, 'wibble')
  145. })
  146. })
  147. suite('dataError event:', () => {
  148. setup(d => {
  149. done = d
  150. log.args.on[3][1]('wibble')
  151. })
  152. test('promise is rejected', () => {
  153. assert.strictEqual(rejected, 'wibble')
  154. })
  155. })
  156. })
  157. })
  158. })
  159. })
  160. })