1 var should = require('should');
2 var through = require('through2');
3 var OrderedStreams = require('../');
5 describe('ordered-read-streams', function () {
6 it('should end if no streams are given', function (done) {
7 var streams = OrderedStreams();
8 streams.on('data', function () {
11 streams.on('end', done);
14 it('should throw error if one or more streams are not readable', function (done) {
15 var writable = { readable: false };
18 new OrderedStreams(writable);
20 e.message.should.equal('All input streams must be readable');
25 it('should emit data from all streams', function(done) {
26 var s1 = through.obj(function (data, enc, next) {
30 var s2 = through.obj(function (data, enc, next) {
34 var s3 = through.obj(function (data, enc, next) {
39 var streams = new OrderedStreams([s1, s2, s3]);
41 streams.on('data', function (data) {
44 streams.on('end', function () {
45 results.length.should.be.exactly(3);
46 results[0].should.equal('stream 1');
47 results[1].should.equal('stream 2');
48 results[2].should.equal('stream 3');
62 it('should emit all data event from each stream', function (done) {
63 var s = through.obj(function (data, enc, next) {
68 var streams = new OrderedStreams(s);
70 streams.on('data', function (data) {
73 streams.on('end', function () {
74 results.length.should.be.exactly(3);
84 it('should preserve streams order', function(done) {
85 var s1 = through.obj(function (data, enc, next) {
87 setTimeout(function () {
92 var s2 = through.obj(function (data, enc, next) {
94 setTimeout(function () {
99 var s3 = through.obj(function (data, enc, next) {
101 setTimeout(function () {
107 var streams = new OrderedStreams([s1, s2, s3]);
109 streams.on('data', function (data) {
112 streams.on('end', function () {
113 results.length.should.be.exactly(3);
114 results[0].should.equal('stream 1');
115 results[1].should.equal('stream 2');
116 results[2].should.equal('stream 3');
120 s1.write('stream 1');
123 s2.write('stream 2');
126 s3.write('stream 3');
130 it('should emit stream errors downstream', function (done) {
131 var s = through.obj(function (data, enc, next) {
132 this.emit('error', new Error('stahp!'));
135 var s2 = through.obj(function (data, enc, next) {
142 var streams = new OrderedStreams([s, s2]);
143 streams.on('data', function (data) {
146 streams.on('error', function (err) {
147 errMsg = err.message;
149 streams.on('end', function () {
150 errMsg.should.equal('stahp!');
151 streamData.should.equal('Im ok!');