|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | +import { expect } from 'chai'; |
| 8 | +import { settleAll } from '../src'; |
| 9 | + |
| 10 | +describe('settleAll', () => { |
| 11 | + it('should fulfill all promises', async () => { |
| 12 | + const promises = [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)]; |
| 13 | + const settled = await settleAll(promises); |
| 14 | + expect(settled.fulfilled).to.deep.equal([1, 2, 3]); |
| 15 | + expect(settled.rejected).to.deep.equal([]); |
| 16 | + }); |
| 17 | + it('should reject all promises', async () => { |
| 18 | + const promises = [Promise.reject(1), Promise.reject(2), Promise.reject(3)]; |
| 19 | + const settled = await settleAll(promises); |
| 20 | + expect(settled.fulfilled).to.deep.equal([]); |
| 21 | + expect(settled.rejected).to.deep.equal([1, 2, 3]); |
| 22 | + }); |
| 23 | + it('should fulfill and reject promises', async () => { |
| 24 | + const promises = [Promise.resolve(1), Promise.reject(2), Promise.resolve(3)]; |
| 25 | + const settled = await settleAll(promises); |
| 26 | + expect(settled.fulfilled).to.deep.equal([1, 3]); |
| 27 | + expect(settled.rejected).to.deep.equal([2]); |
| 28 | + }); |
| 29 | + it('should handle empty array', async () => { |
| 30 | + const promises: Array<Promise<never>> = []; |
| 31 | + const settled = await settleAll(promises); |
| 32 | + expect(settled.fulfilled).to.deep.equal([]); |
| 33 | + expect(settled.rejected).to.deep.equal([]); |
| 34 | + }); |
| 35 | + // types other than primitives |
| 36 | + it('should handle types other than primitives', async () => { |
| 37 | + const promises = [Promise.resolve({ a: 1 }), Promise.resolve({ b: 2 }), Promise.resolve({ c: 3 })]; |
| 38 | + const settled = await settleAll<Record<string, number>>(promises); |
| 39 | + expect(settled.fulfilled).to.deep.equal([{ a: 1 }, { b: 2 }, { c: 3 }]); |
| 40 | + expect(settled.rejected).to.deep.equal([]); |
| 41 | + }); |
| 42 | + it('should handle types other than primitives and reject', async () => { |
| 43 | + const promises = [Promise.resolve({ a: 1 }), Promise.reject(new Error('boo')), Promise.resolve({ c: 3 })]; |
| 44 | + const settled = await settleAll<Record<string, number>>(promises); |
| 45 | + expect(settled.fulfilled).to.deep.equal([{ a: 1 }, { c: 3 }]); |
| 46 | + expect(settled.rejected[0].message).to.deep.equal('boo'); |
| 47 | + }); |
| 48 | +}); |
0 commit comments