-
Notifications
You must be signed in to change notification settings - Fork 113
qa array reduce method solution #98
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
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,62 @@ describe('reduce', () => { | |
| delete Array.prototype.reduce2; | ||
| }); | ||
|
|
||
| it('should ', () => { | ||
| let callback; | ||
|
|
||
| beforeEach(() => { | ||
| callback = jest.fn().mockImplementation((a, b) => a + b); | ||
| }); | ||
|
|
||
| // Add tests here | ||
| it('should be declared', () => { | ||
| expect(reduce).toBeInstanceOf(Function); | ||
| }); | ||
|
|
||
| it('should not mutate array', () => { | ||
| const array = [1, 2, 3, 4]; | ||
|
|
||
| const copy = [...array]; | ||
|
|
||
| array.reduce2(callback, 0); | ||
|
|
||
| expect(array).toEqual(copy); | ||
| }); | ||
|
|
||
| it('should run callback array`s length times if initialValue is', () => { | ||
| const array = [1, 2, 3, 4]; | ||
|
|
||
| const result = array.reduce2(callback, 0); | ||
|
|
||
| expect(callback).toHaveBeenCalledTimes(array.length); ; | ||
| expect(result).toBe(10); | ||
| }); | ||
|
|
||
| it('should run callback array`s length - 1 times if no initialValue', () => { | ||
| const array = [1, 2, 3, 4]; | ||
|
|
||
| const result = array.reduce2(callback); | ||
|
|
||
| expect(callback).toHaveBeenCalledTimes(array.length - 1); ; | ||
| expect(result).toBe(10); | ||
| }); | ||
|
|
||
| it('should run callback with correct arguments', () => { | ||
| const array = [1, 2, 3, 4]; | ||
|
|
||
| const result = array.reduce2(callback, 0); | ||
|
|
||
| expect(callback).toHaveBeenNthCalledWith(1, 0, 1, 0, array); | ||
| expect(callback).toHaveBeenNthCalledWith(2, 1, 2, 1, array); | ||
| expect(callback).toHaveBeenNthCalledWith(3, 3, 3, 2, array); | ||
| expect(callback).toHaveBeenNthCalledWith(4, 6, 4, 3, array); | ||
|
|
||
| expect(result).toBe(10); | ||
| }); | ||
|
|
||
| it('should return initial value if array is empty', () => { | ||
| const array = []; | ||
| const initialValue = 0; | ||
| const result = array.reduce2(callback, initialValue); | ||
|
|
||
| expect(result).toBe(initialValue); ; | ||
| }); | ||
|
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. According to the |
||
| }); | ||
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.
While it's good to check that the callback is called with the correct arguments, you're missing a check for the most important part of
reduce: the final accumulated value that it returns. Consider storing the result ofarray.reduce2()and adding an assertion for it.