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
61 changes: 59 additions & 2 deletions src/reduce.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,66 @@ describe('reduce', () => {
delete Array.prototype.reduce2;
});

it('should ', () => {
it('should sum all numbers in array with initial value', () => {
const result = [1, 2, 3, 4].reduce2((acc, curr) => acc + curr, 0);

expect(result).toBe(10);
});

// Add tests here
it('should sum all numbers in array without initial value', () => {
const result = [1, 2, 3, 4].reduce2((acc, curr) => acc + curr);

expect(result).toBe(10);
});

it('should handle reducing to a single string', () => {
const result = ['a', 'b', 'c'].reduce2((acc, curr) => acc + curr, '');

expect(result).toBe('abc');
});

it('should handle reducing an array of objects', () => {
const arr = [{ value: 1 }, { value: 2 }, { value: 3 }];
const result = arr.reduce2((acc, curr) => acc + curr.value, 0);

expect(result).toBe(6);
});

it('should return the first element if no initial value',
() => {
const result = [42].reduce2((acc, curr) => acc + curr);

expect(result).toBe(42);
});

it('should return undefined if array is empty and no value is provided',
() => {
const result = [].reduce2((acc, curr) => acc + curr);

expect(result).toBe(undefined);
Comment on lines +48 to +50

Choose a reason for hiding this comment

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

Issue: According to the standard Array.prototype.reduce behavior, calling reduce on an empty array without an initial value should throw a TypeError, not return undefined. If your implementation is meant to match the standard, this test should expect an error to be thrown instead of returning undefined. Please review the requirements and adjust the test accordingly.

});
Comment on lines +46 to +51

Choose a reason for hiding this comment

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

Issue: The test expects undefined when reducing an empty array without an initial value, but according to the standard, this should throw a TypeError. Please update this test to expect a TypeError instead of undefined to comply with the standard behavior for Array.prototype.reduce.


it('should pass index and array to callback', () => {
const mockFn = jest.fn((acc, curr, index, arr) => acc + curr);

[1, 2, 3].reduce2(mockFn, 0);

expect(mockFn).toHaveBeenCalledWith(0, 1, 0, [1, 2, 3]);
expect(mockFn).toHaveBeenCalledWith(1, 2, 1, [1, 2, 3]);
expect(mockFn).toHaveBeenCalledWith(3, 3, 2, [1, 2, 3]);
});

it('should work correctly with holes in the array', () => {
const arr = [1, 3];
const result = arr.reduce2((acc, curr) => acc + (curr || 0), 0);

expect(result).toBe(4);
});

it('should allow reducing with boolean values', () => {
const result = [true, false, true].reduce2(
(acc, curr) => acc && curr, true);

expect(result).toBe(false);
});
});