Skip to content

Commit

Permalink
test(EmailService): update sendAlert tests with regex for HTML matching
Browse files Browse the repository at this point in the history
  • Loading branch information
SaiKumar2121 committed Oct 9, 2024
1 parent fc2ecee commit 0639af2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
13 changes: 8 additions & 5 deletions tests/utils/alert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { getStorageConnection } = require('../../lib/main/server/storageConnectio
const axios = require('axios');
const nodemailer = require('nodemailer');
const { EmailService, SlackService, testSlackAlert, testEmailAlert, clearEmailTransport, customLoggerAlert, handleUncaughtExceptions } = require('../../lib/main/server/utils/alerts'); // Adjust the path as needed
/* globals expect, jest, beforeEach, describe, beforeAll, afterAll, it, afterEach */
/* globals expect, jest, beforeEach, describe, beforeAll, afterAll, it, afterEach */

jest.mock('axios');
jest.mock('nodemailer');
Expand Down Expand Up @@ -110,11 +110,12 @@ describe('EmailService', () => {

const result = await EmailService.sendAlert('Test message', 'Test type', { appName: 'TestApp', environmentName: 'TestEnv' });

// Use a regular expression to match the 'html' content, allowing for flexible whitespace
expect(mockTransporter.sendMail).toHaveBeenCalledWith(expect.objectContaining({
from: '[email protected]',
to: '[email protected]',
subject: 'Errsole: Test type (TestApp app, TestEnv environment)',
html: '<p><b>App Name: TestApp\nEnvironment Name: TestEnv</b></p><br/><pre style="border: 1px solid #ccc; background-color: #f9f9f9; padding: 10px;">Test message</pre>'
html: expect.stringMatching(/<p><b>App Name:<\/b> TestApp<\/p>\s*<p><b>Environment Name:<\/b> TestEnv<\/p><br\/><pre style="border: 1px solid #ccc; background-color: #f9f9f9; padding: 10px;">Test message<\/pre>/)
}));
expect(result).toBe(true);
});
Expand Down Expand Up @@ -185,7 +186,7 @@ describe('EmailService', () => {
from: '[email protected]',
to: '[email protected]',
subject: 'Errsole: Test type (TestApp app, TestEnv environment)',
html: '<p><b>App Name: TestApp\nEnvironment Name: TestEnv</b></p><br/><pre style="border: 1px solid #ccc; background-color: #f9f9f9; padding: 10px;">Test message</pre>'
html: expect.stringMatching(/<p><b>App Name:<\/b> TestApp<\/p>\s*<p><b>Environment Name:<\/b> TestEnv<\/p><br\/><pre style="border: 1px solid #ccc; background-color: #f9f9f9; padding: 10px;">Test message<\/pre>/)
}));
});

Expand Down Expand Up @@ -217,7 +218,7 @@ describe('EmailService', () => {
from: '[email protected]',
to: '[email protected]',
subject: 'Errsole: Test type (TestApp app)',
html: '<p><b>App Name: TestApp</b></p><br/><pre style="border: 1px solid #ccc; background-color: #f9f9f9; padding: 10px;">Test message</pre>'
html: expect.stringMatching(/<p><b>App Name:<\/b> TestApp<\/p><br\/><pre style="border: 1px solid #ccc; background-color: #f9f9f9; padding: 10px;">Test message<\/pre>/)
}));
});

Expand Down Expand Up @@ -249,7 +250,7 @@ describe('EmailService', () => {
from: '[email protected]',
to: '[email protected]',
subject: 'Errsole: Test type (TestEnv environment)',
html: '<p><b>Environment Name: TestEnv</b></p><br/><pre style="border: 1px solid #ccc; background-color: #f9f9f9; padding: 10px;">Test message</pre>'
html: expect.stringMatching(/<p><b>Environment Name:<\/b> TestEnv<\/p><br\/><pre style="border: 1px solid #ccc; background-color: #f9f9f9; padding: 10px;">Test message<\/pre>/)
}));
});

Expand Down Expand Up @@ -379,6 +380,7 @@ describe('SlackService', () => {
});

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

expect(console.error).toHaveBeenCalledWith('Failed to send slack alert:', expect.any(Error));
expect(result).toBe(false);
});
Expand All @@ -390,6 +392,7 @@ describe('SlackService', () => {
mockStorageConnection.getConfig.mockResolvedValue(mockConfig);

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

expect(result).toBe(false);
});
});
Expand Down
17 changes: 12 additions & 5 deletions tests/utils/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,14 @@ describe('EmailService.sendAlert', () => {

const result = await EmailService.sendAlert('Test message', 'Test type', { appName: 'TestApp', environmentName: 'TestEnv' });

// Updated 'html' expectation using regex
expect(mockTransporter.sendMail).toHaveBeenCalledWith(expect.objectContaining({
from: '[email protected]',
to: '[email protected]',
subject: 'Errsole: Test type (TestApp app, TestEnv environment)',
html: expect.stringContaining('<p><b>App Name: TestApp\nEnvironment Name: TestEnv</b></p>')
html: expect.stringMatching(
/<p><b>App Name:<\/b> TestApp<\/p>\s*<p><b>Environment Name:<\/b> TestEnv<\/p><br\/><pre style="border: 1px solid #ccc; background-color: #f9f9f9; padding: 10px;">Test message<\/pre>/
)
}));
expect(result).toBe(true);
});
Expand Down Expand Up @@ -369,8 +372,8 @@ describe('EmailService.sendAlert', () => {
mockStorageConnection.getConfig.mockResolvedValue(mockConfig);

const result = await EmailService.sendAlert('Test message', 'Test type', {});
expect(result).toBe(false); // Expect result to be false when email integration is disabled
// Removed the console.log expectation since it's not in the function code
expect(result).toBe(false); // Expecting result to be false when email integration is disabled.
// No need to check console.log as it's not used in the function
});

it('should construct email with appName and environmentName', async () => {
Expand Down Expand Up @@ -400,7 +403,9 @@ describe('EmailService.sendAlert', () => {
from: '[email protected]',
to: '[email protected]',
subject: 'Errsole: Test type (TestApp app, TestEnv environment)',
html: expect.stringContaining('<p><b>App Name: TestApp\nEnvironment Name: TestEnv</b></p>')
html: expect.stringMatching(
/<p><b>App Name:<\/b> TestApp<\/p>\s*<p><b>Environment Name:<\/b> TestEnv<\/p><br\/><pre style="border: 1px solid #ccc; background-color: #f9f9f9; padding: 10px;">Test message<\/pre>/
)
}));
expect(result).toBe(true);
});
Expand Down Expand Up @@ -432,7 +437,9 @@ describe('EmailService.sendAlert', () => {
from: '[email protected]',
to: '[email protected]',
subject: 'Errsole: Test type (TestEnv environment)',
html: expect.stringContaining('<p><b>Environment Name: TestEnv</b></p>')
html: expect.stringMatching(
/<p><b>Environment Name:<\/b> TestEnv<\/p><br\/><pre style="border: 1px solid #ccc; background-color: #f9f9f9; padding: 10px;">Test message<\/pre>/
)
}));
expect(result).toBe(true);
});
Expand Down

0 comments on commit 0639af2

Please sign in to comment.