chan.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* global describe:true, beforeEach:true, it:true */
  2. var chan = require('..')
  3. var expect = require('expect.js')
  4. var fs = require('fs')
  5. describe('Channel make', function () {
  6. it(
  7. 'should return a channel function',
  8. function () {
  9. var ch = chan()
  10. expect(ch).to.be.a(Function)
  11. }
  12. )
  13. })
  14. describe('A channel', function () {
  15. var ch
  16. beforeEach(function () {
  17. ch = chan()
  18. })
  19. it(
  20. 'should receive a value of any non-function type as the first argument',
  21. function () {
  22. var typeCases = [
  23. 1,
  24. 'foo',
  25. [1, 2 , 3],
  26. {foo: 'bar'},
  27. true,
  28. false,
  29. null,
  30. void 0
  31. ]
  32. typeCases.forEach(function (val) {
  33. ch(val)
  34. ch(function (err, result) {
  35. expect(result).to.be(val)
  36. })
  37. })
  38. }
  39. )
  40. it(
  41. 'should receive a function value as a second argument if the first is null',
  42. function () {
  43. ch(null, function () {})
  44. ch(function (err, result) {
  45. expect(result).to.be.a(Function)
  46. })
  47. }
  48. )
  49. it(
  50. 'should queue values until they are yielded/removed',
  51. function () {
  52. var values = [1, 2, 3, 4, 5]
  53. values.forEach(function (value) {
  54. ch(value)
  55. })
  56. values.forEach(function (value) {
  57. ch(function (err, result) {
  58. expect(result).to.be(value)
  59. })
  60. })
  61. }
  62. )
  63. it(
  64. 'should queue callbacks until values are added',
  65. function () {
  66. var values = [1, 2, 3, 4, 5]
  67. values.forEach(function (value) {
  68. ch(function (err, result) {
  69. expect(result).to.be(value)
  70. })
  71. })
  72. values.forEach(function (value) {
  73. ch(value)
  74. })
  75. }
  76. )
  77. it(
  78. 'should pass errors as the first argument to callbacks',
  79. function () {
  80. var e = new Error('Foo')
  81. ch(e)
  82. ch(function (err) {
  83. expect(err).to.be(e)
  84. })
  85. }
  86. )
  87. it(
  88. 'should be useable directly as a callback for node style async functions',
  89. function (done) {
  90. ch(function (err, contents) {
  91. expect(err).to.be(null)
  92. expect(contents).to.be.a(Buffer)
  93. done()
  94. })
  95. fs.readFile(__filename, ch)
  96. }
  97. )
  98. })