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.
2.0 kB · 75 lines
JavaScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576import asyncMap from './map';
import { doubleInputArr, duplicate, duplicateInRandomTime, getTimer, inputArr, makeDelayed,} from '../test-utils';
describe('asyncMap()', () => { it('example from README works as described', async () => { const asyncMappedArr = await asyncMap([1, 2, 3], async (el) => el * 2); // [2, 4, 6]
expect(asyncMappedArr).toEqual([2, 4, 6]); });
it('takes exactly the time necessary to execute', async () => { const delay = 100;
const timer = getTimer();
timer.start();
await asyncMap([1, 2, 3], makeDelayed((el) => el * 2, delay));
const timeElapsed = timer.stop();
expect(timeElapsed).toBeGreaterThanOrEqual(delay); expect(timeElapsed).toBeLessThan(delay + 10); });
it.skip('assertions below are valid for synchronous .map()', () => { const mapper = jest.fn().mockImplementation(duplicate);
inputArr.map(mapper);
expect.assertions(1 + inputArr.length);
expect(mapper).toHaveBeenCalledTimes(inputArr.length); inputArr.forEach((el, idx) => { expect(mapper).toHaveBeenCalledWith(el, idx, inputArr); }); });
it('maps values properly', async () => { const mapper = jest.fn().mockImplementation(duplicateInRandomTime);
await asyncMap(inputArr, mapper);
expect.assertions(1 + inputArr.length);
expect(mapper).toHaveBeenCalledTimes(inputArr.length); inputArr.forEach((el, idx) => { expect(mapper).toHaveBeenCalledWith(el, idx, inputArr); }); });
it.skip('assertions below are valid for synchronous .map()', () => { const mapper = jest.fn().mockImplementation(duplicate);
const result = inputArr.map(mapper);
expect(result).toEqual(doubleInputArr); });
it('returns result properly', async () => { const mapper = jest.fn().mockImplementation(duplicateInRandomTime);
const result = await asyncMap(inputArr, mapper);
expect(result).toEqual(doubleInputArr); });});