From f5b9840c4bf5a55436750e373d72af8c91f494bf Mon Sep 17 00:00:00 2001 From: Dmytro Nikulin Date: Wed, 4 Mar 2026 16:40:22 +0200 Subject: [PATCH 1/2] Add solution --- src/reduce.test.js | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/reduce.test.js b/src/reduce.test.js index 47a892f..4cdec53 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -11,9 +11,48 @@ describe('reduce', () => { delete Array.prototype.reduce2; }); - it('should ', () => { + it('should call a callback for every item in an array', () => { + const cb = jest.fn(); + [1, 2, 3, 4].reduce2(cb, 0); + expect(cb).toHaveBeenCalledTimes(4); }); // Add tests here + + it(`should call a callback for every item in an array + (minus one when no initial value is provided)`, () => { + const cb = jest.fn(); + + [1, 2, 3, 4].reduce2(cb); + expect(cb).toHaveBeenCalledTimes(3); + }); + + it('should not call a callback if array is empty', () => { + const cb = jest.fn(); + + [].reduce2(cb, 0); + expect(cb).not.toHaveBeenCalled(); + }); + + it('should pass an accumulator and current value to a callback', () => { + const cb = jest.fn((acc, curr) => acc + curr); + + [1, 2, 3, 4].reduce2(cb, 0); + + expect(cb).toHaveBeenNthCalledWith( + 4, 6, 4, expect.anything(), expect.anything() + ); + }); + + it('should return a value', () => { + expect([].reduce2(() => {}, 0)).toBeDefined(); + }); + + it('should correctly return a value calculated from an array', () => { + const cb = jest.fn((acc, curr) => acc + curr); + const result = [1, 2, 3, 4].reduce2(cb, 0); + + expect(result).toBe(10); + }); }); From ba5cff32f6b6ed366b6f3ef3dedb478566e87b0c Mon Sep 17 00:00:00 2001 From: Dmytro Nikulin Date: Wed, 4 Mar 2026 16:48:33 +0200 Subject: [PATCH 2/2] Edge cases added --- src/reduce.test.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/reduce.test.js b/src/reduce.test.js index 4cdec53..82054f0 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -28,11 +28,29 @@ describe('reduce', () => { expect(cb).toHaveBeenCalledTimes(3); }); - it('should not call a callback if array is empty', () => { + it(`should not call a callback and return initial value + if array is empty`, () => { const cb = jest.fn(); + const result = [].reduce2(cb, 0); - [].reduce2(cb, 0); expect(cb).not.toHaveBeenCalled(); + expect(result).toBe(0); + }); + + it(`should throw a TypeError if array is empty + while no initial value is given`, () => { + const cb = jest.fn(); + + expect(() => [].reduce2(cb)).toThrow(TypeError); + }); + + it(`should return an item from array with a single item + when no initial value is given`, () => { + const cb = jest.fn(); + const result = [1].reduce2(cb); + + expect(cb).not.toHaveBeenCalled(); + expect(result).toBe(1); }); it('should pass an accumulator and current value to a callback', () => { @@ -41,7 +59,11 @@ describe('reduce', () => { [1, 2, 3, 4].reduce2(cb, 0); expect(cb).toHaveBeenNthCalledWith( - 4, 6, 4, expect.anything(), expect.anything() + 4, + 6, + 4, + expect.anything(), + expect.anything(), ); });