|
| 1 | +import { mockDeep } from 'jest-mock-extended'; |
| 2 | +import { SESClient, SendRawEmailCommand } from '@aws-sdk/client-ses'; |
| 3 | +import { Logger } from 'nhs-notify-web-template-management-utils/logger'; |
| 4 | +import { EmailClient } from '../../../templates/infra/email-client'; |
| 5 | +import { TemplateDto } from 'nhs-notify-backend-client'; |
| 6 | + |
| 7 | +jest.mock('node:fs', () => ({ |
| 8 | + readFileSync: jest.fn(() => '<html>{{templateId}}</html>'), |
| 9 | +})); |
| 10 | + |
| 11 | +jest.mock('mimetext', () => ({ |
| 12 | + createMimeMessage: () => { |
| 13 | + const msg = { |
| 14 | + setSender: jest.fn(), |
| 15 | + setTo: jest.fn(), |
| 16 | + setSubject: jest.fn(), |
| 17 | + addMessage: jest.fn(), |
| 18 | + asRaw: jest.fn(() => 'raw-email-content'), |
| 19 | + }; |
| 20 | + return msg; |
| 21 | + }, |
| 22 | +})); |
| 23 | + |
| 24 | +describe('EmailClient', () => { |
| 25 | + const recipientEmails = { |
| 26 | + |
| 27 | + |
| 28 | + }; |
| 29 | + |
| 30 | + const sesClient = mockDeep<SESClient>(); |
| 31 | + const logger = mockDeep<Logger>(); |
| 32 | + |
| 33 | + const mockTemplate = mockDeep<TemplateDto>({ |
| 34 | + id: 'template-id', |
| 35 | + templateType: 'LETTER', |
| 36 | + files: { |
| 37 | + proofs: { |
| 38 | + proof1: { fileName: 'proof1.pdf', supplier: 'supplier1' }, |
| 39 | + proof2: { fileName: 'proof2.pdf', supplier: 'supplier2' }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + updatedAt: '2022-01-01T00:00:00Z', |
| 43 | + name: 'Test Template', |
| 44 | + }); |
| 45 | + |
| 46 | + beforeEach(() => { |
| 47 | + jest.clearAllMocks(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('does not send email when no email address provided', async () => { |
| 51 | + const client = new EmailClient(sesClient, '', recipientEmails, logger); |
| 52 | + |
| 53 | + await client.sendTemplateSubmittedEmailToSuppliers(mockTemplate); |
| 54 | + |
| 55 | + expect(sesClient.send).not.toHaveBeenCalled(); |
| 56 | + expect(logger.info).toHaveBeenCalledWith({ |
| 57 | + description: |
| 58 | + 'Not sending template submitted email to suppliers because no email address is provided', |
| 59 | + templateId: mockTemplate.id, |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('does not send email if templateType is not LETTER', async () => { |
| 64 | + const client = new EmailClient( |
| 65 | + sesClient, |
| 66 | + |
| 67 | + recipientEmails, |
| 68 | + logger |
| 69 | + ); |
| 70 | + const template = mockDeep<TemplateDto>({ |
| 71 | + ...mockTemplate, |
| 72 | + templateType: 'NHS_APP', |
| 73 | + }); |
| 74 | + |
| 75 | + await client.sendTemplateSubmittedEmailToSuppliers(template); |
| 76 | + |
| 77 | + expect(sesClient.send).not.toHaveBeenCalled(); |
| 78 | + expect(logger.info).toHaveBeenCalledWith({ |
| 79 | + description: |
| 80 | + 'Not sending template submitted email to suppliers because templateType is not LETTER', |
| 81 | + templateId: template.id, |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('sends no email if there are no proofs on the template', async () => { |
| 86 | + const client = new EmailClient( |
| 87 | + sesClient, |
| 88 | + |
| 89 | + recipientEmails, |
| 90 | + logger |
| 91 | + ); |
| 92 | + const template = mockDeep<TemplateDto>({ |
| 93 | + id: 'template-id', |
| 94 | + templateType: 'LETTER', |
| 95 | + files: { |
| 96 | + proofs: undefined, |
| 97 | + }, |
| 98 | + updatedAt: '2022-01-01T00:00:00Z', |
| 99 | + name: 'Test Template', |
| 100 | + }); |
| 101 | + |
| 102 | + await client.sendTemplateSubmittedEmailToSuppliers(template); |
| 103 | + |
| 104 | + expect(sesClient.send).not.toHaveBeenCalled(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('sends email to multiple suppliers', async () => { |
| 108 | + const client = new EmailClient( |
| 109 | + sesClient, |
| 110 | + |
| 111 | + recipientEmails, |
| 112 | + logger |
| 113 | + ); |
| 114 | + |
| 115 | + await client.sendTemplateSubmittedEmailToSuppliers(mockTemplate); |
| 116 | + |
| 117 | + expect(sesClient.send).toHaveBeenCalledTimes(2); |
| 118 | + expect(sesClient.send).toHaveBeenCalledWith( |
| 119 | + expect.any(SendRawEmailCommand) |
| 120 | + ); |
| 121 | + expect(logger.info).toHaveBeenCalled(); |
| 122 | + }); |
| 123 | +}); |
0 commit comments