Skip to content

Commit af52b8f

Browse files
committed
added tests and fixed docs
1 parent 1ed3445 commit af52b8f

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

spec/ApiMailAdapter.spec.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,51 @@ describe('ApiMailAdapter', () => {
219219
},
220220
extra: {
221221
field: "ExampleExtra"
222-
}
222+
},
223+
user: undefined,
223224
};
224225
const expectedArguments = Object.assign({}, options, { direct: true });
225226

226227
await expectAsync(adapter.sendMail(options)).toBeResolved();
227228
expect(_sendMail.calls.all()[0].args[0]).toEqual(expectedArguments);
228229
});
230+
231+
it('passes user to callback when user is passed to sendMail()', async () => {
232+
const adapter = new ApiMailAdapter(config);
233+
const localeCallbackSpy = spyOn(config.templates.customEmailWithLocaleCallback, 'localeCallback').and.callThrough();
234+
const options = {
235+
templateName: 'customEmailWithLocaleCallback',
236+
user: new Parse.User(),
237+
};
238+
239+
await expectAsync(adapter.sendMail(options)).toBeResolved();
240+
expect(localeCallbackSpy.calls.all()[0].args[0].get('locale')).toBe(options.user.get('locale'));
241+
});
242+
243+
it('uses user email if no recipient is passed to sendMail()', async () => {
244+
const adapter = new ApiMailAdapter(config);
245+
const apiCallbackSpy = spyOn(adapter, 'apiCallback').and.callThrough();
246+
const options = {
247+
templateName: 'customEmailWithLocaleCallback',
248+
user: new Parse.User(),
249+
};
250+
251+
await expectAsync(adapter.sendMail(options)).toBeResolved();
252+
expect(apiCallbackSpy.calls.all()[0].args[0].payload.to).toBe(options.user.get('email'));
253+
});
254+
255+
it('overrides user email if recipient is passed to sendMail()', async () => {
256+
const adapter = new ApiMailAdapter(config);
257+
const apiCallbackSpy = spyOn(adapter, 'apiCallback').and.callThrough();
258+
const options = {
259+
recipient: '[email protected]',
260+
templateName: 'customEmailWithLocaleCallback',
261+
user: new Parse.User(),
262+
};
263+
264+
await expectAsync(adapter.sendMail(options)).toBeResolved();
265+
expect(apiCallbackSpy.calls.all()[0].args[0].payload.to).toBe(options.recipient);
266+
});
229267
});
230268

231269
describe('generate API payload', function () {

spec/helper.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
'use strict';
2-
31
// Simulate Parse User class
42
const Parse = {
53
User: class User {
64
get(key) {
75
switch (key) {
86
case 'username':
9-
return 'ExampleUsername'
7+
return 'ExampleUsername';
108
case 'email':
11-
9+
10+
case 'locale':
11+
return 'de-AT';
1212
}
1313
}
1414
}

src/ApiMailAdapter.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,16 @@ class ApiMailAdapter extends MailAdapter {
8383

8484
/**
8585
* @function sendMail
86-
* @desciption Sends an email.
86+
* @description Sends an email.
8787
* @param {String} [sender] The email from address.
88-
* @param {String} recipient The email recipient.
88+
* @param {String} recipient The email recipient; if set overrides the email address of the `user`.
8989
* @param {String} [subject] The email subject.
9090
* @param {String} [text] The plain-text email content.
9191
* @param {String} [html] The HTML email content.
9292
* @param {String} [templateName] The template name.
93-
* @param {String} [placeholders] The template placeholders.
94-
* @param {String} [extra] Any additional variables to pass to the mail provider API.
93+
* @param {Object} [placeholders] The template placeholders.
94+
* @param {Object} [extra] Any additional variables to pass to the mail provider API.
95+
* @param {Parse.User} [user] The Parse User that the is the recipient of the email.
9596
* @returns {Promise<Any>} The mail provider API response.
9697
*/
9798
async sendMail({ sender, recipient, subject, text, html, templateName, placeholders, extra, user }) {
@@ -119,9 +120,9 @@ class ApiMailAdapter extends MailAdapter {
119120

120121
// Define parameters
121122
let message;
122-
const templateName = email.templateName;
123-
// Get user from email object
124123
const user = email.user;
124+
const userEmail = user ? user.get('email') : undefined;
125+
const templateName = email.templateName;
125126

126127
// If template name is not set
127128
if (!templateName) {
@@ -147,7 +148,7 @@ class ApiMailAdapter extends MailAdapter {
147148
if (email.direct) {
148149

149150
// If recipient is not set
150-
if (!email.recipient) {
151+
if (!email.recipient && !userEmail) {
151152
throw Errors.Error.noRecipient;
152153
}
153154

@@ -158,7 +159,7 @@ class ApiMailAdapter extends MailAdapter {
158159
message = Object.assign(
159160
{
160161
from: email.sender || this.sender,
161-
to: email.recipient,
162+
to: email.recipient || userEmail,
162163
subject: email.subject,
163164
text: email.text,
164165
html: email.html
@@ -174,14 +175,14 @@ class ApiMailAdapter extends MailAdapter {
174175
Object.assign(placeholders, {
175176
link,
176177
appName,
177-
email: user.get('email'),
178+
email: userEmail,
178179
username: user.get('username')
179180
});
180181

181182
// Set message properties
182183
message = {
183184
from: this.sender,
184-
to: user.get('email')
185+
to: userEmail
185186
};
186187
}
187188

0 commit comments

Comments
 (0)