From 7e1ff47991edfa4b02bc12a18de3497b45083f5e Mon Sep 17 00:00:00 2001 From: Heorhii Savostikov Date: Sun, 29 Mar 2026 14:30:43 +0300 Subject: [PATCH 1/4] Added some new tests for the function --- src/reduce.test.js | 68 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/src/reduce.test.js b/src/reduce.test.js index c3331e7..7a0d5ca 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -3,6 +3,8 @@ const { reduce } = require('./reduce'); describe('reduce', () => { + let array = [1, 2, 3, 4, 5, 6]; + beforeAll(() => { Array.prototype.reduce2 = reduce; // eslint-disable-line }); @@ -11,7 +13,69 @@ describe('reduce', () => { delete Array.prototype.reduce2; }); - it('should ', () => {}); + it('should be declared', () => { + expect(Array.prototype.reduce2).toBeInstanceOf(Function); + }); + + it('should sum array with initial value', () => { + const callback = jest.fn((prev, x) => prev + x); + + const result = array.reduce2(callback, 0); + + expect(result).toBe(21); + expect(callback).toHaveBeenCalledTimes(6); + }); + + it('should sum array without initial value', () => { + const callback = jest.fn((prev, x) => prev + x); + + const result = array.reduce2(callback); + + expect(result).toBe(21); + expect(callback).toHaveBeenCalledTimes(5); + }); + + it('should return initial value for empty array', () => { + array = []; + + const callback = jest.fn((prev, x) => prev + x); + + const result = array.reduce2(callback, 0); + + expect(result).toBe(0); + expect(callback).not.toHaveBeenCalled(); + }); - // Add tests here + it('should return undefined for empty array without initial value', () => { + array = []; + + const callback = jest.fn((prev, x) => prev + x); + + const result = array.reduce2(callback); + + expect(result).toBe(undefined); + expect(callback).not.toHaveBeenCalled(); + }); + + it('should work with single element and initial value', () => { + array = [1]; + + const callback = jest.fn((prev, x) => prev + x); + + const result = array.reduce2(callback, 2); + + expect(result).toBe(3); + expect(callback).toHaveBeenCalledTimes(1); + }); + + it('should return element if single element and no initial value', () => { + array = [1]; + + const callback = jest.fn((prev, x) => prev + x); + + const result = array.reduce2(callback); + + expect(result).toBe(1); + expect(callback).not.toHaveBeenCalled(); + }); }); From f5252dca3e191a2a6bdbb5f8f85e72e37a1b2045 Mon Sep 17 00:00:00 2001 From: Heorhii Savostikov Date: Sun, 29 Mar 2026 14:39:24 +0300 Subject: [PATCH 2/4] Deleted one test --- src/reduce.test.js | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/src/reduce.test.js b/src/reduce.test.js index 7a0d5ca..c7e5846 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -3,8 +3,6 @@ const { reduce } = require('./reduce'); describe('reduce', () => { - let array = [1, 2, 3, 4, 5, 6]; - beforeAll(() => { Array.prototype.reduce2 = reduce; // eslint-disable-line }); @@ -18,6 +16,7 @@ describe('reduce', () => { }); it('should sum array with initial value', () => { + const array = [1, 2, 3, 4, 5, 6]; const callback = jest.fn((prev, x) => prev + x); const result = array.reduce2(callback, 0); @@ -27,6 +26,7 @@ describe('reduce', () => { }); it('should sum array without initial value', () => { + const array = [1, 2, 3, 4, 5, 6]; const callback = jest.fn((prev, x) => prev + x); const result = array.reduce2(callback); @@ -36,7 +36,7 @@ describe('reduce', () => { }); it('should return initial value for empty array', () => { - array = []; + const array = []; const callback = jest.fn((prev, x) => prev + x); @@ -46,19 +46,8 @@ describe('reduce', () => { expect(callback).not.toHaveBeenCalled(); }); - it('should return undefined for empty array without initial value', () => { - array = []; - - const callback = jest.fn((prev, x) => prev + x); - - const result = array.reduce2(callback); - - expect(result).toBe(undefined); - expect(callback).not.toHaveBeenCalled(); - }); - it('should work with single element and initial value', () => { - array = [1]; + const array = [1]; const callback = jest.fn((prev, x) => prev + x); @@ -69,7 +58,7 @@ describe('reduce', () => { }); it('should return element if single element and no initial value', () => { - array = [1]; + const array = [1]; const callback = jest.fn((prev, x) => prev + x); From d62bdd39600ebde0e4069eb61eca63e3b7104e88 Mon Sep 17 00:00:00 2001 From: Heorhii Savostikov Date: Sun, 29 Mar 2026 14:44:07 +0300 Subject: [PATCH 3/4] Returned one test --- src/reduce.test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/reduce.test.js b/src/reduce.test.js index c7e5846..f128faf 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -46,6 +46,16 @@ describe('reduce', () => { expect(callback).not.toHaveBeenCalled(); }); + it('should return undefined for empty array without initial value', () => { + const array = []; + + const callback = jest.fn((prev, x) => prev + x); + + const result = () => array.reduce2(callback); + + expect(result).toThrow(); + }); + it('should work with single element and initial value', () => { const array = [1]; From 494e8f1433a2427e0d91db426f5403f201394de1 Mon Sep 17 00:00:00 2001 From: Heorhii Savostikov Date: Sun, 29 Mar 2026 14:53:51 +0300 Subject: [PATCH 4/4] Changed one test --- src/reduce.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/reduce.test.js b/src/reduce.test.js index f128faf..aafd7f0 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -51,7 +51,7 @@ describe('reduce', () => { const callback = jest.fn((prev, x) => prev + x); - const result = () => array.reduce2(callback); + const result = () => array.reduce(callback); expect(result).toThrow(); });