From 0262ce670a4c4042b338781662d7e456e943a446 Mon Sep 17 00:00:00 2001 From: melarkkkk Date: Sun, 28 Dec 2025 16:11:52 +0100 Subject: [PATCH 1/3] add task solution --- src/reduce.test.js | 70 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/src/reduce.test.js b/src/reduce.test.js index 47a892f..1b3f815 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -11,9 +11,75 @@ describe('reduce', () => { delete Array.prototype.reduce2; }); - it('should ', () => { + it('should be declared', () => { + expect(reduce).toBeInstanceOf(Function); + }); + + it('should not mutate the original array', () => { + const array = [1, 2, 3, 4, 5]; + const copy = [...array]; + + array.reduce((prev, curr) => prev + curr, 0); + + expect(array).toEqual(copy); + }); + + it('should return a value', () => { + const arr = [1, 2, 3]; + + const result = arr.reduce((acc, x) => acc + x, 0); + expect(result).toBeDefined(); }); - // Add tests here + it('should return the same type as initial value', () => { + const arr = [1, 2, 3]; + + const result = arr.reduce((acc, x) => { + acc.push(x); + + return acc; + }, []); + + expect(Array.isArray(result)).toBe(true); + }); + + it('should call callback once per element', () => { + const arr = [1, 2, 3]; + const cb = jest.fn((acc, x) => acc + x); + + arr.reduce(cb, 0); + + expect(cb).toHaveBeenCalledTimes(arr.length); + }); + + it('should pass accumulator, current value, index and array', () => { + const arr = [10, 20]; + const cb = jest.fn((acc, x) => acc + x); + + arr.reduce(cb, 0); + + expect(cb).toHaveBeenNthCalledWith( + 1, + 0, + 10, + 0, + arr + ); + }); + + it('should not call callback for empty array', () => { + const cb = jest.fn(); + + const result = [].reduce(cb, 100); + + expect(cb).not.toHaveBeenCalled(); + expect(result).toBe(100); + }); + + it('should throw error on empty array without initial value', () => { + expect(() => { + [].reduce(() => {}); + }).toThrow(TypeError); + }); }); From d983abcc4e125119c51d741c9de61a10671f85a9 Mon Sep 17 00:00:00 2001 From: melarkkkk Date: Sun, 28 Dec 2025 16:23:54 +0100 Subject: [PATCH 2/3] fix: fixed all the incorrect method names --- src/reduce.test.js | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/reduce.test.js b/src/reduce.test.js index 1b3f815..3094038 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -11,15 +11,11 @@ describe('reduce', () => { delete Array.prototype.reduce2; }); - it('should be declared', () => { - expect(reduce).toBeInstanceOf(Function); - }); - it('should not mutate the original array', () => { const array = [1, 2, 3, 4, 5]; const copy = [...array]; - array.reduce((prev, curr) => prev + curr, 0); + array.reduce2((prev, curr) => prev + curr, 0); expect(array).toEqual(copy); }); @@ -27,7 +23,7 @@ describe('reduce', () => { it('should return a value', () => { const arr = [1, 2, 3]; - const result = arr.reduce((acc, x) => acc + x, 0); + const result = arr.reduce2((acc, x) => acc + x, 0); expect(result).toBeDefined(); }); @@ -35,7 +31,7 @@ describe('reduce', () => { it('should return the same type as initial value', () => { const arr = [1, 2, 3]; - const result = arr.reduce((acc, x) => { + const result = arr.reduce2((acc, x) => { acc.push(x); return acc; @@ -48,7 +44,7 @@ describe('reduce', () => { const arr = [1, 2, 3]; const cb = jest.fn((acc, x) => acc + x); - arr.reduce(cb, 0); + arr.reduce2(cb, 0); expect(cb).toHaveBeenCalledTimes(arr.length); }); @@ -57,7 +53,7 @@ describe('reduce', () => { const arr = [10, 20]; const cb = jest.fn((acc, x) => acc + x); - arr.reduce(cb, 0); + arr.reduce2(cb, 0); expect(cb).toHaveBeenNthCalledWith( 1, @@ -71,15 +67,9 @@ describe('reduce', () => { it('should not call callback for empty array', () => { const cb = jest.fn(); - const result = [].reduce(cb, 100); + const result = [].reduce2(cb, 100); expect(cb).not.toHaveBeenCalled(); expect(result).toBe(100); }); - - it('should throw error on empty array without initial value', () => { - expect(() => { - [].reduce(() => {}); - }).toThrow(TypeError); - }); }); From 9da2e8382f1fbf3de9735581ea800096d6769bc2 Mon Sep 17 00:00:00 2001 From: melarkkkk Date: Sun, 28 Dec 2025 16:32:12 +0100 Subject: [PATCH 3/3] feat: add additional tests --- src/reduce.test.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/reduce.test.js b/src/reduce.test.js index 3094038..181c3d4 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -1,4 +1,5 @@ 'use strict'; +/* eslint-disable max-len */ const { reduce } = require('./reduce'); @@ -28,6 +29,13 @@ describe('reduce', () => { expect(result).toBeDefined(); }); + it('should reduce array without initial value', () => { + const arr = [1, 2, 3]; + const result = arr.reduce2((acc, curr) => acc + curr); + + expect(result).toBe(6); + }); + it('should return the same type as initial value', () => { const arr = [1, 2, 3]; @@ -40,6 +48,21 @@ describe('reduce', () => { expect(Array.isArray(result)).toBe(true); }); + it.skip('should throw TypeError when reducing empty array without initial value', () => { + const arr = []; + + expect(() => arr.reduce2((acc, curr) => acc + curr)).toThrow(TypeError); + }); + + it('should work with the single element array correctly', () => { + const arr = [42]; + const result1 = arr.reduce2((acc, curr) => acc + curr); + const result2 = arr.reduce2((acc, curr) => acc + curr, -42); + + expect(result1).toBe(42); + expect(result2).toBe(0); + }); + it('should call callback once per element', () => { const arr = [1, 2, 3]; const cb = jest.fn((acc, x) => acc + x);