Add comprehensive tests for custom Array reduce methodUpdate reduce.t…#91
Add comprehensive tests for custom Array reduce methodUpdate reduce.t…#91JoAnna578 wants to merge 7 commits intomate-academy:masterfrom
Conversation
…est.js Dodano zestaw testów dla metody reduce2, obejmujący: sumowanie, mnożenie, brak wartości początkowej, pustą tablicę, oraz sprawdzenie poprawności argumentów przekazywanych do callbacka. Testy zapewniają pełną zgodność z zachowaniem natywnej metody Array.prototype.reduce.
…czątkowej Doprecyzowano działanie funkcji reduce, tak aby rzucała wyjątek, gdy tablica jest pusta i nie podano wartości początkowej. Dzięki temu wszystkie testy, w tym sprawdzające błędne przypadki, przechodzą poprawnie. Pozostałe przypadki redukcji pozostają bez zmian.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
I am requesting changes because several required edge-case tests are missing, which blocks meeting the task’s completeness criteria. You did a solid job covering core scenarios (sum/product, with/without initial, empty array behaviors, callback arguments), but the suite must also match native reduce semantics across edge cases.
Critical issues to fix:
- Add a spy-based assertion that the reducer is not called for an empty array when an initial value is provided (e.g., around current test near line 30).
- For empty array without an initial value (around line 40), assert
toThrow(TypeError)and, if applicable, the native message. - Add tests for single-element arrays: without initial returns the element and does not call the callback; with initial processes once and returns the correct sum.
- Add a test that non-function callbacks throw
TypeError. - Add tests for sparse arrays (holes) to ensure skipped indices and correct call indices.
- Add tests for mutation-during-reduction (push/splice/pop) to confirm length captured at start and correct visitation.
- Add tests for non-number types (string concatenation, object accumulation) to verify accumulator propagation.
- Add tests for callback
thisbehavior (unbound vs bound) to mirror native behavior. - Add a test ensuring
reduce2does not modify the original array when the reducer doesn’t mutate it.
Minor improvements: also consider a test where the initial value is explicitly undefined to ensure it’s treated as provided and invokes the callback. Once these are added, your suite will comprehensively validate parity with native reduce. Keep up the good work—your current structure makes adding these cases straightforward.
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Important
Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.
Tip
If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.
Caution
Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.
Footnotes
| it('should handle empty array with initial value', () => { | ||
| const arr = []; | ||
| const result = arr.reduce2((acc, val) => acc + val, 5); | ||
| expect(result).toBe(5); |
There was a problem hiding this comment.
This test verifies the return value for an empty array with an initial value, which is required. However, it should also assert that the reducer callback is not called. Use a spy (e.g., const cb = jest.fn(); arr.reduce2(cb, 5); expect(cb).toHaveBeenCalledTimes(0);) to ensure the callback is not invoked, matching native reduce behavior.
src/reduce.test.js
Outdated
| it('should throw error when empty array and no initial value', () => { | ||
| const arr = []; | ||
| expect(() => arr.reduce2((acc, val) => acc + val)).toThrow(); |
There was a problem hiding this comment.
The empty-array-without-initial-value test uses toThrow() generically. The native Array.prototype.reduce throws a TypeError in this case. Make this assertion specific by using toThrow(TypeError) so the custom implementation must match the native error type.
| it('should work without initial value', () => { | ||
| const arr = [1, 2, 3]; | ||
| const result = arr.reduce2((acc, val) => acc + val); | ||
| expect(result).toBe(6); |
There was a problem hiding this comment.
Add tests for single-element arrays with and without an initial value. For example: (1) [5].reduce2((acc, v) => acc + v) should return 5; (2) [5].reduce2((acc, v) => acc + v, 10) should return 15. These confirm correct behavior when using the first element as the initial accumulator and when an explicit initial is provided.
| 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); |
There was a problem hiding this comment.
Include tests that exercise non-number types to ensure accumulator propagation works across types. For example, a string concatenation test (['a','b','c'].reduce2((a,b)=>a+b, '')) and an object-merge test (accumulating properties into an object) are required by the description.
| 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); |
There was a problem hiding this comment.
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 pass correct arguments to callback', () => { | ||
| const arr = [10, 20]; |
There was a problem hiding this comment.
Add a test for sparse arrays (holes). For example: const arr = [1, , 3]; const cb = jest.fn((acc, v, idx) => acc + v); arr.reduce2(cb, 0); then assert cb was called for indices 0 and 2 only and that the hole at index 1 was skipped. This matches native reduce semantics and is required by the description.
src/reduce.test.js
Outdated
| it('should pass correct arguments to callback', () => { | ||
| const arr = [10, 20]; | ||
| const mockCb = jest.fn((acc, val, idx, array) => acc + val); | ||
| arr.reduce2(mockCb, 0); | ||
|
|
||
| expect(mockCb).toHaveBeenCalledTimes(2); | ||
| expect(mockCb).toHaveBeenNthCalledWith(1, 0, 10, 0, arr); | ||
| expect(mockCb).toHaveBeenNthCalledWith(2, 10, 20, 1, arr); |
There was a problem hiding this comment.
Add tests for callback this behavior. The description requires verifying that reduce's callback doesn't implicitly change this. For example, use an unbound function and a bound function to assert this inside the callback is as expected (or undefined/global depending on strict mode) and mirrors native behavior.
| it('should ', () => { | ||
| it('should sum numbers in an array', () => { | ||
| const arr = [1, 2, 3, 4]; | ||
| const result = arr.reduce2((acc, val) => acc + val, 0); |
There was a problem hiding this comment.
Add an explicit test ensuring reduce itself does not modify the original array (unless the reducer deliberately mutates it). For example, copy the array before/after calling reduce2 and assert the array content and length are unchanged when the reducer doesn't mutate the array.
src/reduce.test.js
Outdated
| // Add tests here | ||
| it('should throw error when empty array and no initial value', () => { | ||
| const arr = []; | ||
| expect(() => arr.reduce2((acc, val) => acc + val)).toThrow(); |
There was a problem hiding this comment.
Where you currently assert thrown errors (line 40), prefer asserting the exact error type and message if applicable. This ensures parity with native behavior and prevents implementations that throw generic errors from passing.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Sorry, the pull request review was unsuccessful for the following reasons:
Warning
No changes were made in the pull request since the last review. Please make sure that you have added files to the pull request and committed the changes.
[CODE: NCM]
If the issue is within your control, please address it.
The next review attempt will automatically launch in about 1 hour.
If you believe this is an error or need assistance, please contact the support team.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Sorry, the pull request review was unsuccessful for the following reasons:
Warning
No changes were made in the pull request since the last review. Please make sure that you have added files to the pull request and committed the changes.
[CODE: NCM]
If the issue is within your control, please address it.
The next review attempt will automatically launch in about 1 hour.
If you believe this is an error or need assistance, please contact the support team.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Sorry, the pull request review was unsuccessful for the following reasons:
Warning
No changes were made in the pull request since the last review. Please make sure that you have added files to the pull request and committed the changes.
[CODE: NCM]
If the issue is within your control, please address it.
The next review attempt will automatically launch in about 1 hour.
If you believe this is an error or need assistance, please contact the support team.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Sorry, the pull request review was unsuccessful for the following reasons:
Warning
No changes were made in the pull request since the last review. Please make sure that you have added files to the pull request and committed the changes.
[CODE: NCM]
If the issue is within your control, please address it.
The next review attempt will automatically launch in about 1 hour.
If you believe this is an error or need assistance, please contact the support team.
EwaKobiela
left a comment
There was a problem hiding this comment.
Please introduce changes suggested by AI mentor
Rozszerzono zestaw testów reduce2 o przypadki krawędziowe: pusty array, single-element array, sparse arrays, mutacje w trakcie redukcji, różne typy danych, poprawne wywołanie callback oraz obsługę błędów. Teraz reduce2 zachowuje się zgodnie z natywnym Array.prototype.reduce.
Poprawiłam długie linie w testach toHaveBeenNthCalledWith, zastąpiłam dziury w tablicach jawnym undefined, dodałam sprawdzenie, że callback nie jest wywoływany dla pustej tablicy z wartością początkową.
…est.js
Dodano zestaw testów dla metody reduce2, obejmujący: sumowanie, mnożenie, brak wartości początkowej, pustą tablicę, oraz sprawdzenie poprawności argumentów przekazywanych do callbacka. Testy zapewniają pełną zgodność z zachowaniem natywnej metody Array.prototype.reduce.