|
1 | | -import assert from 'assert'; |
2 | | -import { series, parallel } from '../src'; |
| 1 | +import test from 'ava'; |
| 2 | +import { spy } from 'sinon'; |
| 3 | +import { series, parallel, map } from '..'; |
3 | 4 |
|
4 | | -assert.equal(typeof series, 'function'); |
| 5 | +const get = v => Promise.resolve(v); |
5 | 6 |
|
6 | | -assert.equal(typeof parallel, 'function'); |
| 7 | +const sleep = time => new Promise( r => setTimeout(r, time) ); |
7 | 8 |
|
8 | | -parallel([ |
9 | | - () => Promise.resolve('a'), |
10 | | - () => Promise.resolve('b') |
11 | | -]).then( out => { |
12 | | - assert.deepEqual(out, ['a', 'b']); |
13 | 9 |
|
14 | | - console.log('✅ success'); |
15 | | - process.exit(0); |
| 10 | +test('series', async t => { |
| 11 | + t.is(typeof series, 'function'); |
| 12 | + |
| 13 | + t.deepEqual( |
| 14 | + await series([ |
| 15 | + async () => await get(1), |
| 16 | + async () => await get(2) |
| 17 | + ]), |
| 18 | + [1, 2] |
| 19 | + ); |
| 20 | +}); |
| 21 | + |
| 22 | + |
| 23 | +test('parallel', async t => { |
| 24 | + t.is(typeof parallel, 'function', 'should be a function'); |
| 25 | + |
| 26 | + t.deepEqual( |
| 27 | + await parallel([ |
| 28 | + async () => await get(1), |
| 29 | + async () => await get(2) |
| 30 | + ]), |
| 31 | + [1, 2] |
| 32 | + ); |
| 33 | +}); |
| 34 | + |
| 35 | + |
| 36 | +test('map', async t => { |
| 37 | + t.is(typeof map, 'function'); |
| 38 | + |
| 39 | + let fn = spy( async value => (await sleep(50), await get(value * 2)) ); |
| 40 | + |
| 41 | + let start = Date.now(); |
| 42 | + |
| 43 | + let out = await map([1, 2, 3], fn); |
| 44 | + |
| 45 | + t.deepEqual(out, [2, 4, 6]); |
| 46 | + |
| 47 | + let elapsed = Date.now() - start; |
| 48 | + |
| 49 | + t.true(elapsed < 100, 'Should invoke in parallel'); |
16 | 50 | }); |
0 commit comments