-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡️ Prefix with
[TEST]
in email subject if not in prod. mode
- Loading branch information
1 parent
6b899e9
commit a92d248
Showing
4 changed files
with
110 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { EmailerOutput } from "../../src/outputs/emailer/emailer.output"; | ||
|
||
import nodemailer from "nodemailer"; | ||
|
||
describe("EmailerOutput", () => { | ||
jest.mock("nodemailer"); | ||
nodemailer.createTransport = jest.fn().mockReturnValue({ | ||
sendMail: jest.fn().mockImplementation((mailOptions, callback) => { | ||
callback(null, "any"); | ||
}), | ||
}); | ||
|
||
let emailer: EmailerOutput; | ||
|
||
beforeEach(() => { | ||
emailer = new EmailerOutput({ | ||
host: "smtp.example.com", | ||
port: 587, | ||
secure: false, | ||
auth: { | ||
user: "[email protected]", | ||
pass: "password", | ||
}, | ||
tls: { | ||
rejectUnauthorized: false, | ||
}, | ||
}); | ||
}); | ||
|
||
test(`given NODE_ENV is set to "production" and standard mail options, | ||
when sendEmail is called, | ||
then should send an email with the given options`, async () => { | ||
// given | ||
const mailOptions = { | ||
// eslint-disable-next-line @typescript-eslint/quotes | ||
from: '"Test" <[email protected]>', | ||
to: ["[email protected]"], | ||
subject: "Hello", | ||
text: "Hello world!", | ||
html: "<b>Hello world!</b>", | ||
sender: "Tester", | ||
}; | ||
|
||
const mailOptionsCloned = JSON.parse(JSON.stringify(mailOptions)); | ||
|
||
// when | ||
process.env.NODE_ENV = "production"; | ||
|
||
const spyParameter = jest.spyOn(emailer, "sendEmail"); | ||
await emailer.sendEmail(mailOptions); | ||
|
||
// then | ||
expect(spyParameter.mock.calls.length).toEqual(1); | ||
const firstCallParameter = spyParameter.mock.calls[0][0]; | ||
|
||
expect(firstCallParameter).toBeDefined(); | ||
expect(firstCallParameter).toEqual(mailOptionsCloned); | ||
}); | ||
|
||
test(`given NODE_ENV is set to "development" and standard mail options, | ||
when sendEmail is called, | ||
then should send an email with the given options and subject prefixed by (TEST)`, async () => { | ||
// given | ||
const mailOptions = { | ||
// eslint-disable-next-line @typescript-eslint/quotes | ||
from: '"Test" <[email protected]>', | ||
to: ["[email protected]"], | ||
subject: "Hello", | ||
text: "Hello world!", | ||
html: "<b>Hello world!</b>", | ||
sender: "Tester", | ||
}; | ||
|
||
const mailOptionsCloned = JSON.parse(JSON.stringify(mailOptions)); | ||
|
||
// when | ||
process.env.NODE_ENV = "development"; | ||
|
||
const spyParameter = jest.spyOn(emailer, "sendEmail"); | ||
await emailer.sendEmail(mailOptions); | ||
|
||
// then | ||
expect(spyParameter.mock.calls.length).toEqual(1); | ||
|
||
const firstCallParameter = spyParameter.mock.calls[0][0]; | ||
|
||
expect(firstCallParameter).toBeDefined(); | ||
expect(firstCallParameter).not.toEqual(mailOptionsCloned); | ||
expect(firstCallParameter.subject).toEqual(`(TEST) ${mailOptionsCloned.subject}`); | ||
}); | ||
}); |