-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmailer.unit.tests.js
More file actions
170 lines (146 loc) · 5.04 KB
/
Copy pathmailer.unit.tests.js
File metadata and controls
170 lines (146 loc) · 5.04 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* Module dependencies.
*/
import { jest, describe, test, expect, beforeEach } from '@jest/globals';
// Mock config, files, and providers before importing mailer
jest.unstable_mockModule('../../../../config/index.js', () => ({
default: {
mailer: {
provider: 'resend',
from: 'test@example.com',
options: { apiKey: 're_test_123' },
},
},
}));
jest.unstable_mockModule('../../files.js', () => ({
default: {
readFile: jest.fn().mockResolvedValue('<p>{{name}}</p>'),
},
}));
const mockSend = jest.fn().mockResolvedValue({ id: 'email_123', accepted: ['user@example.com'], rejected: [] });
jest.unstable_mockModule('../provider.resend.js', () => ({
default: jest.fn().mockImplementation(() => ({ send: mockSend })),
}));
jest.unstable_mockModule('../provider.nodemailer.js', () => ({
default: jest.fn().mockImplementation(() => ({ send: jest.fn() })),
}));
const { default: mailer } = await import('../index.js');
describe('mailer index with resend provider unit tests:', () => {
beforeEach(() => {
jest.clearAllMocks();
});
test('should report as configured when from is set', () => {
expect(mailer.isConfigured()).toBe(true);
});
test('should send mail using the resend provider and normalize response', async () => {
mockSend.mockResolvedValue({ id: 'email_456' });
const result = await mailer.sendMail({
to: 'user@example.com',
subject: 'Welcome',
template: 'welcome',
params: { name: 'Alice' },
});
expect(mockSend).toHaveBeenCalledWith(
expect.objectContaining({
from: 'test@example.com',
to: 'user@example.com',
subject: 'Welcome',
html: '<p>Alice</p>',
}),
);
expect(result).toEqual({ id: 'email_456', accepted: ['user@example.com'], rejected: [] });
});
test('should pass through response unchanged when accepted is already an array', async () => {
mockSend.mockResolvedValue({ id: 'email_789', accepted: ['user@example.com'], rejected: [] });
const result = await mailer.sendMail({
to: 'user@example.com',
subject: 'Welcome',
template: 'welcome',
params: { name: 'Charlie' },
});
expect(result).toEqual({ id: 'email_789', accepted: ['user@example.com'], rejected: [] });
});
test('should forward attachments to the provider', async () => {
mockSend.mockResolvedValue({ id: 'email_attach', accepted: ['user@example.com'], rejected: [] });
const attachments = [{ filename: 'report.csv', content: 'a,b\n1,2' }];
await mailer.sendMail({
to: 'user@example.com',
subject: 'Report',
template: 'welcome',
params: { name: 'Carol' },
attachments,
});
expect(mockSend).toHaveBeenCalledWith(
expect.objectContaining({
attachments,
}),
);
});
test('should return null on send error', async () => {
mockSend.mockRejectedValue(new Error('API failure'));
const result = await mailer.sendMail({
to: 'user@example.com',
subject: 'Test',
template: 'welcome',
params: { name: 'Bob' },
});
expect(result).toBeNull();
});
test('should throw when attachment is missing content', async () => {
await expect(
mailer.sendMail({
to: 'user@example.com',
subject: 'Test',
template: 'welcome',
params: { name: 'Bob' },
attachments: [{ filename: 'file.txt' }],
}),
).rejects.toThrow('Attachment content is required');
});
test('should throw when attachment exceeds 25 MB', async () => {
const largeContent = Buffer.alloc(26 * 1024 * 1024); // 26 MB
await expect(
mailer.sendMail({
to: 'user@example.com',
subject: 'Test',
template: 'welcome',
params: { name: 'Bob' },
attachments: [{ filename: 'large.bin', content: largeContent }],
}),
).rejects.toThrow('exceeds 25 MB limit');
});
test('should throw when attachment filename is an empty string', async () => {
await expect(
mailer.sendMail({
to: 'user@example.com',
subject: 'Test',
template: 'welcome',
params: { name: 'Bob' },
attachments: [{ filename: '', content: 'data' }],
}),
).rejects.toThrow('Attachment filename must be a non-empty string');
});
test('should throw when attachment filename is not a string', async () => {
await expect(
mailer.sendMail({
to: 'user@example.com',
subject: 'Test',
template: 'welcome',
params: { name: 'Bob' },
attachments: [{ filename: 123, content: 'data' }],
}),
).rejects.toThrow('Attachment filename must be a non-empty string');
});
test('should not throw for a valid attachment', async () => {
mockSend.mockResolvedValue({ id: 'ok', accepted: ['user@example.com'], rejected: [] });
await expect(
mailer.sendMail({
to: 'user@example.com',
subject: 'Test',
template: 'welcome',
params: { name: 'Bob' },
attachments: [{ filename: 'doc.pdf', content: 'PDF content here' }],
}),
).resolves.not.toThrow();
});
});