highland-callback
Version:
Call a node callback with an optional error after stream completes.
62 lines (54 loc) • 1.37 kB
JavaScript
var _ = require('highland'),
assert = require('chai').assert,
highlandCallback = require('../index');
describe('highland callback', function() {
it('calls after the stream is complete', function(done) {
var order = [];
_([1, 2, 3])
.consume(highlandCallback(callback))
.doto(function(value) {
order.push(value);
})
.resume();
function callback(error) {
assert.isUndefined(error);
assert.deepEqual(order, [1, 2, 3]);
done();
}
});
it('only calls once', function(done) {
var timesCalled = 0;
_(function(push, next) {
push(null, 1);
push(null, 2);
push(null, 3);
push(null, _.nil);
})
.consume(highlandCallback(callback))
.apply(function() {
assert.equal(timesCalled, 1);
done();
});
function callback(error) {
timesCalled++;
}
});
it('propagates the first error', function(done) {
_(function(push, next) {
push(new Error('broken'));
push(null, 1);
push(new Error('broken2'));
push(null, 2);
push(new Error('broken3'));
push(null, 3);
push(null, _.nil);
})
.consume(highlandCallback(callback))
.resume();
function callback(error) {
assert.isTrue(error instanceof Error);
assert.equal(error.message, 'broken');
done();
}
});
});