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.7 kB · 102 lines
JavaScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103import asyncEvery from './every';
import { getTimer, inputArr, largerThanOneHundred, largerThanOneHundredInRandomTime, makeDelayed,} from '../test-utils';
function largerOrEqualThanZero(x) { return x >= 0;}
function largerOrEqualThanZeroInRandomTime(x) { return new Promise( (resolve) => setTimeout(() => { resolve(x >= 0); }, Math.random() * 100), );}
describe('asyncEvery()', () => { it('example from README works as described', async () => { const largerThanZero = await asyncEvery([1, 2, 3], async (el) => el > 0);
expect(largerThanZero).toBe(true); });
it('takes exactly the time necessary to execute', async () => { const delay = 100;
const timer = getTimer();
timer.start();
await asyncEvery([1, 2, 3], makeDelayed((el) => el > 0, delay));
const timeElapsed = timer.stop();
expect(timeElapsed).toBeGreaterThanOrEqual(delay); expect(timeElapsed).toBeLessThan(delay + 10); });
it.skip('assertions below are valid for synchronous .every()', () => { const mapper = jest.fn().mockImplementation(largerOrEqualThanZero);
inputArr.every(mapper);
expect.assertions(1 + inputArr.length);
expect(mapper).toHaveBeenCalledTimes(inputArr.length); inputArr.forEach((el, idx) => { expect(mapper).toHaveBeenCalledWith(el, idx, inputArr); }); });
it('iterates over values properly', async () => { const mapper = jest.fn().mockImplementation(largerOrEqualThanZeroInRandomTime);
await asyncEvery(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 .every()', () => { const mapper = jest.fn().mockImplementation(largerOrEqualThanZero);
const result = inputArr.every(mapper);
expect(result).toEqual(true); });
it('returns truthy result properly', async () => { const mapper = jest.fn().mockImplementation(largerOrEqualThanZeroInRandomTime);
const result = await asyncEvery(inputArr, mapper);
expect(result).toEqual(true); });
it.skip('assertions below are valid for synchronous .every()', () => { const mapper = jest.fn().mockImplementation(largerThanOneHundred);
const result = inputArr.every(mapper);
expect(result).toEqual(false); });
it('returns falsy result properly', async () => { const mapper = jest.fn().mockImplementation(largerThanOneHundredInRandomTime);
const result = await asyncEvery(inputArr, mapper);
expect(result).toEqual(false); });});