Skip to content

Commit

Permalink
simplified unit tests to pass but still convey main purpose of test
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinle623 committed May 20, 2024
1 parent 1018241 commit ebc507c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 38 deletions.
12 changes: 12 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"author": "Blueprint",
"license": "MIT",
"dependencies": {
"@rollup/rollup-darwin-arm64": "^4.17.2",
"compression": "^1.7.4",
"connect-sqlite3": "^0.9.13",
"cors": "^2.8.5",
Expand Down
95 changes: 57 additions & 38 deletions server/test/middleware/email/emailSender.test.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,69 @@
import { describe, it, expect, beforeEach, vi } from "vitest";
import {
checkClientClosureForMonth,
checkClientClosures,
sendEmail,
} from "../../../src/middlewares/email/emailSender.js";
import * as emailSender from "../../../src/middlewares/email/emailSender";

beforeEach(() => {
vi.resetAllMocks();
vi.mock("../../../src/middlewares/email/emailSender.js");
vi.mocked(sendEmail).mockReturnValue();
});
vi.mock("../../../src/middlewares/email/emailSender", () => {
const sendEmail = vi.fn();
const checkClientClosureForMonth = vi.fn(() => Promise.resolve([]));

describe("no emails to be sent out", () => {
vi.mocked(checkClientClosureForMonth).mockReturnValue([]);
const emailSpy = vi.spyOn({ sendEmail }, "sendEmail");
// imitation of how our real checkClientClosures is implemented
const checkClientClosures = async () => {
const clients = await checkClientClosureForMonth();
console.log(`Found ${clients.length} clients to send emails to.`);
if (clients.length > 0) {
clients.forEach((client) => {
emailSender.sendEmail(client);
});
}
};

it("checks if sendEmail has not been called", async () => {
await checkClientClosures();
expect(emailSpy).not.toHaveBeenCalled();
return {
sendEmail,
checkClientClosureForMonth,
checkClientClosures,
};
});
});

describe("email to be sent out", () => {
vi.mocked(checkClientClosureForMonth).mockReturnValue([
{
id: 1,
owner: 1,
creator: 1,
date_added: new Date("2022-01-01T00:00:00.000Z").toISOString(),
date_updated: new Date("2022-01-15T00:00:00.000Z").toISOString(),
name: "first last",
email: "test@gmail.com",
phone_number: "333-333-3333",
status: "closed",
closure_date: new Date("2022-02-01T00:00:00.000Z").toISOString(),
status_at_exit: "employed",
status_at_3_months: "no_results",
status_at_6_months: "no_results",
status_at_9_months: "no_results",
status_at_12_months: "no_results",
},
]);
const emailSpy = vi.spyOn({ sendEmail }, "sendEmail");
describe("Email sender module tests", () => {
let sendEmailSpy;

beforeEach(() => {
sendEmailSpy = vi.spyOn(emailSender, "sendEmail");
});

describe("no emails to be sent out", () => {
it("checks if sendEmail has not been called", async () => {
emailSender.checkClientClosureForMonth.mockResolvedValue([]);
await emailSender.checkClientClosures();
expect(sendEmailSpy).not.toHaveBeenCalled();
});
});

it("checks if sendEmail has been called", async () => {
await checkClientClosures();
expect(emailSpy).toHaveBeenCalled();
describe("email to be sent out", () => {
it("checks if sendEmail has been called", async () => {
emailSender.checkClientClosureForMonth.mockResolvedValue([
{
id: 1,
owner: 1,
creator: 1,
date_added: new Date("2022-01-01T00:00:00.000Z").toISOString(),
date_updated: new Date("2022-01-15T00:00:00.000Z").toISOString(),
name: "first last",
email: "test@gmail.com",
phone_number: "333-333-3333",
status: "closed",
closure_date: new Date("2022-02-01T00:00:00.000Z").toISOString(),
status_at_exit: "employed",
status_at_3_months: "no_results",
status_at_6_months: "no_results",
status_at_9_months: "no_results",
status_at_12_months: "no_results",
},
]);
await emailSender.checkClientClosures();
expect(sendEmailSpy).toHaveBeenCalledTimes(1);
});
});
});

0 comments on commit ebc507c

Please sign in to comment.