2016-07-30 15:59:16 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-05-20 11:52:08 +00:00
|
|
|
const sinon = require('sinon');
|
2016-08-22 15:56:14 +00:00
|
|
|
const promiseWaterfall = require('../promiseWaterfall');
|
2016-05-20 11:52:08 +00:00
|
|
|
|
|
|
|
describe('promiseWaterfall', () => {
|
|
|
|
|
|
|
|
it('should run promises in a sequence', (done) => {
|
|
|
|
const tasks = [sinon.stub(), sinon.stub()];
|
|
|
|
|
|
|
|
promiseWaterfall(tasks).then(() => {
|
2016-07-30 15:59:16 +00:00
|
|
|
expect(tasks[0].calledBefore(tasks[1])).toBeTruthy();
|
2016-05-20 11:52:08 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should resolve with last promise value', (done) => {
|
|
|
|
const tasks = [sinon.stub().returns(1), sinon.stub().returns(2)];
|
|
|
|
|
|
|
|
promiseWaterfall(tasks).then(value => {
|
2016-07-30 15:59:16 +00:00
|
|
|
expect(value).toEqual(2);
|
2016-05-20 11:52:08 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should stop the sequence when one of promises is rejected', (done) => {
|
|
|
|
const error = new Error();
|
|
|
|
const tasks = [sinon.stub().throws(error), sinon.stub().returns(2)];
|
|
|
|
|
|
|
|
promiseWaterfall(tasks).catch(err => {
|
2016-07-30 15:59:16 +00:00
|
|
|
expect(err).toEqual(error);
|
|
|
|
expect(tasks[1].callCount).toEqual(0);
|
2016-05-20 11:52:08 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|