-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathpromise-withResolvers.js
40 lines (34 loc) · 1.36 KB
/
promise-withResolvers.js
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
import {apply, isPolyfilled, isSupported, withResolvers} from '../lib/promise-withResolvers.js'
describe('withResolvers', () => {
it('has standard isSupported, isPolyfilled, apply API', () => {
expect(isSupported).to.be.a('function')
expect(isPolyfilled).to.be.a('function')
expect(apply).to.be.a('function')
expect(isSupported()).to.be.a('boolean')
expect(isPolyfilled()).to.equal(false)
})
it('resolves to first resolving value', async () => {
const arg = withResolvers()
expect(Object.keys(arg).sort()).to.eql(['promise', 'reject', 'resolve'])
expect(arg).to.have.property('promise').to.be.a('promise')
expect(arg).to.have.property('resolve').to.be.a('function')
expect(arg).to.have.property('reject').to.be.a('function')
arg.resolve(1)
expect(await arg.promise).to.be.eql(1)
})
it('rejects to first rejecting reason', async () => {
const arg = withResolvers()
expect(Object.keys(arg).sort()).to.eql(['promise', 'reject', 'resolve'])
expect(arg).to.have.property('promise').to.be.a('promise')
expect(arg).to.have.property('resolve').to.be.a('function')
expect(arg).to.have.property('reject').to.be.a('function')
const err = new Error('rejected')
try {
arg.reject(err)
await arg.promise
expect.fail('should fail')
} catch (e) {
expect(e).to.be.eql(err)
}
})
})