spec.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict';
  2. var chai = require('chai');
  3. var expect = chai.expect;
  4. var es = require('event-stream');
  5. var addStream = require('../');
  6. describe('add-stream', function () {
  7. function emit(chunks) {
  8. var mutableChunks = [].concat(chunks);
  9. return es.readable(function (count, callback) {
  10. if (mutableChunks.length === 0) {
  11. return this.emit('end');
  12. }
  13. callback(null, mutableChunks.shift());
  14. });
  15. }
  16. describe('buffer mode', function () {
  17. it('should append a stream', function (done) {
  18. var firstChunks = ['abc', 'def'];
  19. var secondChunks = ['ghi', 'jkl'];
  20. emit(firstChunks)
  21. .pipe(addStream(emit(secondChunks)))
  22. .pipe(es.wait(function (err, buffer) {
  23. expect(buffer.toString()).to.equal(firstChunks.concat(secondChunks).join(''));
  24. done();
  25. }));
  26. });
  27. it('should append a stream from a factory function', function (done) {
  28. var firstChunks = ['abc', 'def'];
  29. var secondChunks = ['ghi', 'jkl'];
  30. emit(firstChunks)
  31. .pipe(addStream(function () {
  32. return emit(secondChunks);
  33. }))
  34. .pipe(es.wait(function (err, buffer) {
  35. expect(buffer.toString()).to.equal(firstChunks.concat(secondChunks).join(''));
  36. done();
  37. }));
  38. });
  39. });
  40. describe('object mode', function () {
  41. it('should append a stream', function (done) {
  42. es.readArray([{p: 1}, {p: 2}, {p: 3}])
  43. .pipe(addStream.obj(es.readArray([{p: 4}, {p: 5}, {p: 6}])))
  44. .pipe(es.writeArray(function (err, array) {
  45. expect(array).to.eql([{p: 1}, {p: 2}, {p: 3}, {p: 4}, {p: 5}, {p: 6}]);
  46. done();
  47. }));
  48. });
  49. it('should append a stream from a factory function', function (done) {
  50. es.readArray([{p: 1}, {p: 2}, {p: 3}])
  51. .pipe(addStream.obj(function () {return es.readArray([{p: 4}, {p: 5}, {p: 6}])}))
  52. .pipe(es.writeArray(function (err, array) {
  53. expect(array).to.eql([{p: 1}, {p: 2}, {p: 3}, {p: 4}, {p: 5}, {p: 6}]);
  54. done();
  55. }));
  56. });
  57. });
  58. });