diff --git a/README.md b/README.md index a46e26d3..9bc0a60f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# QA: Split integer ([uk](readme.uk.md)) +# QA: Split integer ([uk](https://github.com/AndrzejFront/js_split_integer/blob/main/readme.uk.md)) Write tests for `splitInteger` function that takes 2 positive integers `value` and `numberOfParts` and returns an array containing exactly `numberOfParts` diff --git a/src/splitInteger.test.js b/src/splitInteger.test.js index 24003dd5..dde654bc 100644 --- a/src/splitInteger.test.js +++ b/src/splitInteger.test.js @@ -3,11 +3,54 @@ const splitInteger = require('./splitInteger'); test(`should split a number into equal parts - if a value is divisible by a numberOfParts`, () => {}); + if a value is divisible by a numberOfParts`, () => { + expect(splitInteger(6, 2)).toEqual([3, 3]); +}); test(`should return a part equals to a value - when splitting into 1 part`, () => {}); + when splitting into 1 part`, () => { + expect(splitInteger(8, 1)).toEqual([8]); +}); -test('should sort parts ascending if they are not equal', () => {}); +test('should sort parts ascending if they are not equal', () => { + expect(splitInteger(17, 4)).toEqual([4, 4, 4, 5]); + expect(splitInteger(32, 6)).toEqual([5, 5, 5, 5, 6, 6]); + expect(splitInteger(29, 3)).toEqual([9, 10, 10]); +}); -test('should add zeros if value < numberOfParts', () => {}); +test('should add zeros if value < numberOfParts', () => { + expect(splitInteger(3, 5)).toEqual([0, 0, 1, 1, 1]); +}); + +test('should split the smallest positive value', () => { + expect(splitInteger(1, 1)).toEqual([1]); + expect(splitInteger(1, 4)).toEqual([0, 0, 0, 1]); +}); + +test('should return ones if value equals numberOfParts', () => { + expect(splitInteger(4, 4)).toEqual([1, 1, 1, 1]); +}); + +test('should return exactly numberOfParts elements', () => { + expect(splitInteger(23, 7)).toHaveLength(7); +}); + +test('should preserve the total value', () => { + const parts = splitInteger(23, 7); + + expect(parts.reduce((sum, part) => sum + part, 0)).toBe(23); +}); + +test('should return only integer parts', () => { + const parts = splitInteger(23, 7); + + parts.forEach((part) => { + expect(Number.isInteger(part)).toBe(true); + }); +}); + +test('should keep the difference between max and min parts at most 1', () => { + const parts = splitInteger(23, 7); + + expect(Math.max(...parts) - Math.min(...parts)).toBeLessThanOrEqual(1); +});