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.9 kB · 106 lines
JavaScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107import asyncMapStrict from './map_strict';
import { doubleInputArr, duplicate, duplicateInRandomTime, getTimer, inputArr, makeDelayed, makePushDuplicate, makePushDuplicateInRandomTime,} from '../test-utils';
describe('asyncMapStrict()', () => { it('example from README works as described', async () => { const indexes = [];
const asyncMappedArr = await asyncMapStrict( [1, 2, 3], async (el, index) => { indexes.push(index); return el * 2; }, ); // [2, 4, 6]
expect(asyncMappedArr).toEqual([2, 4, 6]); expect(indexes).toEqual([0, 1, 2]); });
it('takes exactly the time necessary to execute', async () => { const delay = 100;
const timer = getTimer();
timer.start();
await asyncMapStrict([1, 2, 3], makeDelayed((el) => el * 2, delay));
const timeElapsed = timer.stop();
expect(timeElapsed).toBeGreaterThan(delay * 3); expect(timeElapsed).toBeLessThan((delay + 10) * 3); });
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 asyncMapStrict(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 [arr, pushDuplicate] = makePushDuplicate(); const mapper = jest.fn().mockImplementation(pushDuplicate);
inputArr.map(mapper);
expect(mapper).toHaveBeenCalledTimes(inputArr.length); expect(arr).toEqual(doubleInputArr); });
it('maps values properly with side effects', async () => { const [arr, pushDuplicate] = makePushDuplicateInRandomTime(); const mapper = jest.fn().mockImplementation(pushDuplicate);
await asyncMapStrict(inputArr, mapper);
expect(mapper).toHaveBeenCalledTimes(inputArr.length); expect(arr).toEqual(doubleInputArr); });
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 asyncMapStrict(inputArr, mapper);
expect(result).toEqual(doubleInputArr); });});