Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion src/reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ function reduce(callback, startValue) {
}

for (let i = startIndex; i < this.length; i++) {
prev = callback(prev, this[i], i, this);
if (i in this) {
prev = callback(prev, this[i], i, this);
}
}

return prev;
Expand Down
61 changes: 59 additions & 2 deletions src/reduce.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 without initial value', () => {
const result = nums.reduce2(callback);

expect(callback).toHaveBeenCalled();
expect(callback).toHaveBeenCalledTimes(3);
Copy link

Choose a reason for hiding this comment

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

nums nie ma przypadkiem 4 itemów?

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

it('should work without initial array', () => {
Copy link

Choose a reason for hiding this comment

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

Lepsza bylaby nazwa "should work with empty array"

const init = 10;
const result = [].reduce2(callback, init);

expect(callback).not.toHaveBeenCalled();
expect(callback).toHaveBeenCalledTimes(0);
Comment on lines +46 to +47
Copy link

Choose a reason for hiding this comment

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

To sprawdza jedno i to samo

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);
});
Comment on lines +65 to +69
Copy link

Choose a reason for hiding this comment

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

To chyba nie sprawdza tego co zapisałeś. null przy operacjach matematycznych jest zmieniany na 0


it('should invoke callback for indexes without value', () => {
[1, '', '', 1, 2].reduce2(callback);

expect(callback).toHaveBeenCalledTimes(4);
Copy link

Choose a reason for hiding this comment

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

a nie 5 razy?

});

Choose a reason for hiding this comment

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

Missing test: empty array WITH initialValue should return the initialValue and must not call the callback. Add a test that does expect([].reduce2(mockFn, 'x')).toBe('x') and assert mockFn was not called. The current test suite lacks this required case from the task checklist (empty arrays with initialValue) and so does not fully validate native-like behavior .

});