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.
3.8 kB · 137 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138import { assertType, describe, expect, it, vi } from 'vitest';
import asyncMapStrict from './map_strict.js';
import { doubleInputArr, duplicate, duplicateInRandomTime, getTimer, inputArr, makeDelayed, makePushDuplicate, makePushDuplicateInRandomTime, throws,} from '../test-utils.js';
describe('asyncMapStrict()', () => { it('example from README works as described', async () => { const indexes: number[] = [];
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).toBeGreaterThanOrEqual(delay * 3 - 1); expect(timeElapsed).toBeLessThan(delay * 1.25 * 3); });
it.skip('assertions below are valid for synchronous .map()', () => { const mapper = vi.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 = vi.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 = vi.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 = vi.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 = vi.fn().mockImplementation(duplicate);
const result = inputArr.map(mapper);
expect(result).toEqual(doubleInputArr); });
it('returns result properly', async () => { const mapper = vi.fn().mockImplementation(duplicateInRandomTime);
const result = await asyncMapStrict(inputArr, mapper);
expect(result).toEqual(doubleInputArr); });
it.skip('assertions below are valid for synchronous .map()', () => { const mapper = vi.fn().mockImplementation(throws);
expect(() => inputArr.map(mapper)).toThrow('Some error'); });
it('throws if function passed throws', async () => { const mapper = vi.fn().mockImplementation(throws);
await expect(() => asyncMapStrict(inputArr, mapper)).rejects.toThrow('Some error'); });
it('returns type string[] given function that returns type Promise<string>', async () => { async function stringInRandomTime() { return new Promise<string>((resolve) => setTimeout(() => { resolve('foo'); }, Math.random() * 100), ); }
const result = await asyncMapStrict([1, 2, 3], stringInRandomTime);
assertType<string[]>(result);
expect(result).toEqual(['foo', 'foo', 'foo']); });});