-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(EmailService): update sendAlert tests with regex for HTML matching
- Loading branch information
1 parent
fc2ecee
commit 0639af2
Showing
2 changed files
with
20 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
|
@@ -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); | ||
}); | ||
|
@@ -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>/) | ||
})); | ||
}); | ||
|
||
|
@@ -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>/) | ||
})); | ||
}); | ||
|
||
|
@@ -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>/) | ||
})); | ||
}); | ||
|
||
|
@@ -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); | ||
}); | ||
|
@@ -390,6 +392,7 @@ describe('SlackService', () => { | |
mockStorageConnection.getConfig.mockResolvedValue(mockConfig); | ||
|
||
const result = await SlackService.sendAlert('Test message', 'Test type', {}); | ||
|
||
expect(result).toBe(false); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
}); | ||
|
@@ -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 () => { | ||
|
@@ -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); | ||
}); | ||
|
@@ -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); | ||
}); | ||
|