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 58 59 60 61 62 63 64 65 66 67 68 69
|
import test from 'ava';
import { spy } from 'sinon';
import { series, parallel, map, filter, find, every, some } from '../dist/asyncro.js';
const get = v => Promise.resolve(v);
const sleep = time => new Promise( r => setTimeout(r, time) );
test('series', async t => {
t.is(typeof series, 'function');
t.deepEqual(
await series([
async () => await get(1),
async () => await get(2)
]),
[1, 2]
);
});
test('parallel', async t => {
t.is(typeof parallel, 'function', 'should be a function');
t.deepEqual(
await parallel([
async () => await get(1),
async () => await get(2)
]),
[1, 2]
);
});
test('map', async t => {
t.is(typeof map, 'function');
let fn = spy( async value => (await sleep(50), await get(value * 2)) );
let start = Date.now();
let out = await map([1, 2, 3], fn);
t.deepEqual(out, [2, 4, 6]);
let elapsed = Date.now() - start;
t.true(elapsed < 100, 'Should invoke in parallel');
});
test('baseMap', async t => {
t.is(typeof filter, 'function');
t.is(typeof find, 'function');
t.is(typeof every, 'function');
t.is(typeof some, 'function');
let fn = spy( async value => (await sleep(50), await get(value) > 1) );
let filterOut = await filter([1, 2, 3], fn);
let findOut = await find([1,2,3], fn);
let everyOut = await every([1, 2, 3], fn);
let someOut = await some([1,2,3], fn);
t.deepEqual(filterOut, [2, 3]);
t.deepEqual(findOut, 2);
t.false(everyOut);
t.true(someOut);
});
|