|
| 1 | +import {apply, isPolyfilled, isSupported, withResolvers} from '../lib/promise-withResolvers.js' |
| 2 | + |
| 3 | +describe('withResolvers', () => { |
| 4 | + it('has standard isSupported, isPolyfilled, apply API', () => { |
| 5 | + expect(isSupported).to.be.a('function') |
| 6 | + expect(isPolyfilled).to.be.a('function') |
| 7 | + expect(apply).to.be.a('function') |
| 8 | + expect(isSupported()).to.be.a('boolean') |
| 9 | + expect(isPolyfilled()).to.equal(false) |
| 10 | + }) |
| 11 | + |
| 12 | + it('resolves to first resolving value', async () => { |
| 13 | + const arg = withResolvers() |
| 14 | + expect(Object.keys(arg).sort()).to.eql(['promise', 'reject', 'resolve']) |
| 15 | + expect(arg).to.have.property('promise').to.be.a('promise') |
| 16 | + expect(arg).to.have.property('resolve').to.be.a('function') |
| 17 | + expect(arg).to.have.property('reject').to.be.a('function') |
| 18 | + |
| 19 | + arg.resolve(1) |
| 20 | + expect(await arg.promise).to.be.eql(1) |
| 21 | + }) |
| 22 | + |
| 23 | + it('rejects to first rejecting reason', async () => { |
| 24 | + const arg = withResolvers() |
| 25 | + expect(Object.keys(arg).sort()).to.eql(['promise', 'reject', 'resolve']) |
| 26 | + expect(arg).to.have.property('promise').to.be.a('promise') |
| 27 | + expect(arg).to.have.property('resolve').to.be.a('function') |
| 28 | + expect(arg).to.have.property('reject').to.be.a('function') |
| 29 | + |
| 30 | + const err = new Error('rejected') |
| 31 | + |
| 32 | + try { |
| 33 | + arg.reject(err) |
| 34 | + await arg.promise |
| 35 | + expect.fail('should fail') |
| 36 | + } catch (e) { |
| 37 | + expect(e).to.be.eql(err) |
| 38 | + } |
| 39 | + }) |
| 40 | +}) |
0 commit comments