forked from sendgrid/sendgrid-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.spec.js
151 lines (135 loc) · 3.68 KB
/
mail.spec.js
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
'use strict';
/**
* Dependencies
*/
const sgMail = require('./mail');
const sgClient = sgMail.client;
/**
* Setup client
*/
before(() => {
sgClient.setApiKey('SendGrid API Key');
});
/**
* Default mock header
*/
beforeEach(() => {
sgClient.setDefaultHeader('X-Mock', 202);
});
/**
* Tests
*/
describe('sgMail.send()', () => {
//Create mail data
const data = {
to: 'recipient@example.org',
from: 'sender@example.org',
subject: 'Hello world',
text: 'Hello plain world!',
html: '<p>Hello HTML world!</p>',
};
it('should throw an error when no data provided', () => {
return expect(sgMail.send()).to.eventually.be.rejectedWith(Error);
});
it('should send a basic email', () => {
sgClient.setDefaultHeader('X-Mock', 202);
return sgMail
.send(data)
.then(([response, body]) => {
expect(response.statusCode).to.equal(202);
});
});
it('should not be null(error) in callBack function for unsuccessfully delivered emails', (done) => {
sgMail.send({}, false, (error, response) => {
expect(error).to.not.be.null();
done();
});
});
it('should be null(error) in callBack function for successfully delivered emails', (done) => {
sgClient.setDefaultHeader('X-Mock', 202);
sgMail.send(data, false, (error, response) => {
expect(error).to.be.null();
done();
});
});
it('should throw an error if callback is not a function', () => {
return expect(function() {
sgMail.send(data, false, {});
}).to.throw(Error);
});
it('should include custom headers to the request', () => {
sgClient.setDefaultHeader('X-Mock', 202);
const clientSpy = sinon.spy(sgClient, "request")
return sgMail
.send(Object.assign(data, { headers: { customHeader: "Custom Header Content" } }))
.then(([response, body]) => {
expect(response.statusCode).to.equal(202);
expect(clientSpy).to.have.been.calledWith(sinon.match({
url: "/v3/mail/send",
method: "POST",
headers: { customHeader: "Custom Header Content" }
}));
});
});
it('should send email with correct replyToList format', () => {
sgClient.setDefaultHeader('X-Mock', 202);
data["replyToList"] = [
{
"name": "Test Team",
"email": "test@example.org"
},
{
"name": "Support Test Team",
"email": "support.test@example.org"
}
];
return sgMail
.send(data)
.then(([response, body]) => {
expect(response.statusCode).to.equal(202);
});
});
it('should throw error with wrong replyToList format', () => {
sgClient.setDefaultHeader('X-Mock', 202);
data["replyToList"] = {
"name": "Support Test Team",
"email": "support.test@example.org"
};
return expect(function() {
sgMail.send(data, false, {});
}).to.throw(Error);
});
it('should throw error if any record in replyToList is without email', () => {
data["replyToList"] = [
{
"name": "Test Team",
"email": "test@example.org"
},
{
"name": "Support Test Team"
}
];
return expect(function() {
sgMail.send(data, false, {});
}).to.throw(Error);
});
it('should throw error if both replyTo and replyToList are mentioned', () => {
data["replyTo"] = {
"name": "Manual Tester",
"email": "manual.test@example.org"
};
data["replyToList"] = [
{
"name": "Test Team",
"email": "test@example.org"
},
{
"name": "Support Test Team",
"email": "support.test@example.org"
}
];
return expect(function() {
sgMail.send(data, false, {});
}).to.throw(Error);
});
});