-
Notifications
You must be signed in to change notification settings - Fork 113
Solution #85
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
Open
ViktoriiaPylypko
wants to merge
4
commits into
mate-academy:master
Choose a base branch
from
ViktoriiaPylypko:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Solution #85
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,18 +2,128 @@ | |
|
|
||
| const { reduce } = require('./reduce'); | ||
|
|
||
| describe('reduce', () => { | ||
| beforeAll(() => { | ||
| beforeAll(() => { | ||
| Array.prototype.reduce2 = reduce; // eslint-disable-line | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| delete Array.prototype.reduce2; | ||
| }); | ||
|
|
||
| describe('reduce2', () => { | ||
| it('should sum array values with initial value', () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = arr.reduce2((acc, val) => acc + val, 10); | ||
|
|
||
| expect(result).toBe(16); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| delete Array.prototype.reduce2; | ||
| it('should use initial value if provided', () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = arr.reduce2((acc, val) => acc + val, 10); | ||
|
|
||
| expect(result).toBe(16); // 10 + 1 + 2 + 3 | ||
| }); | ||
|
|
||
| it('should ', () => { | ||
| it('should sum array values without initial value', () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = arr.reduce2((acc, val) => acc + val); | ||
|
|
||
| expect(result).toBe(6); // 1 + 2 + 3 | ||
| }); | ||
|
|
||
| // Add tests here | ||
| it('should multiply array values', () => { | ||
| const arr = [2, 3, 4]; | ||
| const result = arr.reduce2((acc, val) => acc * val, 1); | ||
|
|
||
| expect(result).toBe(24); // 1 * 2 * 3 * 4 | ||
| }); | ||
|
|
||
| it('should concatenate strings', () => { | ||
| const arr = ['a', 'b', 'c']; | ||
| const result = arr.reduce2((acc, val) => acc + val, ''); | ||
|
|
||
| expect(result).toBe('abc'); | ||
| }); | ||
|
|
||
| it('should handle single-element array with no initial value', () => { | ||
| const arr = [5]; | ||
| const result = arr.reduce2((acc, val) => acc + val); | ||
|
|
||
| expect(result).toBe(5); // returns the only element | ||
| }); | ||
|
|
||
| it('should handle empty array with initial value', () => { | ||
| const arr = []; | ||
| const result = arr.reduce2((acc, val) => acc + val, 100); | ||
|
|
||
| expect(result).toBe(100); | ||
| }); | ||
|
|
||
| it('should return undefined on empty array with no initial value', () => { | ||
| const arr = []; | ||
|
|
||
| expect(arr.reduce2((acc, val) => acc + val)).toBe(undefined); | ||
| }); | ||
|
|
||
| describe('edge cases', () => { | ||
| it.skip('should skip holes in sparse arrays', () => { | ||
| const arr = [1, 3]; | ||
|
|
||
| arr.splice(1, 0); // тепер arr = [1, <empty>, 3] | ||
|
|
||
| const visited = []; | ||
|
|
||
| const result = arr.reduce2((acc, val, i) => { | ||
| visited.push([i, val]); | ||
|
|
||
| return acc + (val || 0); | ||
| }, 0); | ||
|
|
||
| expect(result).toBe(4); | ||
|
|
||
| expect(visited).toEqual([ | ||
| [0, 1], | ||
| [2, 3], | ||
| ]); | ||
| }); | ||
|
|
||
| it('should work on array-like objects', () => { | ||
| const arrayLike = { | ||
| 0: 'a', | ||
| 1: 'b', | ||
| 2: 'c', | ||
| length: 3, | ||
| reduce2: reduce, | ||
| }; | ||
|
|
||
| const result = arrayLike.reduce2((acc, val) => acc + val, ''); | ||
|
|
||
| expect(result).toBe('abc'); | ||
| }); | ||
|
|
||
| it('should return undefined if callback is not provided', () => { | ||
| const arr = [1, 2, 3]; | ||
|
|
||
| expect(arr.reduce2()).toBe(undefined); | ||
| }); | ||
|
|
||
| it('should return undefined if callback is not a function', () => { | ||
| const arr = [1, 2]; | ||
|
|
||
| expect(arr.reduce2(null)).toBe(undefined); | ||
| expect(arr.reduce2(123)).toBe(undefined); | ||
| expect(arr.reduce2('not a function')).toBe(undefined); | ||
| }); | ||
|
|
||
| it('should handle callback throwing error (user-defined)', () => { | ||
| const arr = [1, 2, 3]; | ||
| const faultyCallback = () => { | ||
| throw new Error('Test error'); | ||
| }; | ||
|
|
||
| expect(() => arr.reduce2(faultyCallback)).toThrow('Test error'); | ||
| }); | ||
| }); | ||
| }); | ||
| // Add tests here | ||
|
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. You are missing required edge case tests as per the task checklist: callback argument checks, sparse arrays, array-like objects, and error handling for invalid callbacks. Please add tests for these scenarios. |
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The test for skipping holes in sparse arrays is currently skipped using
it.skip. If the task checklist requires this edge case to be actively tested, please enable this test by removing.skip.