|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var expect = require('expect'); |
| 4 | + |
| 5 | +var streamx = require('streamx'); |
| 6 | + |
| 7 | +var asyncDone = require('../'); |
| 8 | + |
| 9 | +function success() { |
| 10 | + return streamx.Readable.from('Foo Bar Baz').pipe(new streamx.Writable()); |
| 11 | +} |
| 12 | + |
| 13 | +function failure() { |
| 14 | + return streamx.Readable.from('Foo Bar Baz').pipe( |
| 15 | + new streamx.Writable({ |
| 16 | + write: function (data, cb) { |
| 17 | + cb(new Error('Fail')); |
| 18 | + }, |
| 19 | + }) |
| 20 | + ); |
| 21 | +} |
| 22 | + |
| 23 | +function pipelineError() { |
| 24 | + return streamx.pipeline( |
| 25 | + streamx.Readable.from('Foo Bar Baz'), |
| 26 | + new streamx.Transform(), |
| 27 | + new streamx.Transform({ |
| 28 | + transform: function (data, cb) { |
| 29 | + cb(new Error('Fail')); |
| 30 | + }, |
| 31 | + }), |
| 32 | + new streamx.Writable() |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +function unpiped() { |
| 37 | + return streamx.Readable.from('Foo Bar Baz'); |
| 38 | +} |
| 39 | + |
| 40 | +describe('streamx streams', function () { |
| 41 | + it('should handle a successful stream', function (done) { |
| 42 | + asyncDone(success, function (err) { |
| 43 | + expect(err).not.toBeInstanceOf(Error); |
| 44 | + done(); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should handle an errored stream', function (done) { |
| 49 | + asyncDone(failure, function (err) { |
| 50 | + expect(err).toBeInstanceOf(Error); |
| 51 | + expect(err.message).not.toEqual('premature close'); |
| 52 | + done(); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should handle an errored pipeline', function (done) { |
| 57 | + asyncDone(pipelineError, function (err) { |
| 58 | + expect(err).toBeInstanceOf(Error); |
| 59 | + expect(err.message).not.toEqual('premature close'); |
| 60 | + done(); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('handle a returned stream and cb by only calling callback once', function (done) { |
| 65 | + asyncDone( |
| 66 | + function (cb) { |
| 67 | + return success().on('finish', function () { |
| 68 | + cb(null, 3); |
| 69 | + }); |
| 70 | + }, |
| 71 | + function (err, result) { |
| 72 | + expect(err).not.toBeInstanceOf(Error); |
| 73 | + expect(result).toEqual(3); // To know we called the callback |
| 74 | + done(); |
| 75 | + } |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + it('consumes an unpiped readable stream', function (done) { |
| 80 | + asyncDone(unpiped, function (err) { |
| 81 | + expect(err).not.toBeInstanceOf(Error); |
| 82 | + done(); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments