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.
4.7 kB · 162 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163import { assertType, describe, expect, it, vi } from 'vitest';
import asyncFindIndex from './findIndex.js';
import { getTimer, inputArr, largerThanOneHundred, largerThanOneHundredInRandomTime, largerThanTwo, largerThanTwoInRandomTime, makeDelayed, throws,} from '../test-utils.js';
const firstElementLargerThanTwo = inputArr.findIndex(largerThanTwo);
describe('asyncFindIndex()', () => { it('example from README works as described', async () => { const asyncFoundEl = await asyncFindIndex([1, 2, 3], async (el) => el > 1);
expect(asyncFoundEl).toEqual(1); });
it('takes exactly the time necessary to execute', async () => { const delay = 100;
const timer = getTimer();
timer.start();
await asyncFindIndex( [1, 2, 3], makeDelayed((el) => el > 1, delay), );
const timeElapsed = timer.stop();
expect(timeElapsed).toBeGreaterThanOrEqual(delay * 2 - 1); expect(timeElapsed).toBeLessThan(delay * 1.25 * 2); });
it('assertions below are valid for synchronous .find()', () => { const filter = vi.fn().mockImplementation(largerThanTwo);
inputArr.find(filter);
expect.assertions(1 + firstElementLargerThanTwo);
expect(filter).toHaveBeenCalledTimes(firstElementLargerThanTwo + 1); inputArr.slice(0, firstElementLargerThanTwo).forEach((el, idx) => { expect(filter).toHaveBeenCalledWith(el, idx, inputArr); }); });
it('iterates over values properly', async () => { const filter = vi.fn().mockImplementation(largerThanTwoInRandomTime);
await asyncFindIndex(inputArr, filter);
expect.assertions(1 + firstElementLargerThanTwo);
expect(filter).toHaveBeenCalledTimes(firstElementLargerThanTwo + 1); inputArr.slice(0, firstElementLargerThanTwo).forEach((el, idx) => { expect(filter).toHaveBeenCalledWith(el, idx, inputArr); }); });
it.skip('assertions below are valid for synchronous .find()', () => { const filter = vi.fn().mockImplementation(largerThanTwo);
inputArr.find(filter);
expect.assertions(1 + firstElementLargerThanTwo);
expect(filter).toHaveBeenCalledTimes(firstElementLargerThanTwo + 1); inputArr.slice(0, firstElementLargerThanTwo).forEach((el, idx) => { expect(filter).toHaveBeenCalledWith(el, idx, inputArr); }); });
it('iterates over values properly', async () => { const filter = vi.fn().mockImplementation(largerThanTwoInRandomTime);
await asyncFindIndex(inputArr, filter);
expect.assertions(1 + firstElementLargerThanTwo);
expect(filter).toHaveBeenCalledTimes(firstElementLargerThanTwo + 1); inputArr.slice(0, firstElementLargerThanTwo).forEach((el, idx) => { expect(filter).toHaveBeenCalledWith(el, idx, inputArr); }); });
it.skip('assertions below are valid for synchronous .find()', () => { const filter = vi.fn().mockImplementation(largerThanTwo);
const result = inputArr.find(filter);
expect(result).toEqual(2); });
it('returns truthy result properly', async () => { const filter = vi.fn().mockImplementation(largerThanTwoInRandomTime);
const result = await asyncFindIndex(inputArr, filter);
expect(result).toEqual(2); });
it.skip('assertions below are valid for synchronous .find()', () => { const filter = vi.fn().mockImplementation(largerThanOneHundred);
const result = inputArr.find(filter);
expect(result).toEqual(-1); });
it('returns undefined result properly', async () => { const filter = vi.fn().mockImplementation(largerThanOneHundredInRandomTime);
const result = await asyncFindIndex(inputArr, filter);
expect(result).toEqual(-1); });
it.skip('assertions below are valid for synchronous .find()', () => { const filter = vi.fn().mockImplementation(throws);
expect(() => inputArr.find(filter)).toThrow(); });
it('throws if function passed throws', async () => { const filter = vi.fn().mockImplementation(throws);
await expect(asyncFindIndex(inputArr, filter)).rejects.toThrow('Some error'); });
it('returns type number given function that returns type Promise<boolean>', async () => { const result = await asyncFindIndex(inputArr, largerThanTwoInRandomTime);
assertType<number>(result);
expect(typeof result).toEqual('number'); });
it('returns type -1 given function that returns type Promise<false>', async () => { async function falseInRandomTime() { return new Promise<false>((resolve) => setTimeout(() => { resolve(false); }, Math.random() * 100), ); }
const result = await asyncFindIndex(inputArr, falseInRandomTime);
assertType<-1>(result);
expect(result).toEqual(-1); });});