Skip to content

Commit 93c9bdb

Browse files
YufanKambanggithub-actions[bot]
authored andcommitted
generated the unit test
1 parent 30f3cd2 commit 93c9bdb

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import 'reflect-metadata';
2+
import { faker } from '@faker-js/faker';
3+
import * as Logger from '@user-office-software/duo-logger';
4+
import { container } from 'tsyringe';
5+
6+
import { Tokens } from '../../config/Tokens';
7+
import { ApplicationEvent } from '../../events/applicationEvents';
8+
import { Event } from '../../events/event.enum';
9+
import { stfcEmailHandler } from './stfcEmailHandler';
10+
11+
const ORIGINAL_ENV = process.env;
12+
const spyLogError = jest
13+
.spyOn(Logger.logger, 'logError')
14+
.mockImplementation(() => {});
15+
const spyLogInfo = jest
16+
.spyOn(Logger.logger, 'logInfo')
17+
.mockImplementation(() => {});
18+
// Mock MailService
19+
const mockMailService = {
20+
sendMail: jest.fn(),
21+
};
22+
//we should have a test email inbocx to recieve all the email instead of skipping it, for e2e testing and for better pathways
23+
describe('stfcEmailHandler', () => {
24+
beforeAll(() => {
25+
container.registerInstance(Tokens.MailService, mockMailService);
26+
});
27+
afterEach(() => {
28+
process.env = ORIGINAL_ENV;
29+
jest.clearAllMocks();
30+
jest.resetModules();
31+
});
32+
33+
describe('handling CALL_CREATED event', () => {
34+
it('When running Node process does not have env.FBS_EMAIL value', () => {
35+
process.env.FBS_EMAIL = '';
36+
const mockEvent = {
37+
type: Event.CALL_CREATED,
38+
call: {},
39+
isRejection: false,
40+
} as ApplicationEvent;
41+
42+
stfcEmailHandler(mockEvent);
43+
44+
expect(process.env.FBS_EMAIL).toBe('');
45+
expect(spyLogError).toHaveBeenCalledTimes(1);
46+
expect(spyLogError).toHaveBeenCalledWith(
47+
'Could not send email(s) on call creation, environmental variable (FBS_EMAIL) not found',
48+
{}
49+
);
50+
});
51+
});
52+
53+
describe('mailService.sendMail is sucessful', () => {
54+
it('When all required settings are valid', async () => {
55+
// here instead of using a rng email, we instead set a magic variable for readability
56+
// This test us
57+
const inviteEmail = faker.internet.email();
58+
process.env.FBS_EMAIL = inviteEmail;
59+
const mockEvent = {
60+
type: Event.CALL_CREATED,
61+
call: {
62+
shortCode: 'string',
63+
startCall: new Date(2000, 1, 1),
64+
endCall: new Date(2000, 1, 2),
65+
},
66+
isRejection: false,
67+
} as ApplicationEvent;
68+
69+
//create mock instances
70+
mockMailService.sendMail.mockResolvedValue({ success: true });
71+
72+
await stfcEmailHandler(mockEvent);
73+
74+
expect(process.env.FBS_EMAIL).toBe(inviteEmail);
75+
expect(mockMailService.sendMail).toHaveBeenCalledWith({
76+
content: { template_id: 'call-created-email' },
77+
substitution_data: {
78+
shortCode: 'string',
79+
startCall: new Date(2000, 1, 1),
80+
endCall: new Date(2000, 1, 2),
81+
},
82+
recipients: [{ address: inviteEmail }],
83+
});
84+
expect(spyLogInfo).toHaveBeenCalledTimes(1);
85+
// this result is derived from the SkipSendMailService.ts, as that is what is mapped in the test environment
86+
expect(spyLogInfo).toHaveBeenCalledWith('Emails sent on call creation:', {
87+
result: {
88+
success: true,
89+
},
90+
event: mockEvent,
91+
});
92+
});
93+
});
94+
95+
describe('mailService.sendMail is not sucessful', () => {
96+
it('Then mailService.catch is evoked, logError(x) will be present', async () => {
97+
const inviteEmail = faker.internet.email();
98+
process.env.FBS_EMAIL = inviteEmail;
99+
const mockEvent = {
100+
type: Event.CALL_CREATED,
101+
call: {
102+
shortCode: 'error',
103+
},
104+
isRejection: false,
105+
} as ApplicationEvent;
106+
const forcedError = new Error('SMTP down');
107+
108+
mockMailService.sendMail.mockRejectedValueOnce(forcedError);
109+
container.registerInstance(Tokens.MailService, mockMailService);
110+
111+
await stfcEmailHandler(mockEvent);
112+
// have added this line as the class.method in the handler function is not asynced
113+
// and the asserts/expects will check before .then or .catch gets process
114+
await new Promise(setImmediate);
115+
116+
expect(mockMailService.sendMail).toHaveBeenCalledWith({
117+
content: { template_id: 'call-created-email' },
118+
substitution_data: {
119+
shortCode: 'error',
120+
},
121+
recipients: [{ address: inviteEmail }],
122+
});
123+
expect(mockMailService.sendMail).toHaveBeenCalled();
124+
expect(spyLogError).toHaveBeenCalledTimes(1);
125+
expect(spyLogError).toHaveBeenCalledWith(
126+
'Could not send email(s) on call creation:',
127+
{
128+
error: forcedError,
129+
event: mockEvent,
130+
}
131+
);
132+
});
133+
});
134+
});

0 commit comments

Comments
 (0)