diff --git a/src/reduce.test.js b/src/reduce.test.js index 47a892f..c74f018 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -11,9 +11,62 @@ describe('reduce', () => { delete Array.prototype.reduce2; }); - it('should ', () => { + let callback; + beforeEach(() => { + callback = jest.fn().mockImplementation((a, b) => a + b); }); - // Add tests here + it('should be declared', () => { + expect(reduce).toBeInstanceOf(Function); + }); + + it('should not mutate array', () => { + const array = [1, 2, 3, 4]; + + const copy = [...array]; + + array.reduce2(callback, 0); + + expect(array).toEqual(copy); + }); + + it('should run callback array`s length times if initialValue is', () => { + const array = [1, 2, 3, 4]; + + const result = array.reduce2(callback, 0); + + expect(callback).toHaveBeenCalledTimes(array.length); ; + expect(result).toBe(10); + }); + + it('should run callback array`s length - 1 times if no initialValue', () => { + const array = [1, 2, 3, 4]; + + const result = array.reduce2(callback); + + expect(callback).toHaveBeenCalledTimes(array.length - 1); ; + expect(result).toBe(10); + }); + + it('should run callback with correct arguments', () => { + const array = [1, 2, 3, 4]; + + const result = array.reduce2(callback, 0); + + expect(callback).toHaveBeenNthCalledWith(1, 0, 1, 0, array); + expect(callback).toHaveBeenNthCalledWith(2, 1, 2, 1, array); + expect(callback).toHaveBeenNthCalledWith(3, 3, 3, 2, array); + expect(callback).toHaveBeenNthCalledWith(4, 6, 4, 3, array); + + expect(result).toBe(10); + }); + + it('should return initial value if array is empty', () => { + const array = []; + const initialValue = 0; + const result = array.reduce2(callback, initialValue); + + expect(result).toBe(initialValue); ; + }); });