Add tests for getChecks
This commit is contained in:
1 parent
22a6d2ef2c
commit
9eb06a477f
2 files changed
+56
-2
No files matched your search
@@ -20,7 +20,6 @@ const createCheck = async (req, res, next) => {
|
|||||||
await createCheckParamValidation.validateAsync(req.params);
|
await createCheckParamValidation.validateAsync(req.params);
|
||||||
await createCheckBodyValidation.validateAsync(req.body);
|
await createCheckBodyValidation.validateAsync(req.body);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
|
||||||
next(handleValidationError(error, SERVICE_NAME));
|
next(handleValidationError(error, SERVICE_NAME));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
const { createCheck } = require("../../controllers/checkController");
|
const { createCheck, getChecks } = require("../../controllers/checkController");
|
||||||
const jwt = require("jsonwebtoken");
|
const jwt = require("jsonwebtoken");
|
||||||
const { errorMessages, successMessages } = require("../../utils/messages");
|
const { errorMessages, successMessages } = require("../../utils/messages");
|
||||||
const sinon = require("sinon");
|
const sinon = require("sinon");
|
||||||
@@ -61,3 +61,58 @@ describe("Check Controller - createCheck", () => {
|
|||||||
expect(next.notCalled).to.be.true;
|
expect(next.notCalled).to.be.true;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("Check Controller - getChecks", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
req = {
|
||||||
|
params: {},
|
||||||
|
query: {},
|
||||||
|
db: {
|
||||||
|
getChecks: sinon.stub(),
|
||||||
|
getChecksCount: sinon.stub(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
res = {
|
||||||
|
status: sinon.stub().returnsThis(),
|
||||||
|
json: sinon.stub(),
|
||||||
|
};
|
||||||
|
next = sinon.stub();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
sinon.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should reject with a validation error if params are invalid", async () => {
|
||||||
|
await getChecks(req, res, next);
|
||||||
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
|
expect(next.firstCall.args[0].status).to.equal(422);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return a success message if checks are found", async () => {
|
||||||
|
req.params = {
|
||||||
|
monitorId: "monitorId",
|
||||||
|
};
|
||||||
|
req.db.getChecks.resolves([{ id: "123" }]);
|
||||||
|
req.db.getChecksCount.resolves(1);
|
||||||
|
await getChecks(req, res, next);
|
||||||
|
expect(res.status.calledWith(200)).to.be.true;
|
||||||
|
expect(
|
||||||
|
res.json.calledWith({
|
||||||
|
success: true,
|
||||||
|
msg: successMessages.CHECK_GET,
|
||||||
|
data: { checksCount: 1, checks: [{ id: "123" }] },
|
||||||
|
})
|
||||||
|
).to.be.true;
|
||||||
|
expect(next.notCalled).to.be.true;
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle errors", async () => {
|
||||||
|
req.db.getChecks.rejects(new Error("error"));
|
||||||
|
await getChecks(req, res, next);
|
||||||
|
expect(next.firstCall.args[0]).to.be.an("error");
|
||||||
|
req.db.getChecks.resolves([]);
|
||||||
|
req.db.getChecksCount.rejects(new Error("error"));
|
||||||
|
await getChecks(req, res, next);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user