From d51d2e9e970f7ca0d9a861c451f6db8838a7d6be Mon Sep 17 00:00:00 2001 From: DPende Date: Wed, 24 Apr 2024 17:38:30 +0200 Subject: [PATCH] refactor: update tests after code refactoring --- test/core.test.js | 8 +-- test/editpix.test.js | 55 ++++++++-------- test/utils.test.js | 146 +++++++++++++++---------------------------- 3 files changed, 81 insertions(+), 128 deletions(-) diff --git a/test/core.test.js b/test/core.test.js index f1c06de..ad914f1 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -251,15 +251,15 @@ describe('changeSaturation', () => { const testColor1 = [173, 114, 234]; const testColor2 = [173, 114, 234]; changeSaturation(testColor1, 50); - expect(utils.rgbToHsl(testColor2[0], testColor2[1], testColor2[2])[1]) - .toBeLessThan(utils.rgbToHsl(testColor1[0], testColor1[1], testColor1[2])[1]); + expect(utils.rgbToHsl(testColor2[0], testColor2[1], testColor2[2]).s) + .toBeLessThan(utils.rgbToHsl(testColor1[0], testColor1[1], testColor1[2]).s); }); test('should decrease saturation for negative factors', () => { const testColor1 = [173, 114, 234]; const testColor2 = [173, 114, 234]; changeSaturation(testColor1, -50); - expect(utils.rgbToHsl(testColor2[0], testColor2[1], testColor2[2])[1]) - .toBeGreaterThan(utils.rgbToHsl(testColor1[0], testColor1[1], testColor1[2])[1]); + expect(utils.rgbToHsl(testColor2[0], testColor2[1], testColor2[2]).s) + .toBeGreaterThan(utils.rgbToHsl(testColor1[0], testColor1[1], testColor1[2]).s); }); }); diff --git a/test/editpix.test.js b/test/editpix.test.js index 59ecd84..5defaf9 100644 --- a/test/editpix.test.js +++ b/test/editpix.test.js @@ -1,44 +1,39 @@ import EditPix from "../src/editpix.js" -describe('EditPix convertToRgb method', () => { - test('converts hex colors to RGB correctly for array input', () => { +describe('rgbToHex', () => { + test('Converts RGB to hexadecimal color for valid inputs', () => { const editPix = new EditPix(); - const hexColors = ['#ff0000', '#00ff00', '#0000ff']; // Red, Green, Blue - const expectedRgbColors = [ - [255, 0, 0], - [0, 255, 0], - [0, 0, 255] - ]; - expect(editPix.convertToRgb(hexColors)).toEqual(expectedRgbColors); + expect(editPix.rgbToHex(255, 0, 0)).toBe("#ff0000"); + expect(editPix.rgbToHex(0, 255, 0)).toBe("#00ff00"); + expect(editPix.rgbToHex(0, 0, 255)).toBe("#0000ff"); + // Add more test cases for valid inputs as needed }); - test('converts hex colors to RGB correctly for single color input', () => { + test('Throws error for invalid RGB inputs', () => { const editPix = new EditPix(); - const hexColor = '#ff0000'; // Red - const expectedRgbColor = [[255, 0, 0]]; - expect(editPix.convertToRgb(hexColor)).toEqual(expectedRgbColor); - }); - - test('throws an error for invalid hex color input', () => { - const editPix = new EditPix(); - const invalidHexColor = 'invalid_hex_color'; - expect(() => editPix.convertToRgb(invalidHexColor)).toThrow(Error); + // Test invalid RGB inputs + expect(() => editPix.rgbToHex(-1, 0, 0)).toThrow(Error); + expect(() => editPix.rgbToHex(256, 0, 0)).toThrow(Error); + expect(() => editPix.rgbToHex(0, -1, 0)).toThrow(Error); + expect(() => editPix.rgbToHex(0, 256, 0)).toThrow(Error); + expect(() => editPix.rgbToHex(0, 0, -1)).toThrow(Error); + expect(() => editPix.rgbToHex(0, 0, 256)).toThrow(Error); + // Add more test cases for invalid inputs as needed }); }); -describe('EditPix convertToHex method', () => { - test('converts RGB colors to hex correctly for array input', () => { +describe('EditPix.hexToRgb', () => { + test('Converts hexadecimal color to RGB', () => { const editPix = new EditPix(); - const rgbColors = [[255, 0, 0], [0, 255, 0], [0, 0, 255]]; // Red, Green, Blue - const expectedHexColors = ['#ff0000', '#00ff00', '#0000ff']; - expect(editPix.convertToHex(rgbColors)).toEqual(expectedHexColors); - }); + // Test valid hexadecimal color inputs + expect(editPix.hexToRgb('#ff0000')).toEqual({ r: 255, g: 0, b: 0 }); + expect(editPix.hexToRgb('#00ff00')).toEqual({ r: 0, g: 255, b: 0 }); + expect(editPix.hexToRgb('#0000ff')).toEqual({ r: 0, g: 0, b: 255 }); - test('converts RGB colors to hex correctly for single color input', () => { - const editPix = new EditPix(); - const rgbColor = [255, 0, 0]; // Red - const expectedHexColor = ['#ff0000']; - expect(editPix.convertToHex(rgbColor)).toEqual(expectedHexColor); + // Test invalid hexadecimal color inputs + expect(() => editPix.hexToRgb('#FF00')).toThrow("Invalid hex color: #FF00"); + expect(() => editPix.hexToRgb('#00GG00')).toThrow("Invalid hex color: #00GG00"); + // Add more test cases for invalid inputs as needed }); }); diff --git a/test/utils.test.js b/test/utils.test.js index 310bd05..c3d56cb 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -1,75 +1,30 @@ import utils from "../src/utils.js" -describe('rgbToHex function', () => { - test('converts RGB colors to hexadecimal correctly', () => { - const rgbColors = [ - [255, 0, 0], // Red - [0, 255, 0], // Green - [0, 0, 255] // Blue - ]; - const expectedHexColors = ['#ff0000', '#00ff00', '#0000ff']; - expect(utils.rgbToHex(rgbColors)).toEqual(expectedHexColors); - }); - - test('handles empty input array', () => { - const rgbColors = []; - expect(utils.rgbToHex(rgbColors)).toEqual([]); - }); - - test('converts black color correctly', () => { - const rgbColors = [[0, 0, 0]]; - const expectedHexColors = ['#000000']; - expect(utils.rgbToHex(rgbColors)).toEqual(expectedHexColors); - }); - - test('converts white color correctly', () => { - const rgbColors = [[255, 255, 255]]; - const expectedHexColors = ['#ffffff']; - expect(utils.rgbToHex(rgbColors)).toEqual(expectedHexColors); +describe('rgbToHex', () => { + test('Converts RGB to hexadecimal color for valid inputs', () => { + // Test valid RGB inputs + expect(utils.rgbToHex(255, 0, 0)).toBe("#ff0000"); + expect(utils.rgbToHex(0, 255, 0)).toBe("#00ff00"); + expect(utils.rgbToHex(0, 0, 255)).toBe("#0000ff"); + // Add more test cases for valid inputs as needed }); }); -describe('hexToRgb function', () => { - test('converts hexadecimal colors to RGB correctly', () => { - const hexColors = ['#ff0000', '#00ff00', '#0000ff']; - const expectedRgbColors = [ - [255, 0, 0], // Red - [0, 255, 0], // Green - [0, 0, 255] // Blue - ]; - expect(utils.hexToRgb(hexColors)).toEqual(expectedRgbColors); - }); - - test('handles empty input array', () => { - const hexColors = []; - expect(utils.hexToRgb(hexColors)).toEqual([]); - }); - - test('converts black color correctly', () => { - const hexColors = ['#000000']; - const expectedRgbColors = [[0, 0, 0]]; - expect(utils.hexToRgb(hexColors)).toEqual(expectedRgbColors); - }); - - test('converts white color correctly', () => { - const hexColors = ['#ffffff']; - const expectedRgbColors = [[255, 255, 255]]; - expect(utils.hexToRgb(hexColors)).toEqual(expectedRgbColors); - }); - - test('throws an error for invalid hex input', () => { - const hexColors = ['#ff00', '#00ff00ff']; // Invalid hex colors - expect(() => utils.hexToRgb(hexColors)).toThrow(Error); +describe('hexToRgb', () => { + test('Converts hexadecimal color to RGB for valid inputs', () => { + // Test valid hexadecimal color inputs + expect(utils.hexToRgb('#FF0000')).toEqual({ r: 255, g: 0, b: 0 }); + expect(utils.hexToRgb('#00FF00')).toEqual({ r: 0, g: 255, b: 0 }); + expect(utils.hexToRgb('#0000FF')).toEqual({ r: 0, g: 0, b: 255 }); + // Add more test cases as needed }); - test('converts short hex colors to RGB correctly', () => { - const hexColors = ['#f00', '#0f0', '#00f']; // Short hex colors - const expectedRgbColors = [ - [255, 0, 0], // Red - [0, 255, 0], // Green - [0, 0, 255] // Blue - ]; - expect(utils.hexToRgb(hexColors)).toEqual(expectedRgbColors); + test('Throws error for invalid hexadecimal color inputs', () => { + // Test invalid hexadecimal color inputs + expect(() => utils.hexToRgb('#FF00')).toThrow(Error); + expect(() => utils.hexToRgb('#00GG00')).toThrow(Error); + expect(() => utils.hexToRgb('red')).toThrow(Error); + // Add more test cases as needed }); }); @@ -107,41 +62,44 @@ describe('validate function', () => { }); }); -describe('hslToRgb', () => { - test('Correctly converts HSL to RGB', () => { - expect(utils.hslToRgb(0, 100, 50)).toEqual([255, 0, 0]); - expect(utils.hslToRgb(120, 100, 50)).toEqual([0, 255, 0]); - expect(utils.hslToRgb(240, 100, 50)).toEqual([0, 0, 255]); - }); - - test('Correctly handles HSL with saturation or lightness at the limit', () => { - expect(utils.hslToRgb(0, 0, 0)).toEqual([0, 0, 0]); - expect(utils.hslToRgb(0, 100, 0)).toEqual([0, 0, 0]); - expect(utils.hslToRgb(0, 0, 100)).toEqual([255, 255, 255]); - expect(utils.hslToRgb(0, 100, 100)).toEqual([255, 255, 255]); +describe('rgbToHsl', () => { + test('Converts RGB to HSL for valid inputs', () => { + // Test valid RGB inputs + expect(utils.rgbToHsl(255, 0, 0)).toEqual({ h: 0, s: 100, l: 50 }); + expect(utils.rgbToHsl(0, 255, 0)).toEqual({ h: 120, s: 100, l: 50 }); + expect(utils.rgbToHsl(0, 0, 255)).toEqual({ h: 240, s: 100, l: 50 }); + // Add more test cases as needed }); - test('Throws an error for invalid HSL values', () => { - expect(() => utils.hslToRgb(-10, 100, 50)).toThrow(Error); - expect(() => utils.hslToRgb(361, 100, 50)).toThrow(Error); - expect(() => utils.hslToRgb(0, -10, 50)).toThrow(Error); - expect(() => utils.hslToRgb(0, 100, 101)).toThrow(Error); + test('Throws error for invalid RGB inputs', () => { + // Test invalid RGB inputs + expect(() => utils.rgbToHsl(-1, 0, 0)).toThrow(Error); + expect(() => utils.rgbToHsl(256, 0, 0)).toThrow(Error); + expect(() => utils.rgbToHsl(0, -1, 0)).toThrow(Error); + expect(() => utils.rgbToHsl(0, 256, 0)).toThrow(Error); + expect(() => utils.rgbToHsl(0, 0, -1)).toThrow(Error); + expect(() => utils.rgbToHsl(0, 0, 256)).toThrow(Error); + // Add more test cases as needed }); }); -describe('rgbToHsl', () => { - test('Correctly converts RGB to HSL', () => { - expect(utils.rgbToHsl(255, 0, 0)).toEqual([0, 100, 50]); - expect(utils.rgbToHsl(0, 255, 0)).toEqual([120, 100, 50]); - expect(utils.rgbToHsl(0, 0, 255)).toEqual([240, 100, 50]); - expect(utils.rgbToHsl(255, 255, 255)).toEqual([0, 0, 100]); - expect(utils.rgbToHsl(128, 128, 128)).toEqual([0, 0, 50]); +describe('hslToRgb', () => { + test('Converts HSL to RGB for valid inputs', () => { + // Test valid HSL inputs + expect(utils.hslToRgb(0, 100, 50)).toEqual({ r: 255, g: 0, b: 0 }); + expect(utils.hslToRgb(120, 100, 50)).toEqual({ r: 0, g: 255, b: 0 }); + expect(utils.hslToRgb(240, 100, 50)).toEqual({ r: 0, g: 0, b: 255 }); + // Add more test cases as needed }); - test('Throws an error for invalid RGB values', () => { - expect(() => utils.rgbToHsl(-10, 0, 0)).toThrow(Error); - expect(() => utils.rgbToHsl(256, 0, 0)).toThrow(Error); - expect(() => utils.rgbToHsl(0, 300, 0)).toThrow(Error); - expect(() => utils.rgbToHsl(0, 0, -20)).toThrow(Error); + test('Throws error for invalid HSL inputs', () => { + // Test invalid HSL inputs + expect(() => utils.hslToRgb(-1, 100, 50)).toThrow(Error); + expect(() => utils.hslToRgb(361, 100, 50)).toThrow(Error); + expect(() => utils.hslToRgb(0, -1, 50)).toThrow(Error); + expect(() => utils.hslToRgb(0, 101, 50)).toThrow(Error); + expect(() => utils.hslToRgb(0, 100, -1)).toThrow(Error); + expect(() => utils.hslToRgb(0, 100, 101)).toThrow(Error); + // Add more test cases as needed }); }); \ No newline at end of file