Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/wojtekmaj/async-array-utils. A collection of array-related async utilities.
array array-methods async promise utils
Something went wrong. Try again.
1.4 kB · 51 lines
JavaScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152import { getTimer, makeDelayed } from '../test-utils';import asyncReduce from './reduce';
describe('asyncReduce()', () => { it('example from README works as described', async () => { const result = await asyncReduce( [1, 2, 3], async (tmp, cur) => tmp + cur, 0, );
expect(result).toBe(6); });
it('takes exactly the time necessary to execute', async () => { const delay = 100;
const timer = getTimer();
timer.start();
await asyncReduce( [1, 2, 3], makeDelayed((tmp, cur) => tmp + cur, delay), 0, );
const timeElapsed = timer.stop();
expect(timeElapsed).toBeGreaterThan(delay * 3); expect(timeElapsed).toBeLessThan((delay + 10) * 3); });
it.skip('assertions below are valid for synchronous .reduce()', () => { const mapper = jest.fn().mockImplementation((temp, cur, idx) => [...temp, `${idx}:${cur}`]);
const result = ['a', 'b', 'c'].reduce(mapper, ['start']);
expect(mapper).toHaveBeenCalledTimes(3); expect(result).toEqual(['start', '0:a', '1:b', '2:c']); });
it('reduces an array properly', async () => { const mapper = jest.fn().mockImplementation(async (temp, cur, idx) => [...temp, `${idx}:${cur}`]);
const result = await asyncReduce(['a', 'b', 'c'], mapper, ['start']);
expect(mapper).toHaveBeenCalledTimes(3); expect(result).toEqual(['start', '0:a', '1:b', '2:c']); });});