From cdcadcad4564ee0d10fed9fdbfcba43c1c3a4ae0 Mon Sep 17 00:00:00 2001 From: VSkits Date: Wed, 9 Jul 2025 15:38:32 +0300 Subject: [PATCH 1/2] salty --- src/reduce.test.js | 61 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/src/reduce.test.js b/src/reduce.test.js index 47a892f..4e6af99 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -2,6 +2,10 @@ const { reduce } = require('./reduce'); +const nums = [1, 2, 3, 4]; +const strings = ['1', '2', '3']; +let callback; + describe('reduce', () => { beforeAll(() => { Array.prototype.reduce2 = reduce; // eslint-disable-line @@ -11,9 +15,62 @@ describe('reduce', () => { delete Array.prototype.reduce2; }); - it('should ', () => { + beforeEach(() => { + callback = jest.fn((acc, curr) => acc + curr); + }); + + it('should have instance of function', () => { + expect(reduce).toBeInstanceOf(Function); + }); + + it('should use callback for all items in array', () => { + const init = 10; + + nums.reduce2(callback, init); + + expect(callback).toHaveBeenCalledTimes(nums.length); + }); + + it('should work witout initial value', () => { + const result = nums.reduce2(callback); + + expect(callback).toHaveBeenCalled(); + expect(callback).toHaveBeenCalledTimes(3); + expect(result).toBe(10); + }); + + it('should work witout initial array', () => { + const init = 10; + const result = [].reduce2(callback, init); + expect(callback).not.toHaveBeenCalled(); + expect(callback).toHaveBeenCalledTimes(0); + expect(result).toBe(10); }); - // Add tests here + it('should return undefined without array and initial value', () => { + const result = [].reduce2(callback); + + expect(result).toBe(undefined); + expect(callback).not.toHaveBeenCalled(); + }); + + it('should work with strings', () => { + const check = '123'; + const result = strings.reduce2(callback); + + expect(result).toBe(check); + }); + + it('should skip initial value if it is specified', () => { + const result = [1, 1, 2].reduce2(callback, null); + + expect(result).toBe(4); + }); + + it('should invoke callback for indexes without value', () => { + [1, '', '', 1, 2].reduce2(callback); + + expect(callback).toHaveBeenCalledTimes(4); + }); }); From c4e65fc892d48e85a61a66d41317b1670e50118d Mon Sep 17 00:00:00 2001 From: VSkits Date: Wed, 9 Jul 2025 15:49:34 +0300 Subject: [PATCH 2/2] fix --- src/reduce.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/reduce.test.js b/src/reduce.test.js index 4e6af99..7d07230 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -31,7 +31,7 @@ describe('reduce', () => { expect(callback).toHaveBeenCalledTimes(nums.length); }); - it('should work witout initial value', () => { + it('should work without initial value', () => { const result = nums.reduce2(callback); expect(callback).toHaveBeenCalled(); @@ -39,7 +39,7 @@ describe('reduce', () => { expect(result).toBe(10); }); - it('should work witout initial array', () => { + it('should work without initial array', () => { const init = 10; const result = [].reduce2(callback, init);