Skip to content

Commit 0639af2

Browse files
committed
test(EmailService): update sendAlert tests with regex for HTML matching
1 parent fc2ecee commit 0639af2

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

tests/utils/alert.test.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { getStorageConnection } = require('../../lib/main/server/storageConnectio
22
const axios = require('axios');
33
const nodemailer = require('nodemailer');
44
const { EmailService, SlackService, testSlackAlert, testEmailAlert, clearEmailTransport, customLoggerAlert, handleUncaughtExceptions } = require('../../lib/main/server/utils/alerts'); // Adjust the path as needed
5-
/* globals expect, jest, beforeEach, describe, beforeAll, afterAll, it, afterEach */
5+
/* globals expect, jest, beforeEach, describe, beforeAll, afterAll, it, afterEach */
66

77
jest.mock('axios');
88
jest.mock('nodemailer');
@@ -110,11 +110,12 @@ describe('EmailService', () => {
110110

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

113+
// Use a regular expression to match the 'html' content, allowing for flexible whitespace
113114
expect(mockTransporter.sendMail).toHaveBeenCalledWith(expect.objectContaining({
114115
115116
116117
subject: 'Errsole: Test type (TestApp app, TestEnv environment)',
117-
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>'
118+
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>/)
118119
}));
119120
expect(result).toBe(true);
120121
});
@@ -185,7 +186,7 @@ describe('EmailService', () => {
185186
186187
187188
subject: 'Errsole: Test type (TestApp app, TestEnv environment)',
188-
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>'
189+
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>/)
189190
}));
190191
});
191192

@@ -217,7 +218,7 @@ describe('EmailService', () => {
217218
218219
219220
subject: 'Errsole: Test type (TestApp app)',
220-
html: '<p><b>App Name: TestApp</b></p><br/><pre style="border: 1px solid #ccc; background-color: #f9f9f9; padding: 10px;">Test message</pre>'
221+
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>/)
221222
}));
222223
});
223224

@@ -249,7 +250,7 @@ describe('EmailService', () => {
249250
250251
251252
subject: 'Errsole: Test type (TestEnv environment)',
252-
html: '<p><b>Environment Name: TestEnv</b></p><br/><pre style="border: 1px solid #ccc; background-color: #f9f9f9; padding: 10px;">Test message</pre>'
253+
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>/)
253254
}));
254255
});
255256

@@ -379,6 +380,7 @@ describe('SlackService', () => {
379380
});
380381

381382
const result = await SlackService.sendAlert('Test message', 'Test type', {});
383+
382384
expect(console.error).toHaveBeenCalledWith('Failed to send slack alert:', expect.any(Error));
383385
expect(result).toBe(false);
384386
});
@@ -390,6 +392,7 @@ describe('SlackService', () => {
390392
mockStorageConnection.getConfig.mockResolvedValue(mockConfig);
391393

392394
const result = await SlackService.sendAlert('Test message', 'Test type', {});
395+
393396
expect(result).toBe(false);
394397
});
395398
});

tests/utils/main.test.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,14 @@ describe('EmailService.sendAlert', () => {
325325

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

328+
// Updated 'html' expectation using regex
328329
expect(mockTransporter.sendMail).toHaveBeenCalledWith(expect.objectContaining({
329330
330331
331332
subject: 'Errsole: Test type (TestApp app, TestEnv environment)',
332-
html: expect.stringContaining('<p><b>App Name: TestApp\nEnvironment Name: TestEnv</b></p>')
333+
html: expect.stringMatching(
334+
/<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>/
335+
)
333336
}));
334337
expect(result).toBe(true);
335338
});
@@ -369,8 +372,8 @@ describe('EmailService.sendAlert', () => {
369372
mockStorageConnection.getConfig.mockResolvedValue(mockConfig);
370373

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

376379
it('should construct email with appName and environmentName', async () => {
@@ -400,7 +403,9 @@ describe('EmailService.sendAlert', () => {
400403
401404
402405
subject: 'Errsole: Test type (TestApp app, TestEnv environment)',
403-
html: expect.stringContaining('<p><b>App Name: TestApp\nEnvironment Name: TestEnv</b></p>')
406+
html: expect.stringMatching(
407+
/<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>/
408+
)
404409
}));
405410
expect(result).toBe(true);
406411
});
@@ -432,7 +437,9 @@ describe('EmailService.sendAlert', () => {
432437
433438
434439
subject: 'Errsole: Test type (TestEnv environment)',
435-
html: expect.stringContaining('<p><b>Environment Name: TestEnv</b></p>')
440+
html: expect.stringMatching(
441+
/<p><b>Environment Name:<\/b> TestEnv<\/p><br\/><pre style="border: 1px solid #ccc; background-color: #f9f9f9; padding: 10px;">Test message<\/pre>/
442+
)
436443
}));
437444
expect(result).toBe(true);
438445
});

0 commit comments

Comments
 (0)