Skip to content

Commit

Permalink
chore: generalise test cases, use snapshot testing
Browse files Browse the repository at this point in the history
  • Loading branch information
KenLSM committed Oct 24, 2024
1 parent 661185a commit c7e3cda
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`mail.service sendBounceNotification should send permanent bounce notification successfully 1`] = `
[MockFunction] {
"calls": [
[
{
"from": "mockApp <[email protected]>",
"headers": {
"X-Formsg-Email-Type": "Admin (bounce notification)",
"X-Formsg-Form-ID": "mockFormId",
},
"html": "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html dir="ltr" lang="en"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/></head><body><p style="font-size:14px;line-height:24px;margin:16px 0">Dear form admins(s),</p><p style="font-size:14px;line-height:24px;margin:16px 0">We’re reaching out urgently regarding your FormSG form You are all individuals!(mockApp.example.com/mockFormId). Responses to the following recipient(s) could not be delivered: [email protected], [email protected]. This was likely due to their mailbox being full.</p><p style="font-size:14px;line-height:24px;margin:16px 0">Please refer to our <a href="https://go.gov.sg/formsg-guide-bounces" style="color:#067df7;text-decoration:none" target="_blank">guide</a> for next steps on how to resolve this issue.</p><p style="font-size:14px;line-height:24px;margin:16px 0">The mockApp Support Team</p></body></html>",
"subject": "[Urgent] FormSG Response Delivery Failure / Bounce",
"to": [
"[email protected]",
"[email protected]",
],
},
],
],
"results": [
{
"type": "return",
"value": Promise {},
},
],
}
`;

exports[`mail.service sendBounceNotification should send transient bounce notification successfully 1`] = `
[MockFunction] {
"calls": [
[
{
"from": "mockApp <[email protected]>",
"headers": {
"X-Formsg-Email-Type": "Admin (bounce notification)",
"X-Formsg-Form-ID": "mockFormId",
},
"html": "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html dir="ltr" lang="en"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/></head><body><p style="font-size:14px;line-height:24px;margin:16px 0">Dear form admins(s),</p><p style="font-size:14px;line-height:24px;margin:16px 0">We’re reaching out urgently regarding your FormSG form You are all individuals!(mockApp.example.com/mockFormId). Responses to the following recipient(s) could not be delivered: [email protected], [email protected]. This was likely due to their mailbox being full.</p><p style="font-size:14px;line-height:24px;margin:16px 0">Please refer to our <a href="https://go.gov.sg/formsg-guide-bounces" style="color:#067df7;text-decoration:none" target="_blank">guide</a> for next steps on how to resolve this issue.</p><p style="font-size:14px;line-height:24px;margin:16px 0">The mockApp Support Team</p></body></html>",
"subject": "[Urgent] FormSG Response Delivery Failure / Bounce",
"to": [
"[email protected]",
"[email protected]",
],
},
],
],
"results": [
{
"type": "return",
"value": Promise {},
},
],
}
`;
46 changes: 18 additions & 28 deletions src/app/services/mail/__tests__/mail.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1186,30 +1186,6 @@ describe('mail.service', () => {
const MOCK_FORM_TITLE = 'You are all individuals!'
const MOCK_BOUNCE_TYPE = BounceType.Permanent

const generateExpectedArg = async (bounceType: BounceType) => {
return {
to: MOCK_RECIPIENTS,
from: MOCK_SENDER_STRING,
subject: `[Urgent] FormSG Response Delivery Failure / Bounce`,
html: (
await MailUtils.generateBounceNotificationHtml(
{
appName: MOCK_APP_NAME,
bouncedRecipients: MOCK_BOUNCED_EMAILS.join(', '),
formLink: `${MOCK_APP_URL}/${MOCK_FORM_ID}`,
formTitle: MOCK_FORM_TITLE,
},
bounceType,
)
)._unsafeUnwrap(),
headers: {
// Hardcode in tests in case something changes this.
'X-Formsg-Email-Type': 'Admin (bounce notification)',
'X-Formsg-Form-ID': MOCK_FORM_ID,
},
}
}

it('should send permanent bounce notification successfully', async () => {
// Arrange
// sendMail should return mocked success response
Expand All @@ -1223,12 +1199,19 @@ describe('mail.service', () => {
formId: MOCK_FORM_ID,
formTitle: MOCK_FORM_TITLE,
})
const expectedArgs = await generateExpectedArg(BounceType.Permanent)
// Assert
expect(actualResult._unsafeUnwrap()).toEqual(true)
// Check arguments passed to sendNodeMail
expect(sendMailSpy).toHaveBeenCalledTimes(1)
expect(sendMailSpy).toHaveBeenCalledWith(expectedArgs)
expect(sendMailSpy).toHaveBeenCalledWith(
expect.objectContaining({
to: MOCK_RECIPIENTS,
from: MOCK_SENDER_STRING,
subject: `[Urgent] FormSG Response Delivery Failure / Bounce`,
html: expect.stringMatching(MOCK_FORM_ID),
}),
)
expect(sendMailSpy).toMatchSnapshot()
})

it('should send transient bounce notification successfully', async () => {
Expand All @@ -1244,12 +1227,19 @@ describe('mail.service', () => {
formId: MOCK_FORM_ID,
formTitle: MOCK_FORM_TITLE,
})
const expectedArgs = await generateExpectedArg(BounceType.Transient)
// Assert
expect(actualResult._unsafeUnwrap()).toEqual(true)
// Check arguments passed to sendNodeMail
expect(sendMailSpy).toHaveBeenCalledTimes(1)
expect(sendMailSpy).toHaveBeenCalledWith(expectedArgs)
expect(sendMailSpy).toHaveBeenCalledWith(
expect.objectContaining({
to: MOCK_RECIPIENTS,
from: MOCK_SENDER_STRING,
subject: `[Urgent] FormSG Response Delivery Failure / Bounce`,
html: expect.stringMatching(MOCK_FORM_ID),
}),
)
expect(sendMailSpy).toMatchSnapshot()
})

it('should reject with error when email is invalid', async () => {
Expand Down

0 comments on commit c7e3cda

Please sign in to comment.