Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/splitInteger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,51 @@ const splitInteger = require('./splitInteger');

test(`should split a number into equal parts
if a value is divisible by a numberOfParts`, () => {
const result = splitInteger(8, 2);

const min = Math.min(...result);
const max = Math.max(...result);

expect(max - min).toBeLessThanOrEqual(1);
Comment on lines +9 to +12

Choose a reason for hiding this comment

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

These calculations and the assertion on line 12 are redundant. The toStrictEqual([4, 4]) on line 13 is a comprehensive check that already guarantees the difference between the max and min values is 0, which satisfies the requirement.

expect(result).toStrictEqual([4, 4]);
});

test(`should return a part equals to a value
when splitting into 1 part`, () => {
const result = splitInteger(6, 1);

expect(result[0]).toBe(6);

Choose a reason for hiding this comment

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

This assertion is redundant because toStrictEqual([6]) on the next line already verifies that the first (and only) element is 6.

expect(result).toStrictEqual([6]);
});

test('should sort parts ascending if they are not equal', () => {
const result = splitInteger(5, 3);
const sorted = [...result].sort((a, b) => a - b);

const min = Math.min(...result);
const max = Math.max(...result);

expect(max - min).toBeLessThanOrEqual(1);
expect(result).toStrictEqual(sorted);
Comment on lines +26 to +32

Choose a reason for hiding this comment

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

These checks are helpful for understanding the requirements, but they are redundant. The toStrictEqual([1, 2, 2]) assertion on line 33 already confirms that the array is sorted and that the difference between the max and min values is 1.

expect(result).toStrictEqual([1, 2, 2]);
});

test('should add zeros if value < numberOfParts', () => {
const result = splitInteger(6, 8);

function countZeros(arr) {
let count = 0;

for (const a of arr) {
if (a === 0) {
count++;
}
}

return count;
}

expect(result).toStrictEqual([0, 0, 1, 1, 1, 1, 1, 1]);
expect(countZeros(result)).toBe(2);
expect(result.reduce((a, b) => a + b, 0)).toBe(6);
Comment on lines +39 to +53

Choose a reason for hiding this comment

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

The toStrictEqual assertion on line 51 is a great addition! It's a comprehensive check that makes the assertions on lines 52 and 53 unnecessary. A single, powerful assertion is all you need here, which fully addresses the feedback from the previous review. The countZeros function also becomes unnecessary and can be removed.

});