add test for addJob

This commit is contained in:
Alex Holliday committed 2024-10-14 13:17:08 +08:00
1 parent 61518f898b
commit b063fa267a
3 files changed
+49 -8

No files matched your search

+9 -8
View File
@@ -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;
@@ -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,
});
});
});
+2
View File
@@ -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",