-
Notifications
You must be signed in to change notification settings - Fork 113
array_method_reduce #90
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 |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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); | ||
| expect(result).toBe(10); | ||
| }); | ||
|
|
||
| it('should work without initial array', () => { | ||
|
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. 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
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. 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
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. To chyba nie sprawdza tego co zapisałeś. |
||
|
|
||
| it('should invoke callback for indexes without value', () => { | ||
| [1, '', '', 1, 2].reduce2(callback); | ||
|
|
||
| expect(callback).toHaveBeenCalledTimes(4); | ||
|
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. a nie 5 razy? |
||
| }); | ||
|
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. Missing test: empty array WITH initialValue should return the initialValue and must not call the callback. Add a test that does |
||
| }); | ||
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.
nums nie ma przypadkiem 4 itemów?