Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarkowsky committed Dec 18, 2024
1 parent d864d3f commit e29c0cf
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
2 changes: 1 addition & 1 deletion express-api/src/utilities/failedEmailCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const failedEmailCheck = async () => {
}
}
} catch (e) {
logger.error(`Error in failedEmailCheck: ${e}`);
logger.error(`Error in failedEmailCheck: ${(e as Error).message}`);
}
};

Expand Down
7 changes: 5 additions & 2 deletions express-api/tests/testUtils/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,10 @@ export const produceProjectNotification = (props?: Partial<ProjectStatusNotifica
return notif;
};

export const produceNotificationQueue = (props?: Partial<NotificationQueue>) => {
export const produceNotificationQueue = (
props?: Partial<NotificationQueue>,
includeProject: boolean = false,
) => {
const queue: NotificationQueue = {
Id: faker.number.int(),
Key: randomUUID(),
Expand All @@ -928,7 +931,7 @@ export const produceNotificationQueue = (props?: Partial<NotificationQueue>) =>
Cc: '',
Tag: faker.lorem.word(),
ProjectId: faker.number.int(),
Project: undefined,
Project: includeProject ? produceProject() : undefined,
ToAgencyId: faker.number.int(),
ToAgency: undefined,
TemplateId: faker.number.int(),
Expand Down
64 changes: 64 additions & 0 deletions express-api/tests/unit/utilities/failedEmailCheck.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { AppDataSource } from '@/appDataSource';
import chesServices, { IEmailSentResponse } from '@/services/ches/chesServices';
import notificationServices from '@/services/notifications/notificationServices';
import { NotificationQueue } from '@/typeorm/Entities/NotificationQueue';
import { User } from '@/typeorm/Entities/User';
import failedEmailCheck from '@/utilities/failedEmailCheck';
import logger from '@/utilities/winstonLogger';
import { faker } from '@faker-js/faker/.';
import { produceNotificationQueue, produceUser } from 'tests/testUtils/factories';
import nunjucks from 'nunjucks';

const _notificationFindSpy = jest
.spyOn(AppDataSource.getRepository(NotificationQueue), 'find')
.mockImplementation(async () => [produceNotificationQueue({}, true)]);

const _notificationServicesUpdateSpy = jest
.spyOn(notificationServices, 'updateNotificationStatus')
.mockImplementation(async () => produceNotificationQueue());

const _userFindSpy = jest
.spyOn(AppDataSource.getRepository(User), 'findOneOrFail')
.mockImplementation(async () => produceUser({ Username: 'system' }));

const _nunjucksSpy = jest.spyOn(nunjucks, 'render').mockImplementation(() => {
'mock html goes here';
});

const _chesSendEmailAsyncSpy = jest
.spyOn(chesServices, 'sendEmailAsync')
.mockImplementation(async () => {
return {
messages: [{ msgId: faker.string.uuid, to: faker.internet.email }],
txId: faker.string.uuid,
} as unknown as IEmailSentResponse;
});

const _loggerErrorSpy = jest.spyOn(logger, 'error');

describe('UNIT - failedEmailCheck', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should attempt to send an email if notifications are outstanding', async () => {
await failedEmailCheck();
expect(_notificationServicesUpdateSpy).toHaveBeenCalledTimes(1);
expect(_notificationFindSpy).toHaveBeenCalledTimes(2);
expect(_userFindSpy).toHaveBeenCalledTimes(1);
expect(_nunjucksSpy).toHaveBeenCalledTimes(1);
expect(_chesSendEmailAsyncSpy).toHaveBeenCalledTimes(1);
});

it('should report an error if the email failed to send', async () => {
_chesSendEmailAsyncSpy.mockImplementationOnce(async () => null);
await failedEmailCheck();
expect(_loggerErrorSpy).toHaveBeenCalledWith(
'Error in failedEmailCheck: Email was attempted but not sent. This feature could be disabled.',
);
expect(_notificationServicesUpdateSpy).toHaveBeenCalledTimes(1);
expect(_notificationFindSpy).toHaveBeenCalledTimes(2);
expect(_userFindSpy).toHaveBeenCalledTimes(1);
expect(_nunjucksSpy).toHaveBeenCalledTimes(1);
expect(_chesSendEmailAsyncSpy).toHaveBeenCalledTimes(1);
});
});

0 comments on commit e29c0cf

Please sign in to comment.