3
05/11/2024 9:24 am
Topic starter
Write Mocha tests to check the functionality of the following JS code:
function rgbToHexColor(red, green, blue) { if (!Number.isInteger(red) || (red < 0) || (red > 255)) { return undefined; } if (!Number.isInteger(green) || (green < 0) || (green > 255)) { return undefined; } if (!Number.isInteger(blue) || (blue < 0) || (blue > 255)) { return undefined; } return "#" + ("0" + red.toString(16).toUpperCase()).slice(-2) + ("0" + green.toString(16).toUpperCase()).slice(-2) + ("0" + blue.toString(16).toUpperCase()).slice(-2); } console.log(rgbToHexColor(13, 14, 15)); console.log(rgbToHexColor()); //console.log(rgbToHexColor(0, 0, 0)); //console.log(rgbToHexColor("13", 14, 15)); //console.log(rgbToHexColor(-13, 14, 15)); //console.log(rgbToHexColor(266, 14, 15)); module.exports = {rgbToHexColor};
Your tests will be supplied a function named 'rgbToHexColor', which takes three arguments. It needs to meet the following requirements:
- Takes three integer numbers, representing the red, green and blue values of an RGB color, each within range [0…255]
- Returns the same color in hexadecimal format as a string (e.g. '#FF9EAA')
- Returns 'undefined' if any of the input parameters are of invalid type or not in the expected range
1 Answer
2
05/11/2024 9:25 am
My tests:
let rgbToHexColor = require("../06.RGBToHex").rgbToHexColor; let expect = require("chai").expect; describe("Tests for the RGB TO HEX task", function () { describe("General test", function () { it("should return #100C0D for (16, 12, 13)", function () { expect(rgbToHexColor(16, 12, 13)).to.equal("#100C0D"); }) }); describe("Test lowest possible input: zeros", function () { it("test with zeros", function () { expect(rgbToHexColor(0, 0, 0)).to.equal("#000000"); }) }); describe("Test highest possible input: 255", function () { it("test with 255", function () { expect(rgbToHexColor(255, 255, 255)).to.equal("#FFFFFF"); }) }); describe("Invalid input -negative number", function () { it("should be undefined", function () { expect(typeof rgbToHexColor(-16, 12, 13)).to.equal("undefined"); }) }); describe("Invalid input -negative number", function () { it("should be undefined", function () { expect(typeof rgbToHexColor(16, -12, 13)).to.equal("undefined"); }) }); describe("Invalid input -negative number", function () { it("should be undefined", function () { expect(typeof rgbToHexColor(16, 12, -13)).to.equal("undefined"); }) }); describe("Invalid input out of the Range", function () { it("should be undefined", function () { expect(typeof rgbToHexColor(266, 12, 13)).to.equal("undefined"); }) }); describe("Invalid input: string", function () { it("should be undefined", function () { expect(typeof rgbToHexColor("266", 12, 13)).to.equal("undefined"); }) }); describe("Invalid input: fractional numbers", function () { it("should be undefined", function () { expect(typeof rgbToHexColor(3.13, 12, 13)).to.equal("undefined"); }) }); describe("Pad values with zeros", function () { it("should pad values with zeros", function () { expect(rgbToHexColor(12, 13, 14)).to.equal("#0C0D0E"); }) }); describe("No input test", function () { it("no input test", function () { expect(rgbToHexColor()).to.be.undefined; }) }); });