forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpromiseWaterfall.spec.js
More file actions
48 lines (39 loc) · 1.24 KB
/
promiseWaterfall.spec.js
File metadata and controls
48 lines (39 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @emails oncall+javascript_foundation
*/
'use strict';
const promiseWaterfall = require('../promiseWaterfall');
describe('promiseWaterfall', () => {
it('should run promises in a sequence', async () => {
const tasks = [jest.fn(), jest.fn()];
await promiseWaterfall(tasks);
// Check that tasks[0] is executed before tasks[1].
expect(tasks[0].mock.invocationCallOrder[0]).toBeLessThan(
tasks[1].mock.invocationCallOrder[0],
);
});
it('should resolve with last promise value', async () => {
const tasks = [jest.fn().mockReturnValue(1), jest.fn().mockReturnValue(2)];
expect(await promiseWaterfall(tasks)).toEqual(2);
});
it('should stop the sequence when one of promises is rejected', done => {
const error = new Error();
const tasks = [
jest.fn().mockImplementation(() => {
throw error;
}),
jest.fn().mockReturnValue(2),
];
promiseWaterfall(tasks).catch(err => {
expect(err).toEqual(error);
expect(tasks[1].mock.calls.length).toEqual(0);
done();
});
});
});