Skip to content
Open
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions src/splitInteger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@ const splitInteger = require('./splitInteger');

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

expect(result).toEqual([3, 3]);
});

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

expect(result).toEqual([8]);
});

test('should sort parts ascending if they are not equal', () => {
const result = splitInteger(17, 4);
const unique = new Set(result);

expect([...unique][1]).toBeGreaterThan([...unique][0]);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This assertion is a creative way to check the order, but it doesn't fully verify that the entire array is sorted. For example, if the function returned [4, 5, 4, 4], this test would still pass because the unique ordered values are 4 and 5, but the array itself isn't sorted.

A more reliable way to test this is to compare the result against the complete expected array. For this case, you could assert expect(result).toEqual([4, 4, 4, 5]), which validates the contents, length, and order all at once.

});

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

expect(result).toEqual([0, 0, 1, 1]);
});