|
| 1 | +import asyncFilterStrict from './filter_strict'; |
| 2 | + |
| 3 | +import { |
| 4 | + doubleInputArr, |
| 5 | + inputArr, |
| 6 | + largerThanTwo, |
| 7 | + largerThanTwoInRandomTime, |
| 8 | + makePushDuplicate, |
| 9 | + makePushDuplicateInRandomTime, |
| 10 | +} from '../test-utils'; |
| 11 | + |
| 12 | +describe('asyncFilterStrict()', () => { |
| 13 | + it.skip('assertions below are valid for synchronous .filter()', () => { |
| 14 | + const filter = jest.fn().mockImplementation(largerThanTwo); |
| 15 | + |
| 16 | + inputArr.filter(filter); |
| 17 | + |
| 18 | + expect.assertions(1 + inputArr.length); |
| 19 | + |
| 20 | + expect(filter).toHaveBeenCalledTimes(inputArr.length); |
| 21 | + inputArr.forEach((el, idx) => { |
| 22 | + expect(filter).toHaveBeenCalledWith(el, idx, inputArr); |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + it('iterates over values properly', async () => { |
| 27 | + const filter = jest.fn().mockImplementation(largerThanTwoInRandomTime); |
| 28 | + |
| 29 | + await asyncFilterStrict(inputArr, filter); |
| 30 | + |
| 31 | + expect.assertions(1 + inputArr.length); |
| 32 | + |
| 33 | + expect(filter).toHaveBeenCalledTimes(inputArr.length); |
| 34 | + inputArr.forEach((el, idx) => { |
| 35 | + expect(filter).toHaveBeenCalledWith(el, idx, inputArr); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + it.skip('assertions below are valid for synchronous .map()', () => { |
| 40 | + const [arr, pushDuplicate] = makePushDuplicate(); |
| 41 | + const mapper = jest.fn().mockImplementation(pushDuplicate); |
| 42 | + |
| 43 | + inputArr.filter(mapper); |
| 44 | + |
| 45 | + expect(mapper).toHaveBeenCalledTimes(inputArr.length); |
| 46 | + expect(arr).toEqual(doubleInputArr); |
| 47 | + }); |
| 48 | + |
| 49 | + it('iterates through an array properly with side effects', async () => { |
| 50 | + const [arr, pushDuplicate] = makePushDuplicateInRandomTime(); |
| 51 | + const mapper = jest.fn().mockImplementation(pushDuplicate); |
| 52 | + |
| 53 | + await asyncFilterStrict(inputArr, mapper); |
| 54 | + |
| 55 | + expect(mapper).toHaveBeenCalledTimes(inputArr.length); |
| 56 | + expect(arr).toEqual(doubleInputArr); |
| 57 | + }); |
| 58 | + |
| 59 | + it.skip('assertions below are valid for synchronous .filter()', () => { |
| 60 | + const filter = jest.fn().mockImplementation(largerThanTwo); |
| 61 | + |
| 62 | + const result = inputArr.filter(filter); |
| 63 | + |
| 64 | + expect(result).toEqual([3, 4, 5, 6, 7, 8, 9, 10]); |
| 65 | + }); |
| 66 | + |
| 67 | + it('filters array properly', async () => { |
| 68 | + const filter = jest.fn().mockImplementation(largerThanTwoInRandomTime); |
| 69 | + |
| 70 | + const result = await asyncFilterStrict(inputArr, filter); |
| 71 | + |
| 72 | + expect(result).toEqual([3, 4, 5, 6, 7, 8, 9, 10]); |
| 73 | + }); |
| 74 | +}); |
0 commit comments