Add tests

This commit is contained in:
Alex Holliday
2024-11-11 14:25:44 +08:00
parent 54fca685c0
commit 75ca6f3239
2 changed files with 210 additions and 0 deletions

View File

@@ -0,0 +1,128 @@
import sinon from "sinon";
import {
createStatusPage,
getStatusPageByUrl,
} from "../../controllers/statusPageController.js";
describe("statusPageController", () => {
let req, res, next;
beforeEach(() => {
req = {
params: {},
body: {},
db: {
createStatusPage: sinon.stub(),
getStatusPageByUrl: sinon.stub(),
},
};
res = {
status: sinon.stub().returnsThis(),
json: sinon.stub(),
};
next = sinon.stub();
});
afterEach(() => {
sinon.restore();
});
describe("createStatusPage", () => {
beforeEach(() => {
req.body = {
companyName: "Test Company",
url: "123456",
timezone: "America/Toronto",
color: "#000000",
theme: "light",
monitors: ["67309ca673788e808884c8ac", "67309ca673788e808884c8ac"],
};
});
afterEach(() => {
sinon.restore();
});
it("should handle a validation error", async () => {
req.body = {
// Invalid data that will trigger validation error
companyName: "",
url: "",
timezone: "",
color: "invalid",
theme: "invalid",
monitors: ["invalid-id"],
};
try {
await createStatusPage(req, res, next);
} catch (error) {
expect(error).to.be.an.instanceOf(Error);
expect(error.message).to.equal("Validation error");
}
});
it("should handle a db error", async () => {
const err = new Error("DB error");
req.db.createStatusPage.throws(err);
try {
await createStatusPage(req, res, next);
} catch (error) {
expect(error).to.deep.equal(err);
}
});
it("should insert a properly formatted status page", async () => {
const result = await createStatusPage(req, res, next);
expect(res.status.firstCall.args[0]).to.equal(200);
expect(res.json.firstCall.args[0].success).to.be.true;
});
});
describe("getStatusPageByUrl", () => {
beforeEach(() => {
req.params = {
url: "123456",
};
});
afterEach(() => {
sinon.restore();
});
it("should handle a validation error", async () => {
req.params = {
url: "",
};
try {
await getStatusPageByUrl(req, res, next);
} catch (error) {
expect(error).to.be.an.instanceOf(Error);
expect(error.message).to.equal("Validation error");
}
});
it("should handle a DB error", async () => {
const err = new Error("DB error");
req.db.getStatusPageByUrl.throws(err);
try {
await getStatusPageByUrl(req, res, next);
} catch (error) {
expect(error).to.deep.equal(err);
}
});
it("should return a status page", async () => {
const statusPage = {
_id: "123456",
companyName: "Test Company",
url: "123456",
};
req.db.getStatusPageByUrl.resolves(statusPage);
const result = await getStatusPageByUrl(req, res, next);
expect(res.status.firstCall.args[0]).to.equal(200);
expect(res.json.firstCall.args[0].success).to.be.true;
expect(res.json.firstCall.args[0].data).to.deep.equal(statusPage);
});
});
});

View File

@@ -0,0 +1,82 @@
import sinon from "sinon";
import {
createStatusPage,
getStatusPageByUrl,
urlIsUnique,
} from "../../db/mongo/modules/statusPageModule.js";
import StatusPage from "../../db/models/StatusPage.js";
import { errorMessages } from "../../utils/messages.js";
describe("statusPageModule", () => {
let statusPageFindOneStub, statusPageSaveStub, statusPageFindStub;
beforeEach(() => {
statusPageSaveStub = sinon.stub(StatusPage.prototype, "save");
statusPageFindOneStub = sinon.stub(StatusPage, "findOne");
statusPageFindStub = sinon.stub(StatusPage, "find");
});
afterEach(() => {
sinon.restore();
});
describe("createStatusPage", () => {
it("should throw an error if a non-unique url is provided", async () => {
statusPageFindOneStub.resolves(true);
statusPageFindStub.resolves([{}]);
try {
await createStatusPage({ url: "test" });
} catch (error) {
expect(error.status).to.equal(400);
expect(error.message).to.equal(errorMessages.STATUS_PAGE_URL_NOT_UNIQUE);
}
});
it("should return a status page if a unique url is provided", async () => {
statusPageFindOneStub.resolves(null);
statusPageFindStub.resolves([]);
const mockStatusPage = { url: "test" };
const statusPage = await createStatusPage(mockStatusPage);
expect(statusPage).to.exist;
expect(statusPage.url).to.equal(mockStatusPage.url);
});
});
describe("getStatusPageByUrl", () => {
it("should throw an error if a status page is not found", async () => {
statusPageFindOneStub.resolves(null);
try {
await getStatusPageByUrl("test");
} catch (error) {
expect(error.status).to.equal(404);
expect(error.message).to.equal(errorMessages.STATUS_PAGE_NOT_FOUND);
}
});
it("should return a status page if a status page is found", async () => {
const mockStatusPage = { url: "test" };
statusPageFindOneStub.resolves(mockStatusPage);
const statusPage = await getStatusPageByUrl(mockStatusPage.url);
expect(statusPage).to.exist;
expect(statusPage).to.deep.equal(mockStatusPage);
});
});
describe("urlIsUnique", () => {
it("should throw an error if an error occurs", async () => {
const err = new Error("test");
statusPageFindStub.rejects(err);
try {
await urlIsUnique("test");
} catch (error) {
expect(error).to.deep.equal(err);
}
});
it("should return true if a url is unique", async () => {
statusPageFindStub.resolves([]);
const isUnique = await urlIsUnique("test");
expect(isUnique).to.be.true;
});
it("should return false if a url is not unique", async () => {
statusPageFindStub.resolves([{ url: "test" }]);
const isUnique = await urlIsUnique("test");
expect(isUnique).to.be.false;
});
});
});