Skip to content
Snippets Groups Projects
Commit 817ef884 authored by Janne Seppänen's avatar Janne Seppänen
Browse files

Versio 1.0.0 / Janne

parents
No related branches found
Tags v1.0.0
No related merge requests found
Pipeline #464 failed
node_modules
\ No newline at end of file
This diff is collapsed.
{
"name": "integration-test",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "set NODE_ENV=test&& mocha --reporter nyan",
"dev": "set NODE_ENV=dev&& node ./src/server.js"
},
"repository": {
"type": "git",
"url": "https://version.lab.fi/Janne.Seppanen/integration-test.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"chai": "^4.3.4",
"mocha": "^8.3.2",
"request": "^2.88.2"
},
"directories": {
"test": "test"
},
"dependencies": {
"express": "^4.17.1"
},
"description": ""
}
/**
* Paddin outputs 2 characters always
* @param {string} hex one or two characters
* @returns {string} hex with two characters
*/
const pad = (hex) => {
return (hex.length === 1 ? "0" + hex : hex)
}
module.exports = {
/**
* Converts RGB to Hex string
* @param {number} red 0-255
* @param {number} green 0-255
* @param {number} blue 0-255
* @returns {string} hex value
*/
rgbToHex: (red, green, blue) => {
const redHex = red.toString(16); //may return single char
const greenHex = green.toString(16);
const blueHex = blue.toString(16);
return pad(redHex) + pad(greenHex) + pad(blueHex);
}
}
\ No newline at end of file
const { request } = require('express');
const express = require('express');
const converter = require('./converter');
const app = express();
const port = 3000;
app.get('/', (req, res) => res.send("Hello"));
app.get('/rgb-to-hex', (req, res) => {
const red = parseInt(req.query.red, 10);
const green = parseInt(req.query.green, 10);
const blue = parseInt(req.query.blue);
res.send(converter.rgbToHex(red,green,blue));
});
if (process.env.NODE_ENV === 'test') {
module.exports = app;
} else {
app.listen(port, () => console.log(`Server: localhost:${port}`));
}
\ No newline at end of file
const expect = require("chai").expect;
const converter = require("../src/converter");
describe("Color Code Converter", () => {
describe("RGB to Hex conversion", () => {
it("converts the basic colors", () => {
const redHex = converter.rgbToHex(255,0,0,); // ff0000
const greenHex = converter.rgbToHex(0,255,0,); // 00ff00
const blueHex = converter.rgbToHex(0,0,255,); // 0000ff
expect(redHex).to.equal("ff0000");
expect(greenHex).to.equal("00ff00");
expect(blueHex).to.equal("0000ff");
});
});
});
\ No newline at end of file
// Integration test
const expect = require("chai").expect;
const request = require("request");
const app = require("../src/server");
const port = 3000;
describe("Color Code Converter API", () => {
before("Start server before run tests", (done) => {
server = app.listen(port, () => {
console.log(`Server listening: localhost: ${port}`);
done();
});
});
describe("RGB to Hex conversion", () => {
const url = `http://localhost:${port}/rgb-to-hex?red=255&green=255&blue=255`;
it("returns status 200", (done) => {
request(url, (error, response, body) => {
expect(response.statusCode).to.equal(200);
done();
});
});
it("returns the color in hex", (done) => {
request(url, (errpr, response, body) => {
expect(body).to.equal("ffffff");
done();
});
});
});
after("Stop server after tests", (done) => {
server.close();
done();
})
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment