react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / duplexer2 / test / tests.js
80727 viewsvar assert = require("chai").assert;12var stream = require("readable-stream");34var duplexer2 = require("../");56describe("duplexer2", function() {7var writable, readable;89beforeEach(function() {10writable = new stream.Writable({objectMode: true});11readable = new stream.Readable({objectMode: true});1213writable._write = function _write(input, encoding, done) {14return done();15};1617readable._read = function _read(n) {18};19});2021it("should interact with the writable stream properly for writing", function(done) {22var duplex = duplexer2(writable, readable);2324writable._write = function _write(input, encoding, _done) {25assert.strictEqual(input, "well hello there");2627return done();28};2930duplex.write("well hello there");31});3233it("should interact with the readable stream properly for reading", function(done) {34var duplex = duplexer2(writable, readable);3536duplex.on("data", function(e) {37assert.strictEqual(e, "well hello there");3839return done();40});4142readable.push("well hello there");43});4445it("should end the writable stream, causing it to finish", function(done) {46var duplex = duplexer2(writable, readable);4748writable.once("finish", done);4950duplex.end();51});5253it("should finish when the writable stream finishes", function(done) {54var duplex = duplexer2(writable, readable);5556duplex.once("finish", done);5758writable.end();59});6061it("should end when the readable stream ends", function(done) {62var duplex = duplexer2(writable, readable);6364// required to let "end" fire without reading65duplex.resume();66duplex.once("end", done);6768readable.push(null);69});7071it("should bubble errors from the writable stream when no behaviour is specified", function(done) {72var duplex = duplexer2(writable, readable);7374var originalErr = Error("testing");7576duplex.on("error", function(err) {77assert.strictEqual(err, originalErr);7879return done();80});8182writable.emit("error", originalErr);83});8485it("should bubble errors from the readable stream when no behaviour is specified", function(done) {86var duplex = duplexer2(writable, readable);8788var originalErr = Error("testing");8990duplex.on("error", function(err) {91assert.strictEqual(err, originalErr);9293return done();94});9596readable.emit("error", originalErr);97});9899it("should bubble errors from the writable stream when bubbleErrors is true", function(done) {100var duplex = duplexer2({bubbleErrors: true}, writable, readable);101102var originalErr = Error("testing");103104duplex.on("error", function(err) {105assert.strictEqual(err, originalErr);106107return done();108});109110writable.emit("error", originalErr);111});112113it("should bubble errors from the readable stream when bubbleErrors is true", function(done) {114var duplex = duplexer2({bubbleErrors: true}, writable, readable);115116var originalErr = Error("testing");117118duplex.on("error", function(err) {119assert.strictEqual(err, originalErr);120121return done();122});123124readable.emit("error", originalErr);125});126127it("should not bubble errors from the writable stream when bubbleErrors is false", function(done) {128var duplex = duplexer2({bubbleErrors: false}, writable, readable);129130var timeout = setTimeout(done, 25);131132duplex.on("error", function(err) {133clearTimeout(timeout);134135return done(Error("shouldn't bubble error"));136});137138// prevent uncaught error exception139writable.on("error", function() {});140141writable.emit("error", Error("testing"));142});143144it("should not bubble errors from the readable stream when bubbleErrors is false", function(done) {145var duplex = duplexer2({bubbleErrors: false}, writable, readable);146147var timeout = setTimeout(done, 25);148149duplex.on("error", function(err) {150clearTimeout(timeout);151152return done(Error("shouldn't bubble error"));153});154155// prevent uncaught error exception156readable.on("error", function() {});157158readable.emit("error", Error("testing"));159});160});161162163