From b063fa267a72a935c348297ae7299a9e669e3285 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 14 Oct 2024 13:17:08 +0800 Subject: [PATCH] add test for addJob --- Server/controllers/queueController.js | 17 +++++---- .../tests/controllers/queueController.test.js | 38 +++++++++++++++++++ Server/utils/messages.js | 2 + 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/Server/controllers/queueController.js b/Server/controllers/queueController.js index 68ab7b28d..c49e72c62 100644 --- a/Server/controllers/queueController.js +++ b/Server/controllers/queueController.js @@ -19,13 +19,11 @@ const getMetrics = async (req, res, next) => { const getJobs = async (req, res, next) => { try { const jobs = await req.jobQueue.getJobStats(); - return res - .status(200) - .json({ - success: true, - msg: successMessages.QUEUE_GET_METRICS, - data: jobs, - }); + return res.status(200).json({ + success: true, + msg: successMessages.QUEUE_GET_METRICS, + data: jobs, + }); } catch (error) { next(handleError(error, SERVICE_NAME, "getJobs")); return; @@ -35,7 +33,10 @@ const getJobs = async (req, res, next) => { const addJob = async (req, res, next) => { try { await req.jobQueue.addJob(Math.random().toString(36).substring(7)); - return res.send("Added job"); + return res.status(200).json({ + success: true, + msg: successMessages.QUEUE_ADD_JOB, + }); } catch (error) { next(handleError(error, SERVICE_NAME, "addJob")); return; diff --git a/Server/tests/controllers/queueController.test.js b/Server/tests/controllers/queueController.test.js index 584f240fc..8c0e66ab8 100644 --- a/Server/tests/controllers/queueController.test.js +++ b/Server/tests/controllers/queueController.test.js @@ -90,3 +90,41 @@ describe("Queue Controller - getJobs", () => { }); }); }); + +describe("Queue Controller - addJob", () => { + beforeEach(() => { + req = { + headers: {}, + params: {}, + body: {}, + db: {}, + jobQueue: { + addJob: sinon.stub(), + }, + }; + res = { + status: sinon.stub().returnsThis(), + json: sinon.stub(), + }; + next = sinon.stub(); + handleError = sinon.stub(); + }); + afterEach(() => { + sinon.restore(); + }); + it("should reject with an error if addJob throws an error", async () => { + req.jobQueue.addJob.throws(new Error("addJob error")); + await addJob(req, res, next); + expect(next.firstCall.args[0]).to.be.an("error"); + expect(next.firstCall.args[0].message).to.equal("addJob error"); + }); + it("should return a success message if addJob is successful", async () => { + req.jobQueue.addJob.resolves(); + await addJob(req, res, next); + expect(res.status.firstCall.args[0]).to.equal(200); + expect(res.json.firstCall.args[0]).to.deep.equal({ + success: true, + msg: successMessages.QUEUE_ADD_JOB, + }); + }); +}); diff --git a/Server/utils/messages.js b/Server/utils/messages.js index c1df6af3b..d685e7326 100644 --- a/Server/utils/messages.js +++ b/Server/utils/messages.js @@ -89,6 +89,8 @@ const successMessages = { // Queue Controller QUEUE_GET_METRICS: "Got metrics successfully", QUEUE_GET_METRICS: "Got job stats successfully", + QUEUE_ADD_JOB: "Job added successfully", + QUEUE_OBLITERATE: "Queue obliterated", //Job Queue JOB_QUEUE_DELETE_JOB: "Job removed successfully",