Skip to content

qa array reduce method solution#98

Open
AnnNikiforova wants to merge 3 commits intomate-academy:masterfrom
AnnNikiforova:develop
Open

qa array reduce method solution#98
AnnNikiforova wants to merge 3 commits intomate-academy:masterfrom
AnnNikiforova:develop

Conversation

@AnnNikiforova
Copy link

No description provided.

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

Great start on the test suite for your custom reduce method! You've correctly tested some important cases. However, this requires a few changes to make the tests more comprehensive and accurate.

Here are the key points to address:

  1. Incorrect Error Handling Test: Your test for reducing an empty array without an initial value incorrectly expects undefined. According to the specification, this scenario should throw a TypeError. Please update this test to use Jest's .toThrow() matcher to assert that an error is thrown.
  2. Missing Return Value Assertions: Several tests are missing a crucial assertion for the final accumulated value returned by reduce2(). The main purpose of reduce is to produce this value, so it's essential to verify it's correct in your tests.
  3. Ineffective Immutability Test: The assertion expect(array).toEqual(array) will always pass. To properly test that the original array is not mutated, you should create a copy of it before calling reduce2 and then compare the original array to that copy afterward.

Addressing these points will ensure your test suite correctly validates the behavior of the reduce method.


✨ 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


array.reduce2(callback, 0);

expect(array).toEqual(array);

Choose a reason for hiding this comment

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

This assertion expect(array).toEqual(array) will always be true and doesn't effectively test for immutability. To properly test that the array wasn't mutated, you should compare it against a copy that was made before calling reduce2.

Comment on lines +56 to +65
it('should run callback with correct arguments', () => {
const array = [1, 2, 3, 4];

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

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 of array.reduce2() and adding an assertion for it.

Comment on lines +75 to +79
it('should return undefined if array is empty and no initial value', () => {
const array = [];
const result = array.reduce2(callback);

expect(result).toBe(undefined); ;

Choose a reason for hiding this comment

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

According to the MDN documentation, calling reduce on an empty array without an initial value should throw a TypeError. This test incorrectly expects undefined. You should use Jest's toThrow() or toThrowError() matchers to assert that an error is thrown.

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

Great job addressing the previous feedback, especially on the immutability test! However, a couple of changes are needed to meet the core requirements. First, your test for reducing an empty array without an initial value needs to assert that a TypeError is thrown, as required by the specification. You can use Jest's .toThrow() for this. Second, a few tests are still missing assertions to verify that reduce2 returns the correct final accumulated value. Please ensure every test that produces a result checks that the result is correct. You're very close to finishing this!


✨ 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

it('should run callback array`s length times if initialValue is', () => {
const array = [1, 2, 3, 4];

array.reduce2(callback, 0);

Choose a reason for hiding this comment

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

This test correctly checks the number of times the callback is called, but it's also important to verify that reduce2 returns the correct final accumulated value. Please add an assertion for the return value.

it('should run callback array`s length - 1 times if no initialValue', () => {
const array = [1, 2, 3, 4];

array.reduce2(callback);

Choose a reason for hiding this comment

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

Similar to the test above, this one is missing an assertion for the final return value of reduce2. Verifying the accumulated result is a key part of testing reduce.

Comment on lines +50 to +56
it('should not run callback if array is empty', () => {
const array = [];

array.reduce2(callback);

expect(callback).toHaveBeenCalledTimes(0); ;
});

Choose a reason for hiding this comment

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

According to the Array.prototype.reduce specification, calling reduce on an empty array without providing an initial value must throw a TypeError. This test should be updated to assert that an error is thrown, for example by using Jest's .toThrow() matcher.

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

Great job adding assertions to verify the return values in your tests! That's a huge improvement and makes your test suite much more robust.

I am approving your solution as an educational override so you can keep making progress. However, there is one critical test case still missing from src/reduce.test.js. According to the specification, calling reduce on an empty array without providing an initial value must throw a TypeError. Please add a test to verify this behavior. You can use Jest's expect(() => ...).toThrow(TypeError) to check for the thrown error.

Keep up the great work!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

const result = array.reduce2(callback, initialValue);

expect(result).toBe(initialValue); ;
});

Choose a reason for hiding this comment

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

According to the Array.prototype.reduce specification, calling reduce on an empty array without providing an initial value should result in a TypeError. Please add a test case to verify this behavior. You can use Jest's expect(() => ...).toThrow(TypeError) to assert that an error is thrown.

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.

2 participants