Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/be/tests settings module #1146

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions Server/tests/db/settingsModule.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import sinon from "sinon";
import {
getAppSettings,
updateAppSettings,
} from "../../db/mongo/modules/settingsModule.js";
import AppSettings from "../../db/models/AppSettings.js";

const mockAppSettings = {
appName: "Test App",
};

describe("SettingsModule", () => {
let appSettingsFindOneStub, appSettingsFindOneAndUpdateStub;
beforeEach(() => {
appSettingsFindOneStub = sinon.stub(AppSettings, "findOne");
appSettingsFindOneAndUpdateStub = sinon.stub(AppSettings, "findOneAndUpdate");
});
afterEach(() => {
sinon.restore();
});
Comment on lines +14 to +20
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Mom's spaghetti moment: Let's verify those stub calls! 🍝

The setup is clean, but we're missing verification that our stubs are called with the expected parameters. Consider adding assertions to verify the stub calls.

 it("should return app settings", async () => {
   appSettingsFindOneStub.resolves(mockAppSettings);
   const result = await getAppSettings();
   expect(result).to.deep.equal(mockAppSettings);
+  sinon.assert.calledOnce(appSettingsFindOneStub);
+  sinon.assert.calledWith(appSettingsFindOneStub, {});
 });

Committable suggestion skipped: line range outside the PR's diff.


describe("getAppSettings", () => {
it("should return app settings", async () => {
appSettingsFindOneStub.resolves(mockAppSettings);
const result = await getAppSettings();
expect(result).to.deep.equal(mockAppSettings);
});
it("should handle an error", async () => {
const err = new Error("Test error");
appSettingsFindOneStub.throws(err);
try {
await getAppSettings();
} catch (error) {
expect(error).to.deep.equal(err);
}
});
});
Comment on lines +22 to +37
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Yo! We're missing some edge cases here! 🍝

The test suite should include cases for when the database returns null or empty results.

+it("should handle null settings", async () => {
+  appSettingsFindOneStub.resolves(null);
+  const result = await getAppSettings();
+  expect(result).to.be.null;
+});
+
+it("should handle empty settings", async () => {
+  appSettingsFindOneStub.resolves({});
+  const result = await getAppSettings();
+  expect(result).to.deep.equal({});
+});
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
describe("getAppSettings", () => {
it("should return app settings", async () => {
appSettingsFindOneStub.resolves(mockAppSettings);
const result = await getAppSettings();
expect(result).to.deep.equal(mockAppSettings);
});
it("should handle an error", async () => {
const err = new Error("Test error");
appSettingsFindOneStub.throws(err);
try {
await getAppSettings();
} catch (error) {
expect(error).to.deep.equal(err);
}
});
});
describe("getAppSettings", () => {
it("should return app settings", async () => {
appSettingsFindOneStub.resolves(mockAppSettings);
const result = await getAppSettings();
expect(result).to.deep.equal(mockAppSettings);
});
it("should handle an error", async () => {
const err = new Error("Test error");
appSettingsFindOneStub.throws(err);
try {
await getAppSettings();
} catch (error) {
expect(error).to.deep.equal(err);
}
});
it("should handle null settings", async () => {
appSettingsFindOneStub.resolves(null);
const result = await getAppSettings();
expect(result).to.be.null;
});
it("should handle empty settings", async () => {
appSettingsFindOneStub.resolves({});
const result = await getAppSettings();
expect(result).to.deep.equal({});
});
});

describe("updateAppSettings", () => {
it("should update app settings", async () => {
appSettingsFindOneAndUpdateStub.resolves(mockAppSettings);
const result = await updateAppSettings(mockAppSettings);
expect(result).to.deep.equal(mockAppSettings);
});
it("should handle an error", async () => {
const err = new Error("Test error");
appSettingsFindOneAndUpdateStub.throws(err);
try {
await updateAppSettings(mockAppSettings);
} catch (error) {
expect(error).to.deep.equal(err);
}
});
});
Comment on lines +38 to +53
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Time to level up these update tests! 💪

The updateAppSettings tests could use some additional scenarios and parameter validation.

 describe("updateAppSettings", () => {
+  it("should validate update parameters", async () => {
+    await expect(updateAppSettings(null))
+      .to.be.rejectedWith(Error, "Invalid settings");
+  });
+
+  it("should handle partial updates", async () => {
+    const partialUpdate = { appName: "Updated App" };
+    appSettingsFindOneAndUpdateStub.resolves({
+      ...mockAppSettings,
+      ...partialUpdate
+    });
+    const result = await updateAppSettings(partialUpdate);
+    expect(result.appName).to.equal("Updated App");
+    sinon.assert.calledWith(
+      appSettingsFindOneAndUpdateStub,
+      {},
+      { $set: partialUpdate },
+      { new: true }
+    );
+  });
+
   // Existing tests...
 });

Committable suggestion skipped: line range outside the PR's diff.

});