Skip to content

Commit

Permalink
test(emailService): add error handling tests for sendAlert function
Browse files Browse the repository at this point in the history
  • Loading branch information
SaiKumar2121 committed Nov 4, 2024
1 parent be7ffe7 commit dc7f254
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions tests/utils/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,4 +716,108 @@ describe('EmailService.sendAlert', () => {
}));
expect(result).toBe(true); // Verify successful sending
});

it('should log error and return false when transporter initialization fails', async () => {
const mockStorageConnection = {
getConfig: jest.fn().mockResolvedValue(mockConfig)
};
getStorageConnection.mockReturnValue(mockStorageConnection);

// Mock nodemailer.createTransport to throw an error
nodemailer.createTransport.mockImplementation(() => {
throw new Error('Transporter initialization failed');
});

// Spy on console.error
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

const result = await EmailService.sendAlert('Test message', 'Test type', {});

expect(consoleErrorSpy).toHaveBeenCalledWith(
'Failed to create email transporter: ',
expect.any(Error)
);
expect(result).toBe(false);

// Restore the spy
consoleErrorSpy.mockRestore();
});

// it('should log error and return false when sending email fails', async () => {
// const mockStorageConnection = {
// getConfig: jest.fn().mockResolvedValue(mockConfig)
// };
// getStorageConnection.mockReturnValue(mockStorageConnection);

// // Mock transporter and sendMail to reject
// const mockTransporter = {
// sendMail: jest.fn().mockRejectedValue(new Error('Email send failed'))
// };
// nodemailer.createTransport.mockReturnValue(mockTransporter);

// // Spy on console.error
// const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

// const result = await EmailService.sendAlert('Test message', 'Test type', {});

// expect(mockTransporter.sendMail).toHaveBeenCalled();
// expect(consoleErrorSpy).toHaveBeenCalledWith(
// 'Failed to send email alert:',
// expect.any(Error)
// );
// expect(result).toBe(false);

// // Restore the spy
// consoleErrorSpy.mockRestore();
// });

it('should log error and return false when retrieving configuration fails', async () => {
const mockStorageConnection = {
getConfig: jest.fn().mockRejectedValue(new Error('Configuration retrieval failed'))
};
getStorageConnection.mockReturnValue(mockStorageConnection);

// Spy on console.error
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

const result = await EmailService.sendAlert('Test message', 'Test type', {});

expect(mockStorageConnection.getConfig).toHaveBeenCalledWith('emailIntegration');
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Failed to create email transporter: ',
expect.any(Error)
);
expect(result).toBe(false);

// Restore the spy
consoleErrorSpy.mockRestore();
});

it('should log unexpected errors and return false', async () => {
const mockStorageConnection = {
getConfig: jest.fn().mockResolvedValue(mockConfig)
};
getStorageConnection.mockReturnValue(mockStorageConnection);

// Mock transporter and sendMail to throw a non-standard error
const mockTransporter = {
sendMail: jest.fn(() => { throw new Error('Unexpected error'); })
};
nodemailer.createTransport.mockReturnValue(mockTransporter);

// Spy on console.error
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

const result = await EmailService.sendAlert('Test message', 'Test type', {});

expect(mockTransporter.sendMail).toHaveBeenCalled();
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Failed to send email alert:',
expect.any(Error)
);
expect(result).toBe(false);

// Restore the spy
consoleErrorSpy.mockRestore();
});
});

0 comments on commit dc7f254

Please sign in to comment.