This repository has been archived by the owner on Sep 7, 2024. It is now read-only.
generated from NuroDev/npm-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail.test.ts
104 lines (89 loc) · 2.8 KB
/
email.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { describe, it, expect, beforeAll } from "vitest";
import "dotenv/config";
import { bulkEmailStatus, sendEmail, sendBulkEmails } from ".";
const {
MAILERSEND_API_KEY,
MAILERSEND_TEST_RECIPIENT_EMAIL,
MAILERSEND_TEST_SENDER_EMAIL,
} = process.env as Record<string, string>;
describe("Email", () => {
let bulkEmailId: string;
beforeAll(() => {
if (!MAILERSEND_API_KEY)
throw "No MailerSend API key found in environment variables";
});
it.concurrent("Send Email", async () => {
if (!MAILERSEND_TEST_SENDER_EMAIL || !MAILERSEND_TEST_RECIPIENT_EMAIL)
throw "No or invalid test email addresses found in environment variables";
try {
const sendEmailResponse = await sendEmail(MAILERSEND_API_KEY, {
from: {
email: MAILERSEND_TEST_SENDER_EMAIL,
name: "TEST SENDER",
},
to: [
{
email: MAILERSEND_TEST_RECIPIENT_EMAIL,
name: "TEST RECIPIENT",
},
],
subject: `TEST EMAIL - ${new Date().toLocaleDateString("en-US", {
day: "2-digit",
month: "2-digit",
year: "numeric",
})}`,
html: `<h1>TEST EMAIL</h1>`,
text: "TEST EMAIL",
});
expect(sendEmailResponse).not.toBeNull();
expect(sendEmailResponse.success).toBeTruthy();
} catch (error) {
console.error(error);
throw error;
}
});
it("Send Bulk Emails", async () => {
if (!MAILERSEND_TEST_SENDER_EMAIL || !MAILERSEND_TEST_RECIPIENT_EMAIL)
throw "No or invalid test email addresses found in environment variables";
try {
const bulkEmailsResponse = await sendBulkEmails(MAILERSEND_API_KEY, [
{
from: {
email: MAILERSEND_TEST_SENDER_EMAIL,
name: "TEST SENDER",
},
to: [
{
email: MAILERSEND_TEST_RECIPIENT_EMAIL,
name: "TEST RECIPIENT",
},
],
subject: `BULK TEST EMAIL - ${Date.now()}`,
html: `<h1>BULK TEST EMAIL</h1>`,
text: "BULK TEST EMAIL",
},
]);
expect(bulkEmailsResponse).not.toBeNull();
expect(bulkEmailsResponse.bulk_email_id).toBeDefined();
expect(bulkEmailsResponse.message).toBeDefined();
bulkEmailId = bulkEmailsResponse.bulk_email_id;
} catch (error) {
console.error(error);
throw error;
}
});
it("Bulk Email Status", async () => {
if (!bulkEmailId) throw "No or invalid bulk email ID found";
try {
const bulkEmailStatusResponse = await bulkEmailStatus(
MAILERSEND_API_KEY,
bulkEmailId
);
expect(bulkEmailStatusResponse).toBeDefined();
expect(bulkEmailStatusResponse.data).toBeDefined();
} catch (error) {
console.error(error);
throw error;
}
});
});