From 0c80ac2f9a0bd90c25c46b1298222b4149dbda8a Mon Sep 17 00:00:00 2001 From: Maksym Korchynskyi Date: Sun, 22 Jun 2025 20:00:30 +0300 Subject: [PATCH 1/2] add tests --- src/reduce.test.js | 104 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 3 deletions(-) diff --git a/src/reduce.test.js b/src/reduce.test.js index 47a892f..dbb1377 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -2,7 +2,7 @@ const { reduce } = require('./reduce'); -describe('reduce', () => { +describe('The reduce function', () => { beforeAll(() => { Array.prototype.reduce2 = reduce; // eslint-disable-line }); @@ -11,9 +11,107 @@ describe('reduce', () => { delete Array.prototype.reduce2; }); - it('should ', () => { + it('should be declared', () => { + expect(reduce).toBeInstanceOf(Function); + }); + + describe('With a start value', () => { + it('should not call the callback for an empty array', () => { + const fn = jest.fn(); + const startValue = 10; + + [].reduce2(fn, startValue); + expect(fn).not.toHaveBeenCalled(); + }); + + it('should return the start value for an empty array', () => { + const fn = () => 1; + const startValue = 10; + + expect([].reduce2(fn, startValue)).toBe(startValue); + }); + + it('should call the callback for each item', () => { + const fn = jest.fn(); + const items = [1, 2, 3]; + items.reduce2(fn, 0); + expect(fn).toHaveBeenCalledTimes(items.length); + }); + + it('should pass the correct arguments to the callback', () => { + const startValue = 0; + const items = [1, 2, 3]; + const fn = jest.fn((_acc, v) => v); + + items.reduce2(fn, startValue); + + for (let i = 0; i < items.length; i++) { + expect(fn).toHaveBeenNthCalledWith( + i + 1, + items[i - 1] || startValue, + items[i], + i, + items, + ); + } + }); + + it('should return the correct result', () => { + const items = [1, 2, 3]; + const sumFn = jest.fn((sum, v) => sum + v); + + expect(items.reduce2(sumFn, 0)).toBe(6); + }); }); - // Add tests here + describe('Without a start value', () => { + it('should not call the callback for an empty array', () => { + const fn = jest.fn(); + + [].reduce2(fn); + expect(fn).not.toHaveBeenCalled(); + }); + + it('should return undefined for an empty array', () => { + const items = []; + const fn = jest.fn(() => 1); + + expect(items.reduce2(fn)).toBeUndefined(); + }); + + it('should use the first item as the start value', () => { + const fn = jest.fn(); + const items = [1, 2, 3]; + + items.reduce2(fn); + expect(fn).toHaveBeenNthCalledWith(1, items[0], items[1], 1, items); + }); + + it('should skip the first item', () => { + const fn = jest.fn(); + const items = [1, 2, 3]; + + items.reduce2(fn); + expect(fn).toHaveBeenCalledTimes(items.length - 1); + }); + + it('should pass the correct arguments to the callback', () => { + const items = [1, 2, 3]; + const fn = jest.fn((_acc, v) => v); + + items.reduce2(fn); + + for (let i = 1; i < items.length; i++) { + expect(fn).toHaveBeenNthCalledWith(i, items[i - 1], items[i], i, items); + } + }); + + it('should return the correct result', () => { + const items = [1, 2, 3]; + const sumFn = jest.fn((sum, v) => sum + v); + + expect(items.reduce2(sumFn)).toBe(6); + }); + }); }); From 7bdf1a0c987b0560cb6c167fdd586771e79016ca Mon Sep 17 00:00:00 2001 From: Maksym Korchynskyi Date: Sun, 22 Jun 2025 20:06:20 +0300 Subject: [PATCH 2/2] add spyOn for reduce --- src/reduce.test.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/reduce.test.js b/src/reduce.test.js index dbb1377..ad28e61 100644 --- a/src/reduce.test.js +++ b/src/reduce.test.js @@ -4,11 +4,11 @@ const { reduce } = require('./reduce'); describe('The reduce function', () => { beforeAll(() => { - Array.prototype.reduce2 = reduce; // eslint-disable-line + jest.spyOn(Array.prototype, 'reduce').mockImplementation(reduce); }); afterAll(() => { - delete Array.prototype.reduce2; + Array.prototype.reduce.mockRestore(); }); it('should be declared', () => { @@ -20,7 +20,7 @@ describe('The reduce function', () => { const fn = jest.fn(); const startValue = 10; - [].reduce2(fn, startValue); + [].reduce(fn, startValue); expect(fn).not.toHaveBeenCalled(); }); @@ -28,14 +28,14 @@ describe('The reduce function', () => { const fn = () => 1; const startValue = 10; - expect([].reduce2(fn, startValue)).toBe(startValue); + expect([].reduce(fn, startValue)).toBe(startValue); }); it('should call the callback for each item', () => { const fn = jest.fn(); const items = [1, 2, 3]; - items.reduce2(fn, 0); + items.reduce(fn, 0); expect(fn).toHaveBeenCalledTimes(items.length); }); @@ -44,7 +44,7 @@ describe('The reduce function', () => { const items = [1, 2, 3]; const fn = jest.fn((_acc, v) => v); - items.reduce2(fn, startValue); + items.reduce(fn, startValue); for (let i = 0; i < items.length; i++) { expect(fn).toHaveBeenNthCalledWith( @@ -61,7 +61,7 @@ describe('The reduce function', () => { const items = [1, 2, 3]; const sumFn = jest.fn((sum, v) => sum + v); - expect(items.reduce2(sumFn, 0)).toBe(6); + expect(items.reduce(sumFn, 0)).toBe(6); }); }); @@ -69,7 +69,7 @@ describe('The reduce function', () => { it('should not call the callback for an empty array', () => { const fn = jest.fn(); - [].reduce2(fn); + [].reduce(fn); expect(fn).not.toHaveBeenCalled(); }); @@ -77,14 +77,14 @@ describe('The reduce function', () => { const items = []; const fn = jest.fn(() => 1); - expect(items.reduce2(fn)).toBeUndefined(); + expect(items.reduce(fn)).toBeUndefined(); }); it('should use the first item as the start value', () => { const fn = jest.fn(); const items = [1, 2, 3]; - items.reduce2(fn); + items.reduce(fn); expect(fn).toHaveBeenNthCalledWith(1, items[0], items[1], 1, items); }); @@ -92,7 +92,7 @@ describe('The reduce function', () => { const fn = jest.fn(); const items = [1, 2, 3]; - items.reduce2(fn); + items.reduce(fn); expect(fn).toHaveBeenCalledTimes(items.length - 1); }); @@ -100,7 +100,7 @@ describe('The reduce function', () => { const items = [1, 2, 3]; const fn = jest.fn((_acc, v) => v); - items.reduce2(fn); + items.reduce(fn); for (let i = 1; i < items.length; i++) { expect(fn).toHaveBeenNthCalledWith(i, items[i - 1], items[i], i, items); @@ -111,7 +111,7 @@ describe('The reduce function', () => { const items = [1, 2, 3]; const sumFn = jest.fn((sum, v) => sum + v); - expect(items.reduce2(sumFn)).toBe(6); + expect(items.reduce(sumFn)).toBe(6); }); }); });