Question
Need help with Add mocha testing code (describe and it) in the testconverter.js and testserver.js files to test the added new code above. Test converter.js:
Need help with Add mocha testing code (describe and it) in the test\converter.js and test\server.js files to test the added new code above.
Test converter.js:
var expect = require("chai").expect; var converter = require("../app/converter");
describe("Color Code Converter - stand alone test", function() { describe("RGB to Hex conversion", function() { it("converts the basic colors: red, green, blue", function() { var redHex = converter.rgbToHex(255, 0, 0); var greenHex = converter.rgbToHex(0, 255, 0); var blueHex = converter.rgbToHex(0, 0, 255);
expect(redHex).to.equal("ff0000"); expect(greenHex).to.equal("00ff00"); expect(blueHex).to.equal("0000ff"); }); });
describe("Hex to RGB conversion", function() { it("converts the basic colors: red, green, blue", function() { var red = converter.hexToRgb("ff0000"); var green = converter.hexToRgb("00ff00"); var blue = converter.hexToRgb("0000ff");
expect(red).to.deep.equal([255, 0, 0]); expect(green).to.deep.equal([0, 255, 0]); expect(blue).to.deep.equal([0, 0, 255]); }); }); describe("", function(){ it("converts ", function(){ }); }); describe("", function(){ it("converts", function(){ }); }); });
...............................................
Test Server.js:
var expect = require("chai").expect; var request = require("request");
describe("Color Code Converter - web service version", function() {
describe("RGB to Hex conversion - http://localhost:3000/rgbToHex?red=255&green=255&blue=255", function() {
var url = "http://localhost:3000/rgbToHex?red=255&green=255&blue=255";
it("returns status 200", function(done) { request(url, function(error, response, body) { expect(response.statusCode).to.equal(200); done(); }); });
it("returns the color in hex", function(done) { request(url, function(error, response, body) { expect(body).to.equal("ffffff"); done(); }); });
});
describe("Hex to RGB conversion - http://localhost:3000/hexToRgb?hex=00ff00", function() { var url = "http://localhost:3000/hexToRgb?hex=00ff00";
it("returns status 200", function(done) { request(url, function(error, response, body) { expect(response.statusCode).to.equal(200); done(); }); });
it("returns the color in RGB", function(done) { request(url, function(error, response, body) { expect(body).to.equal("[0,255,0]"); done(); }); }); });
});
................................................
App Converter.js
exports.rgbToHex = function(red, green, blue) {
var redHex = red.toString(16); var greenHex = green.toString(16); var blueHex = blue.toString(16);
return pad(redHex) + pad(greenHex) + pad(blueHex);
};
function pad(hex) { return (hex.length === 1 ? "0" + hex : hex); }
exports.hexToRgb = function(hex) {
var red = parseInt(hex.substring(0, 2), 16); var green = parseInt(hex.substring(2, 4), 16); var blue = parseInt(hex.substring(4, 6), 16);
return [red, green, blue]; exports.celciusToFahrenheit = function(celcius) {//adding cel to fah function return Math.round(celcius * 9 / 5 + 32); };
exports.fahrenheitToCelcius = function(fahrenheit) {//adding fah to cel function return Math.round((fahrenheit - 32) * 5 / 9); };
};
.................................................................
App Server.js
var express = require("express"); var app = express(); var converter = require("./converter");
app.get("/rgbToHex", function(req, res) { var red = parseInt(req.query.red, 10); var green = parseInt(req.query.green, 10); var blue = parseInt(req.query.blue, 10);
var hex = converter.rgbToHex(red, green, blue);
res.send(hex); });
app.get("/hexToRgb", function(req, res) { var hex = req.query.hex;
var rgb = converter.hexToRgb(hex);
res.send(JSON.stringify(rgb)); });
app.get("/celciusToFahrenheit", function(req, res) {//adding cel to fah function var celcius = req.query.celcius; var fahrenheit = converter.celciusToFahrenheit(celcius); res.send(JSON.stringify(fahrenheit)); });
app.get("/fahrenheitToCelcius", function(req, res) {//adding fah to cel function var fahrenheit = req.query.fahrenheit; var celcius = converter.fahrenheitToCelcius(fahrenheit); res.send(JSON.stringify(celcius)); });
app.listen(3000); console.log("server started at http://localhost:3000...");
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started