From 22a6d2ef2c32efbb82b9019d9bda574078f12ad8 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 11 Oct 2024 10:39:18 +0800 Subject: [PATCH] Add creatCheck test --- Server/.mocharc.js | 2 +- Server/controllers/checkController.js | 1 + .../tests/controllers/checkController.test.js | 63 +++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 Server/tests/controllers/checkController.test.js diff --git a/Server/.mocharc.js b/Server/.mocharc.js index 5c8840544..87f578c2e 100644 --- a/Server/.mocharc.js +++ b/Server/.mocharc.js @@ -1,6 +1,6 @@ module.exports = { require: ["chai/register-expect.js"], // Include Chai's "expect" interface globally - spec: "tests/**/*.test.js", // Specify test files + // spec: "tests/**/*.test.js", // Specify test files timeout: 5000, // Set test-case timeout in milliseconds recursive: true, // Include subdirectories reporter: "spec", // Use the "spec" reporter diff --git a/Server/controllers/checkController.js b/Server/controllers/checkController.js index b0e98ed38..130509b1e 100644 --- a/Server/controllers/checkController.js +++ b/Server/controllers/checkController.js @@ -20,6 +20,7 @@ const createCheck = async (req, res, next) => { await createCheckParamValidation.validateAsync(req.params); await createCheckBodyValidation.validateAsync(req.body); } catch (error) { + console.log(error); next(handleValidationError(error, SERVICE_NAME)); return; } diff --git a/Server/tests/controllers/checkController.test.js b/Server/tests/controllers/checkController.test.js new file mode 100644 index 000000000..43f431df2 --- /dev/null +++ b/Server/tests/controllers/checkController.test.js @@ -0,0 +1,63 @@ +const { createCheck } = require("../../controllers/checkController"); +const jwt = require("jsonwebtoken"); +const { errorMessages, successMessages } = require("../../utils/messages"); +const sinon = require("sinon"); + +describe("Check Controller - createCheck", () => { + beforeEach(() => { + req = { + params: {}, + body: {}, + db: { + createCheck: sinon.stub(), + }, + }; + res = { + status: sinon.stub().returnsThis(), + json: sinon.stub(), + }; + next = sinon.stub(); + handleError = sinon.stub(); + }); + + afterEach(() => { + sinon.restore(); // Restore the original methods after each test + }); + + it("should reject with a validation if params are invalid", async () => { + await createCheck(req, res, next); + expect(next.firstCall.args[0]).to.be.an("error"); + expect(next.firstCall.args[0].status).to.equal(422); + }); + + it("should reject with a validation error if body is invalid", async () => { + req.params = { + monitorId: "monitorId", + }; + await createCheck(req, res, next); + expect(next.firstCall.args[0]).to.be.an("error"); + }); + it("should return a success message if check is created", async () => { + req.params = { + monitorId: "monitorId", + }; + req.db.createCheck.resolves({ id: "123" }); + req.body = { + monitorId: "monitorId", + status: true, + responseTime: 100, + statusCode: 200, + message: "message", + }; + await createCheck(req, res, next); + expect(res.status.calledWith(200)).to.be.true; + expect( + res.json.calledWith({ + success: true, + msg: successMessages.CHECK_CREATE, + data: { id: "123" }, + }) + ).to.be.true; + expect(next.notCalled).to.be.true; + }); +});