From 312e6be9b86d8750d72cc894a7870a3d26d48abc Mon Sep 17 00:00:00 2001 From: Aditya Date: Wed, 23 Oct 2019 22:09:15 +0530 Subject: [PATCH] Added area of square --- routes/route.js | 29 +++++++++++++++++++++++++++-- test/tests.js | 26 ++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/routes/route.js b/routes/route.js index eacbb2d..e5a0347 100644 --- a/routes/route.js +++ b/routes/route.js @@ -139,13 +139,38 @@ router.post("/areaOfRectangle", (req, res) => { try{ const { param1, param2 } = req.body; - let result = parseFloat(param2, 10) * parseFloat(param1, 10); + let result = parseFloat(param1, 10) * parseFloat(param2, 10); + console.log(result) + res.json({ + result, + meta: { + success:true, + message: `Calculated area of rectangle with sides ${param1, param2}`, + code: 200 + } + }); + } catch (err) { + res.json({ + meta: { + success: false, + message: err.message, + code: 400 + } + }); + } +}); + +router.post("/areaOfSquare", (req, res) => { + try{ + const { param1 } = req.body; + + let result = Math.pow(parseFloat(param1, 10),2); res.json({ result, meta: { success:true, - message: `Calculated area of rectangle with sides ${param1}`, + message: `Calculated area of square with side ${param1}`, code: 200 } }); diff --git a/test/tests.js b/test/tests.js index f77d81a..4e64ed3 100644 --- a/test/tests.js +++ b/test/tests.js @@ -135,8 +135,8 @@ describe("----------START TEST FOR app.js----------", () => { it("Checks the POST /math/areaOfRectangle", done => { chai .request(app) - .post("/math/power") - .send({ param1: 3, param2: 2 }) + .post("/math/areaOfRectangle") + .send({ param1: 2, param2: 3}) .end((err, res) => { if (err) { done(err); @@ -153,4 +153,26 @@ describe("----------START TEST FOR app.js----------", () => { } }); }); + + it("Checks the POST /math/areaOfSquare", done => { + chai + .request(app) + .post("/math/areaOfSquare") + .send({ param1: 1 }) + .end((err, res) => { + if (err) { + done(err); + process.exit(1); + } else { + res.body.result.should.be.a("number"); + res.body.meta.success.should.be.a("boolean"); + res.body.meta.message.should.be.a("string"); + res.body.meta.code.should.be.a("number"); + + res.body.result.should.equal(1); + + done(); + } + }); + }); });