Added getMoniotrsByTeamId tests

This commit is contained in:
Alex Holliday
2024-10-14 09:43:47 +08:00
parent 73e087982b
commit a0b651fd24
2 changed files with 55 additions and 1 deletions
+1 -1
View File
@@ -208,7 +208,7 @@ const getMonitorsByTeamId = async (req, res, next) => {
try {
const teamId = req.params.teamId;
const monitors = await req.db.getMonitorsByTeamId(req, res);
return res.json({
return res.status(200).json({
success: true,
msg: successMessages.MONITOR_GET_BY_USER_ID(teamId),
data: monitors,
@@ -321,3 +321,57 @@ describe("Monitor Controller - getMonitorsAndSummaryByTeamId", () => {
).to.be.true;
});
});
describe("Monitor Controller - getMonitorsByTeamId", () => {
beforeEach(() => {
req = {
params: {
teamId: "123",
},
query: {},
body: {},
db: {
getMonitorsByTeamId: sinon.stub(),
},
};
res = {
status: sinon.stub().returnsThis(),
json: sinon.stub(),
};
next = sinon.stub();
});
afterEach(() => {
sinon.restore();
});
it("should reject with an error if param validation fails", async () => {
req.params = {};
await getMonitorsByTeamId(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 an error if query validation fails", async () => {
req.query = { invalid: 1 };
await getMonitorsByTeamId(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 an error if DB operations fail", async () => {
req.db.getMonitorsByTeamId.throws(new Error("DB error"));
await getMonitorsByTeamId(req, res, next);
expect(next.firstCall.args[0]).to.be.an("error");
expect(next.firstCall.args[0].message).to.equal("DB error");
});
it("should return success message and data if all operations succeed", async () => {
const data = { monitors: "data" };
req.db.getMonitorsByTeamId.returns(data);
await getMonitorsByTeamId(req, res, next);
expect(res.status.firstCall.args[0]).to.equal(200);
expect(
res.json.calledOnceWith({
success: true,
msg: successMessages.MONITOR_GET_BY_USER_ID(req.params.teamId),
data: data,
})
).to.be.true;
});
});