Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 81 additions & 2 deletions src/reduce.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
/* eslint-disable max-len */

const { reduce } = require('./reduce');

Expand All @@ -11,9 +12,87 @@ describe('reduce', () => {
delete Array.prototype.reduce2;
});

it('should ', () => {
it('should not mutate the original array', () => {
const array = [1, 2, 3, 4, 5];
const copy = [...array];

array.reduce2((prev, curr) => prev + curr, 0);

expect(array).toEqual(copy);
});

it('should return a value', () => {
const arr = [1, 2, 3];

const result = arr.reduce2((acc, x) => acc + x, 0);

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];

const result = arr.reduce2((acc, x) => {
acc.push(x);

return acc;
}, []);

expect(Array.isArray(result)).toBe(true);
});

it.skip('should throw TypeError when reducing empty array without initial value', () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is skipped using it.skip, which means it isn't running. According to the requirements, you need to verify that calling reduce on an empty array without an initial value throws a TypeError. Please remove .skip to enable this important test case.

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);

arr.reduce2(cb, 0);

expect(cb).toHaveBeenCalledTimes(arr.length);
});

// Add tests here
it('should pass accumulator, current value, index and array', () => {
const arr = [10, 20];
const cb = jest.fn((acc, x) => acc + x);

arr.reduce2(cb, 0);

expect(cb).toHaveBeenNthCalledWith(
1,
0,
10,
0,
arr
);
});

it('should not call callback for empty array', () => {
const cb = jest.fn();

const result = [].reduce2(cb, 100);

expect(cb).not.toHaveBeenCalled();
expect(result).toBe(100);
});
Comment on lines +90 to +97

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great set of tests for cases where an initial value is provided. However, a key part of reduce's functionality is how it behaves when no initial value is given.

Could you add test cases for that scenario? Specifically, you should test:

  1. A standard array like [1, 2, 3].
  2. An array with a single element (the callback shouldn't be called).
  3. An empty array (this should throw a TypeError).

});