Skip to content

Add comprehensive tests for custom Array reduce methodUpdate reduce.t…#91

Open
JoAnna578 wants to merge 7 commits intomate-academy:masterfrom
JoAnna578:develop
Open

Add comprehensive tests for custom Array reduce methodUpdate reduce.t…#91
JoAnna578 wants to merge 7 commits intomate-academy:masterfrom
JoAnna578:develop

Conversation

@JoAnna578
Copy link

…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.

…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.
Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 this behavior (unbound vs bound) to mirror native behavior.
  • Add a test ensuring reduce2 does 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

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

Comment on lines +32 to +35
it('should handle empty array with initial value', () => {
const arr = [];
const result = arr.reduce2((acc, val) => acc + val, 5);
expect(result).toBe(5);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +38 to +40
it('should throw error when empty array and no initial value', () => {
const arr = [];
expect(() => arr.reduce2((acc, val) => acc + val)).toThrow();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +26 to +29
it('should work without initial value', () => {
const arr = [1, 2, 3];
const result = arr.reduce2((acc, val) => acc + val);
expect(result).toBe(6);

Choose a reason for hiding this comment

The 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) [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.

Comment on lines +14 to +17
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);

Choose a reason for hiding this comment

The 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 (['a','b','c'].reduce2((a,b)=>a+b, '')) and an object-merge test (accumulating properties into an object) are required by the description.

Comment on lines +20 to +23
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);

Choose a reason for hiding this comment

The 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 pass correct arguments to callback', () => {
const arr = [10, 20];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +43 to +50
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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

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 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.

// Add tests here
it('should throw error when empty array and no initial value', () => {
const arr = [];
expect(() => arr.reduce2((acc, val) => acc + val)).toThrow();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link

@EwaKobiela EwaKobiela left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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ą.
@JoAnna578 JoAnna578 requested a review from EwaKobiela October 14, 2025 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants