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