Skip to content

Commit 2f909f9

Browse files
committed
Added test for email helper
1 parent abaa1f1 commit 2f909f9

2 files changed

Lines changed: 60 additions & 5 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { expect, describe, it, mock, beforeEach, afterEach } from "bun:test";
2+
3+
const mockSendMail = mock(() => {
4+
console.log("SENDING");
5+
return Promise.resolve({});
6+
});
7+
8+
mock.module("@aws-sdk/client-sesv2", () => {
9+
class MockSESv2Client {
10+
send = mockSendMail;
11+
}
12+
13+
class MockSendEmailCommand {
14+
constructor(args: any) {
15+
return args;
16+
}
17+
}
18+
19+
return {
20+
SESv2Client: MockSESv2Client,
21+
SendEmailCommand: MockSendEmailCommand,
22+
};
23+
});
24+
25+
const originalEnv = process.env;
26+
beforeEach(() => {
27+
process.env = {
28+
...originalEnv,
29+
AWS_REGION: "us-east-1",
30+
EMAIL_AWS_ACCESS_KEY: "mock-aws-access-key",
31+
EMAIL_AWS_SECRET_ACCESS_KEY: "mock-aws-secret-access-key",
32+
};
33+
});
34+
35+
afterEach(() => {
36+
process.env = originalEnv;
37+
mockSendMail.mockReset();
38+
});
39+
40+
const { sendEmail } = await import("./email");
41+
42+
describe("Email Helper", () => {
43+
const MockEmailTemplate = () => <>Hello</>;
44+
const options = {
45+
from: "from@example.com",
46+
to: "to@example.com",
47+
subject: "Test",
48+
};
49+
describe("sendEmail", () => {
50+
it("should send email with correct arguments", async () => {
51+
await sendEmail({
52+
template: <MockEmailTemplate />,
53+
options: options,
54+
});
55+
56+
expect(mockSendMail).toHaveBeenCalledTimes(1);
57+
});
58+
});
59+
});

apps/contact/tests/api-route.test.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it, expect, beforeEach, mock, vi } from "bun:test";
1+
import { describe, it, expect, beforeEach, mock } from "bun:test";
22
import { POST, OPTIONS } from "../app/api/contact/route";
33

44
const mockCreateContact = mock(() =>
@@ -70,10 +70,6 @@ describe("Contact API Route", () => {
7070
body: contentType === "application/json" ? JSON.stringify(body) : body,
7171
});
7272

73-
(request as any).nextUrl = {
74-
searchParams: new URLSearchParams(new URL(url).search),
75-
};
76-
7773
return request as any;
7874
};
7975

0 commit comments

Comments
 (0)