From e4c12bb3d9e41b1042e32d2e2d329ddf4af952e7 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 14 Oct 2024 13:34:11 +0800 Subject: [PATCH 1/2] Add tests for getAppSettings --- .../controllers/settingsController.test.js | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Server/tests/controllers/settingsController.test.js diff --git a/Server/tests/controllers/settingsController.test.js b/Server/tests/controllers/settingsController.test.js new file mode 100644 index 000000000..387f19c21 --- /dev/null +++ b/Server/tests/controllers/settingsController.test.js @@ -0,0 +1,49 @@ +const { afterEach } = require("node:test"); +const { + getAppSettings, + updateAppSettings, +} = require("../../controllers/settingsController"); + +const { errorMessages, successMessages } = require("../../utils/messages"); +const sinon = require("sinon"); + +describe("Settings Controller - getAppSettings", () => { + beforeEach(() => { + req = { + headers: {}, + params: {}, + body: {}, + db: {}, + settingsService: { + getSettings: sinon.stub(), + }, + }; + res = { + status: sinon.stub().returnsThis(), + json: sinon.stub(), + }; + next = sinon.stub(); + handleError = sinon.stub(); + }); + afterEach(() => { + sinon.restore(); + }); + it("should throw an error if getSettings throws an error", async () => { + req.settingsService.getSettings.throws(new Error("getSettings error")); + await getAppSettings(req, res, next); + expect(next.firstCall.args[0]).to.be.an("error"); + expect(next.firstCall.args[0].message).to.equal("getSettings error"); + }); + + it("should return a success message and data if getSettings is successful", async () => { + const data = { data: "settings" }; + req.settingsService.getSettings.returns(data); + await getAppSettings(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.GET_APP_SETTINGS, + data, + }); + }); +}); From f34b8466cde1e07d30f52ae0995f8b18074db1cf Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Mon, 14 Oct 2024 13:38:53 +0800 Subject: [PATCH 2/2] Add tests for updateAppSettings --- .../controllers/settingsController.test.js | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/Server/tests/controllers/settingsController.test.js b/Server/tests/controllers/settingsController.test.js index 387f19c21..b06565f38 100644 --- a/Server/tests/controllers/settingsController.test.js +++ b/Server/tests/controllers/settingsController.test.js @@ -47,3 +47,59 @@ describe("Settings Controller - getAppSettings", () => { }); }); }); + +describe("Settings Controller - updateAppSettings", () => { + beforeEach(() => { + req = { + headers: {}, + params: {}, + body: {}, + db: { + updateAppSettings: sinon.stub(), + }, + settingsService: { + reloadSettings: 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 body validation fails", async () => { + req.body = { invalid: 1 }; + await updateAppSettings(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 updateAppSettings throws an error", async () => { + req.db.updateAppSettings.throws(new Error("updateAppSettings error")); + await updateAppSettings(req, res, next); + expect(next.firstCall.args[0]).to.be.an("error"); + expect(next.firstCall.args[0].message).to.equal("updateAppSettings error"); + }); + it("should reject with an error if reloadSettings throws an error", async () => { + req.settingsService.reloadSettings.throws( + new Error("reloadSettings error") + ); + await updateAppSettings(req, res, next); + expect(next.firstCall.args[0]).to.be.an("error"); + expect(next.firstCall.args[0].message).to.equal("reloadSettings error"); + }); + it("should return a success message and data if updateAppSettings is successful", async () => { + const data = { data: "settings" }; + req.settingsService.reloadSettings.returns(data); + await updateAppSettings(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.UPDATE_APP_SETTINGS, + data, + }); + }); +});