From 0f305517a668aa54261d7094873e6f655976a26d Mon Sep 17 00:00:00 2001 From: Vladislav Yesypenko Date: Sat, 24 May 2025 19:35:09 +0300 Subject: [PATCH] Test fix --- package-lock.json | 3 ++- package.json | 2 +- src/splitInteger.test.js | 54 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 79127efc..f6abf20a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,7 @@ "eslint": "^5.16.0", "eslint-plugin-jest": "^22.4.1", "eslint-plugin-node": "^8.0.1", - "jest": "^24.5.0" + "jest": "^24.9.0" } }, "node_modules/@babel/code-frame": { @@ -3567,6 +3567,7 @@ "resolved": "https://registry.npmjs.org/jest/-/jest-24.9.0.tgz", "integrity": "sha512-YvkBL1Zm7d2B1+h5fHEOdyjCG+sGMz4f8D86/0HiqJ6MB4MnDc8FgP5vdWsGnemOQro7lnYo8UakZ3+5A0jxGw==", "dev": true, + "license": "MIT", "dependencies": { "import-local": "^2.0.0", "jest-cli": "^24.9.0" diff --git a/package.json b/package.json index f782ac5a..077b4aee 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "eslint": "^5.16.0", "eslint-plugin-jest": "^22.4.1", "eslint-plugin-node": "^8.0.1", - "jest": "^24.5.0" + "jest": "^24.9.0" }, "mateAcademy": { "projectType": "javascript" diff --git a/src/splitInteger.test.js b/src/splitInteger.test.js index a610317d..6109f7c0 100644 --- a/src/splitInteger.test.js +++ b/src/splitInteger.test.js @@ -3,19 +3,65 @@ 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 numberOfParts`, () => { + expect(splitInteger(6, 3)).toEqual([2, 2, 2]); + expect(splitInteger(12, 4)).toEqual([3, 3, 3, 3]); }); -test(`should return a part equals to a value +test(`should return a part equal to value when splitting into 1 part`, () => { - + expect(splitInteger(8, 1)).toEqual([8]); + expect(splitInteger(123, 1)).toEqual([123]); }); test('should sort parts ascending if they are not equal', () => { + const result = splitInteger(17, 4); + expect(result).toEqual([4, 4, 4, 5]); + expect(result).toEqual([...result].sort((a, b) => a - b)); }); test('should add zeros if value < numberOfParts', () => { + expect(splitInteger(3, 5)).toEqual([0, 0, 1, 1, 1]); + expect(splitInteger(0, 3)).toEqual([0, 0, 0]); + expect(splitInteger(1, 4)).toEqual([0, 0, 0, 1]); +}); + +test('should ensure the sum of parts equals the original value', () => { + const testCases = [ + [8, 1], + [6, 2], + [17, 4], + [32, 6], + [0, 3], + [3, 5], + [1, 4], + [100, 9], + ]; + + for (const [value, parts] of testCases) { + const result = splitInteger(value, parts); + const sum = result.reduce((a, b) => a + b, 0); + + expect(sum).toBe(value); + } +}); + +test('should ensure the difference between max and min is at most 1', () => { + const testCases = [ + [17, 4], + [32, 6], + [3, 5], + [0, 3], + [1, 4], + [100, 9], + ]; + + for (const [value, parts] of testCases) { + const result = splitInteger(value, parts); + const min = Math.min(...result); + const max = Math.max(...result); + expect(max - min).toBeLessThanOrEqual(1); + } });