react / wstein / node_modules / react / node_modules / envify / node_modules / through / test / index.js
80540 views1var test = require('tape')2var spec = require('stream-spec')3var through = require('../')45/*6I'm using these two functions, and not streams and pipe7so there is less to break. if this test fails it must be8the implementation of _through_9*/1011function write(array, stream) {12array = array.slice()13function next() {14while(array.length)15if(stream.write(array.shift()) === false)16return stream.once('drain', next)1718stream.end()19}2021next()22}2324function read(stream, callback) {25var actual = []26stream.on('data', function (data) {27actual.push(data)28})29stream.once('end', function () {30callback(null, actual)31})32stream.once('error', function (err) {33callback(err)34})35}3637test('simple defaults', function(assert) {3839var l = 100040, expected = []4142while(l--) expected.push(l * Math.random())4344var t = through()45var s = spec(t).through().pausable()4647read(t, function (err, actual) {48assert.ifError(err)49assert.deepEqual(actual, expected)50assert.end()51})5253t.on('close', s.validate)5455write(expected, t)56});5758test('simple functions', function(assert) {5960var l = 100061, expected = []6263while(l--) expected.push(l * Math.random())6465var t = through(function (data) {66this.emit('data', data*2)67})68var s = spec(t).through().pausable()697071read(t, function (err, actual) {72assert.ifError(err)73assert.deepEqual(actual, expected.map(function (data) {74return data*275}))76assert.end()77})7879t.on('close', s.validate)8081write(expected, t)82})8384test('pauses', function(assert) {8586var l = 100087, expected = []8889while(l--) expected.push(l) //Math.random())9091var t = through()9293var s = spec(t)94.through()95.pausable()9697t.on('data', function () {98if(Math.random() > 0.1) return99t.pause()100process.nextTick(function () {101t.resume()102})103})104105read(t, function (err, actual) {106assert.ifError(err)107assert.deepEqual(actual, expected)108})109110t.on('close', function () {111s.validate()112assert.end()113})114115write(expected, t)116})117118test('does not soft-end on `undefined`', function(assert) {119var stream = through()120, count = 0121122stream.on('data', function (data) {123count++124})125126stream.write(undefined)127stream.write(undefined)128129assert.equal(count, 2)130131assert.end()132})133134135