2017-11-02 13:14:11 +00:00
|
|
|
/**
|
2018-02-09 01:10:29 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2017-11-02 13:14:11 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-11-02 13:14:11 +00:00
|
|
|
*
|
2018-05-11 19:43:49 +00:00
|
|
|
* @format
|
2017-11-02 13:14:11 +00:00
|
|
|
* @emails oncall+javascript_foundation
|
|
|
|
*/
|
|
|
|
|
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', () => {
|
2018-05-11 19:43:49 +00:00
|
|
|
it('should run promises in a sequence', done => {
|
2016-05-20 11:52:08 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
it('should resolve with last promise value', done => {
|
2016-05-20 11:52:08 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
it('should stop the sequence when one of promises is rejected', done => {
|
2016-05-20 11:52:08 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|