-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
promise.ts
57 lines (54 loc) · 1.76 KB
/
promise.ts
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
49
50
51
52
53
54
55
56
57
import * as chai from 'chai'
import * as sinon from 'sinon'
import * as sinonChai from 'sinon-chai'
import * as chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
chai.use(sinonChai)
chai.use(chaiAsPromised)
const P = require('../../../src/util/promise')
describe('util/promise', function () {
describe('.mapSeries()', async function () {
it('should resolve when all resolved', async function () {
const result = await P.mapSeries(
['first', 'second', 'third'],
(item: string) => Promise.resolve(item)
)
return expect(result).to.deep.equal(['first', 'second', 'third'])
})
it('should reject with the error that first callback rejected', () => {
const p = P.mapSeries(['first', 'second'],
(item: string) => Promise.reject(item))
return expect(p).to.rejectedWith('first')
})
it('should resolve in series', function () {
const spy1 = sinon.spy()
const spy2 = sinon.spy()
return P
.mapSeries(
['first', 'second'],
(item: string, idx: number) => new Promise(function (resolve) {
if (idx === 0) {
setTimeout(function () {
spy1()
resolve('first cb')
}, 10)
} else {
spy2()
resolve('foo')
}
}))
.then(() => expect(spy2).to.have.been.calledAfter(spy1))
})
it('should not call rest of callbacks once rejected', () => {
const spy = sinon.spy()
return P
.mapSeries(['first', 'second'], (item: string, idx: number) => {
if (idx > 0) {
spy()
}
return Promise.reject(new Error(item))
})
.catch(() => expect(spy).to.not.have.been.called)
})
})
})