Skip to content

Commit

Permalink
[Tests] increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jan 26, 2024
1 parent fe34373 commit 0a0a33f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 9 deletions.
3 changes: 1 addition & 2 deletions test/auto-destroy.js
Expand Up @@ -6,8 +6,7 @@ var through = require('../');
// must emit end before close.

test('end before close', function (assert) {
var ts = through();
ts.autoDestroy = false;
var ts = through(null, null, { autoDestroy: false });
var ended = false;
var closed = false;

Expand Down
29 changes: 29 additions & 0 deletions test/buffering.js
Expand Up @@ -68,3 +68,32 @@ test('buffering has data in queue, when ends', function (assert) {
assert.ok(ended, 'end should be emitted once all data was delivered');
assert.end();
});

test('data pauses', function (t) {
var ts = through();

var results = [];
ts.on('data', function (data) {
ts.pause();
results.push(data);
});

t.equal(ts.paused, false, 'starts not paused');

ts.write(1);
t.equal(ts.paused, true, 'pauses on write');
ts.write(2);
t.equal(ts.paused, true, 'pauses on write');
ts.write(3);
t.equal(ts.paused, true, 'pauses on write');

t.deepEqual(results, [1], 'has not been resumed');
ts.resume();
t.equal(ts.paused, true, 'resume unpauses, but write re-pauses');

t.deepEqual(results, [1, 2], 'has only been resumed once');
ts.resume();
t.deepEqual(results, [1, 2, 3], 'has been resumed twice');

t.end();
});
44 changes: 38 additions & 6 deletions test/end.js
Expand Up @@ -5,30 +5,36 @@ var through = require('../');

// must emit end before close.

test('end before close', function (assert) {
test('end before close', function (t) {
t.plan(4);

var ts = through();
var ended = false;
var closed = false;

ts.on('end', function () {
assert.ok(!closed);
t.ok(!closed);
ended = true;
});
ts.on('close', function () {
assert.ok(ended);
t.ok(ended);
closed = true;
});

ts.write(1);
ts.write(2);
ts.write(3);
ts.end();
assert.ok(ended);
assert.ok(closed);
assert.end();

t.ok(ended);
t.ok(closed);

t.end();
});

test('end only once', function (t) {
t.plan(4);

var ts = through();
var ended = false;

Expand All @@ -43,5 +49,31 @@ test('end only once', function (t) {

ts.resume();

t.equal(ended, true, 'is ended');

ts.end();
t.equal(ended, true, 'is still ended after end()');

ts.end();
t.equal(ended, true, 'is still ended after end() 2x');

t.end();
});

test('end with value', function (t) {
var ts = through(null, null, { autoDestroy: false });

var results = [];
ts.on('data', function (data) {
results.push(data);
});

ts.queue(123);
ts.queue(456);

ts.end('end');

t.deepEqual(results, [123, 456, 'end']);

t.end();
});
10 changes: 9 additions & 1 deletion test/index.js
Expand Up @@ -78,8 +78,16 @@ test('simple functions', function (assert) {
write(expected, t);
});

test('pauses', function (assert) {
test('pauses twice', function (t) {
var ts = through();

t.equal(ts.pause(), ts, 'when pausing, returns stream');
t.equal(ts.pause(), undefined, 'when already paused, returns void');

t.end();
});

test('pauses', function (assert) {
var l = 1000;
var expected = [];

Expand Down

0 comments on commit 0a0a33f

Please sign in to comment.