-
Notifications
You must be signed in to change notification settings - Fork 113
Add comprehensive tests for custom Array reduce methodUpdate reduce.t… #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
c773286
f67b04f
c85ad62
37ec831
0634829
19bc7dc
099f721
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,9 +11,52 @@ describe('reduce', () => { | |
| delete Array.prototype.reduce2; | ||
| }); | ||
|
|
||
| it('should ', () => { | ||
| it('should sum numbers in an array', () => { | ||
| const arr = [1, 2, 3, 4]; | ||
| const result = arr.reduce2((acc, val) => acc + val, 0); | ||
| expect(result).toBe(10); | ||
|
Comment on lines
+14
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Include tests that exercise non-number types to ensure accumulator propagation works across types. For example, a string concatenation test ( |
||
| }); | ||
|
|
||
| it('should multiply numbers in an array', () => { | ||
| const arr = [1, 2, 3, 4]; | ||
| const result = arr.reduce2((acc, val) => acc * val, 1); | ||
| expect(result).toBe(24); | ||
|
Comment on lines
+20
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add tests that verify mutation-during-reduction semantics. Specifically, check that elements appended after reduction starts are not visited (length captured at start) and elements removed before being visited are skipped. Create a test where the reducer mutates the array (push/splice/pop) and then assert which indices were visited and how many times the callback was called. |
||
| }); | ||
|
|
||
| it('should work without initial value', () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = arr.reduce2((acc, val) => acc + val); | ||
| expect(result).toBe(6); | ||
|
Comment on lines
+26
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add tests for single-element arrays with and without an initial value. For example: (1) |
||
| }); | ||
|
|
||
| // Add tests here | ||
| it('should handle empty array with initial value', () => { | ||
| const arr = []; | ||
| const mockCb = jest.fn(); | ||
| const result = arr.reduce2(mockCb, 5); | ||
|
|
||
| expect(result).toBe(5); | ||
| expect(mockCb).toHaveBeenCalledTimes(0); | ||
| }); | ||
|
|
||
| it('should throw error when empty array and no initial value', () => { | ||
| const arr = []; | ||
| expect(() => | ||
| arr.reduce2((acc, val) => acc + val) | ||
| ).toThrow(TypeError); | ||
| }); | ||
|
|
||
| it('should pass correct arguments to callback', () => { | ||
| const arr = [10, 20]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a test for sparse arrays (holes). For example: |
||
| const mockCb = jest.fn((acc, val, idx, array) => acc + val); | ||
| arr.reduce2(mockCb, 0); | ||
|
|
||
| const argsCall1 = [0, 10, 0, arr]; | ||
| const argsCall2 = [10, 20, 1, arr]; | ||
|
|
||
| expect(mockCb).toHaveBeenCalledTimes(2); | ||
| expect(mockCb).toHaveBeenNthCalledWith(1, ...argsCall1); | ||
| expect(mockCb).toHaveBeenNthCalledWith(2, ...argsCall2); | ||
| }); | ||
| }); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add an explicit test ensuring
reduceitself does not modify the original array (unless the reducer deliberately mutates it). For example, copy the array before/after callingreduce2and assert the array content and length are unchanged when the reducer doesn't mutate the array.