From 1756bae4bced1e7dd22b9ac5b1a3ff7a4494be7d Mon Sep 17 00:00:00 2001 From: Anna Nikiforova Date: Tue, 30 Dec 2025 23:13:40 +0400 Subject: [PATCH 1/3] qa array reduce method solution --- src/reduce.test.js | 66 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/src/reduce.test.js b/src/reduce.test.js index 47a892f..3411ce1 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -11,9 +11,71 @@ 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]; + + array.reduce2(callback, 0); + + expect(array).toEqual(array); + }); + + it('should run callback array`s length times if initialValue is', () => { + const array = [1, 2, 3, 4]; + + array.reduce2(callback, 0); + + expect(callback).toHaveBeenCalledTimes(array.length); ; + }); + + it('should run callback array`s length - 1 times if no initialValue', () => { + const array = [1, 2, 3, 4]; + + array.reduce2(callback); + + expect(callback).toHaveBeenCalledTimes(array.length - 1); ; + }); + + it('should not run callback if array is empty', () => { + const array = []; + + array.reduce2(callback); + + expect(callback).toHaveBeenCalledTimes(0); ; + }); + + it('should run callback with correct arguments', () => { + const array = [1, 2, 3, 4]; + + 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); + }); + + it('should return initial value if array is empty', () => { + const array = []; + const initialValue = 0; + const result = array.reduce2(callback, initialValue); + + expect(result).toBe(initialValue); ; + }); + + it('should return undefined if array is empty and no initial value', () => { + const array = []; + const result = array.reduce2(callback); + + expect(result).toBe(undefined); ; + }); }); From 9556f8df7c4e5a254006740f1fdf81465ed09c6d Mon Sep 17 00:00:00 2001 From: Anna Nikiforova Date: Tue, 30 Dec 2025 23:37:57 +0400 Subject: [PATCH 2/3] fix the issues --- src/reduce.test.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/reduce.test.js b/src/reduce.test.js index 3411ce1..5a0edc5 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -24,9 +24,11 @@ describe('reduce', () => { it('should not mutate array', () => { const array = [1, 2, 3, 4]; + const copy = [...array]; + array.reduce2(callback, 0); - expect(array).toEqual(array); + expect(array).toEqual(copy); }); it('should run callback array`s length times if initialValue is', () => { @@ -56,12 +58,14 @@ describe('reduce', () => { it('should run callback with correct arguments', () => { const array = [1, 2, 3, 4]; - array.reduce2(callback, 0); + 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', () => { @@ -71,11 +75,4 @@ describe('reduce', () => { expect(result).toBe(initialValue); ; }); - - it('should return undefined if array is empty and no initial value', () => { - const array = []; - const result = array.reduce2(callback); - - expect(result).toBe(undefined); ; - }); }); From 4dec39b8f1bb052d56e40bf9945a446a872222c6 Mon Sep 17 00:00:00 2001 From: Anna Nikiforova Date: Wed, 31 Dec 2025 00:11:32 +0400 Subject: [PATCH 3/3] fix the issues 2 --- src/reduce.test.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/reduce.test.js b/src/reduce.test.js index 5a0edc5..c74f018 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -34,25 +34,19 @@ describe('reduce', () => { it('should run callback array`s length times if initialValue is', () => { const array = [1, 2, 3, 4]; - array.reduce2(callback, 0); + 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]; - array.reduce2(callback); + const result = array.reduce2(callback); expect(callback).toHaveBeenCalledTimes(array.length - 1); ; - }); - - it('should not run callback if array is empty', () => { - const array = []; - - array.reduce2(callback); - - expect(callback).toHaveBeenCalledTimes(0); ; + expect(result).toBe(10); }); it('should run callback with correct arguments', () => {