From c7e3cda6bb92358845e3815e023bfcc9118367d4 Mon Sep 17 00:00:00 2001 From: Ken Date: Thu, 24 Oct 2024 20:05:15 +0800 Subject: [PATCH] chore: generalise test cases, use snapshot testing --- .../__snapshots__/mail.service.spec.ts.snap | 57 +++++++++++++++++++ .../mail/__tests__/mail.service.spec.ts | 46 ++++++--------- 2 files changed, 75 insertions(+), 28 deletions(-) create mode 100644 src/app/services/mail/__tests__/__snapshots__/mail.service.spec.ts.snap diff --git a/src/app/services/mail/__tests__/__snapshots__/mail.service.spec.ts.snap b/src/app/services/mail/__tests__/__snapshots__/mail.service.spec.ts.snap new file mode 100644 index 0000000000..c592cf95c5 --- /dev/null +++ b/src/app/services/mail/__tests__/__snapshots__/mail.service.spec.ts.snap @@ -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 ", + "headers": { + "X-Formsg-Email-Type": "Admin (bounce notification)", + "X-Formsg-Form-ID": "mockFormId", + }, + "html": "

Dear form admins(s),

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: to3@example.com, to4@example.com. This was likely due to their mailbox being full.

Please refer to our guide for next steps on how to resolve this issue.

The mockApp Support Team

", + "subject": "[Urgent] FormSG Response Delivery Failure / Bounce", + "to": [ + "to@example.com", + "to2@example.com", + ], + }, + ], + ], + "results": [ + { + "type": "return", + "value": Promise {}, + }, + ], +} +`; + +exports[`mail.service sendBounceNotification should send transient bounce notification successfully 1`] = ` +[MockFunction] { + "calls": [ + [ + { + "from": "mockApp ", + "headers": { + "X-Formsg-Email-Type": "Admin (bounce notification)", + "X-Formsg-Form-ID": "mockFormId", + }, + "html": "

Dear form admins(s),

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: to3@example.com, to4@example.com. This was likely due to their mailbox being full.

Please refer to our guide for next steps on how to resolve this issue.

The mockApp Support Team

", + "subject": "[Urgent] FormSG Response Delivery Failure / Bounce", + "to": [ + "to@example.com", + "to2@example.com", + ], + }, + ], + ], + "results": [ + { + "type": "return", + "value": Promise {}, + }, + ], +} +`; diff --git a/src/app/services/mail/__tests__/mail.service.spec.ts b/src/app/services/mail/__tests__/mail.service.spec.ts index 84016d7efd..7f2dfd709a 100644 --- a/src/app/services/mail/__tests__/mail.service.spec.ts +++ b/src/app/services/mail/__tests__/mail.service.spec.ts @@ -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 @@ -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 () => { @@ -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 () => {