|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { EmailsService } from './emails.service'; |
| 3 | +import { AMAZON_SES_WRAPPER } from './amazon-ses.wrapper'; |
| 4 | + |
| 5 | +describe('EmailsService', () => { |
| 6 | + let service: EmailsService; |
| 7 | + let mockAmazonSESWrapper: any; |
| 8 | + let loggerErrorSpy: jest.SpyInstance; |
| 9 | + |
| 10 | + beforeEach(async () => { |
| 11 | + mockAmazonSESWrapper = { |
| 12 | + sendEmail: jest.fn(), |
| 13 | + }; |
| 14 | + |
| 15 | + const module: TestingModule = await Test.createTestingModule({ |
| 16 | + providers: [ |
| 17 | + EmailsService, |
| 18 | + { |
| 19 | + provide: AMAZON_SES_WRAPPER, |
| 20 | + useValue: mockAmazonSESWrapper, |
| 21 | + }, |
| 22 | + ], |
| 23 | + }).compile(); |
| 24 | + service = module.get<EmailsService>(EmailsService); |
| 25 | + loggerErrorSpy = jest.spyOn(service['logger'], 'error'); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + jest.clearAllMocks(); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('sendEmail', () => { |
| 33 | + const recipientEmail = 'user@example.com'; |
| 34 | + const subject = 'Test Email Subject'; |
| 35 | + const bodyHTML = '<h1>Test Email</h1><p>This is a test email body.</p>'; |
| 36 | + |
| 37 | + it('should be defined', () => { |
| 38 | + expect(service).toBeDefined(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should send email successfully with valid parameters', async () => { |
| 42 | + const expectedResponse = { MessageId: 'test-message-id-123' }; |
| 43 | + mockAmazonSESWrapper.sendEmail.mockResolvedValue(expectedResponse); |
| 44 | + |
| 45 | + const result = await service.sendEmail(recipientEmail, subject, bodyHTML); |
| 46 | + |
| 47 | + expect(mockAmazonSESWrapper.sendEmail).toHaveBeenCalledTimes(1); |
| 48 | + expect(mockAmazonSESWrapper.sendEmail).toHaveBeenCalledWith( |
| 49 | + [recipientEmail], |
| 50 | + subject, |
| 51 | + bodyHTML, |
| 52 | + ); |
| 53 | + expect(result).toEqual(expectedResponse); |
| 54 | + expect(loggerErrorSpy).not.toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should throw an error and pass on information with no loss if the SESWrapper throws', async () => { |
| 58 | + mockAmazonSESWrapper.sendEmail.mockRejectedValueOnce( |
| 59 | + new Error('Error in sending email.'), |
| 60 | + ); |
| 61 | + await expect( |
| 62 | + service.sendEmail('recipient@email.com', 'Subject', '<h1>body</h1>'), |
| 63 | + ).rejects.toThrow('Error in sending email.'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should propagate the exact error thrown by wrapper', async () => { |
| 67 | + const customError = new Error('Custom error message'); |
| 68 | + customError.name = 'CustomSESError'; |
| 69 | + (customError as any).code = 'CUSTOM_CODE'; |
| 70 | + mockAmazonSESWrapper.sendEmail.mockRejectedValue(customError); |
| 71 | + |
| 72 | + await expect( |
| 73 | + service.sendEmail(recipientEmail, subject, bodyHTML), |
| 74 | + ).rejects.toThrow(customError); |
| 75 | + |
| 76 | + try { |
| 77 | + await service.sendEmail(recipientEmail, subject, bodyHTML); |
| 78 | + fail('Should have thrown an error'); |
| 79 | + } catch (thrownError) { |
| 80 | + expect(thrownError).toBeInstanceOf(Error); |
| 81 | + expect((thrownError as Error).name).toBe('CustomSESError'); |
| 82 | + expect((thrownError as any).code).toBe('CUSTOM_CODE'); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + it('should handle concurrent email sending calls', async () => { |
| 87 | + mockAmazonSESWrapper.sendEmail.mockResolvedValue({ MessageId: 'test' }); |
| 88 | + const promises = [ |
| 89 | + service.sendEmail('user1@example.com', 'Subject 1', '<p>Body 1</p>'), |
| 90 | + service.sendEmail('user2@example.com', 'Subject 2', '<p>Body 2</p>'), |
| 91 | + service.sendEmail('user3@example.com', 'Subject 3', '<p>Body 3</p>'), |
| 92 | + ]; |
| 93 | + |
| 94 | + await Promise.all(promises); |
| 95 | + expect(mockAmazonSESWrapper.sendEmail).toHaveBeenCalledTimes(3); |
| 96 | + expect(mockAmazonSESWrapper.sendEmail).toHaveBeenNthCalledWith( |
| 97 | + 1, |
| 98 | + ['user1@example.com'], |
| 99 | + 'Subject 1', |
| 100 | + '<p>Body 1</p>', |
| 101 | + ); |
| 102 | + expect(mockAmazonSESWrapper.sendEmail).toHaveBeenNthCalledWith( |
| 103 | + 2, |
| 104 | + ['user2@example.com'], |
| 105 | + 'Subject 2', |
| 106 | + '<p>Body 2</p>', |
| 107 | + ); |
| 108 | + expect(mockAmazonSESWrapper.sendEmail).toHaveBeenNthCalledWith( |
| 109 | + 3, |
| 110 | + ['user3@example.com'], |
| 111 | + 'Subject 3', |
| 112 | + '<p>Body 3</p>', |
| 113 | + ); |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments