From e29c0cf1eb778665f5557b05bc3ba0508f8229a0 Mon Sep 17 00:00:00 2001 From: dbarkowsky Date: Wed, 18 Dec 2024 11:13:01 -0800 Subject: [PATCH] add unit tests --- express-api/src/utilities/failedEmailCheck.ts | 2 +- express-api/tests/testUtils/factories.ts | 7 +- .../unit/utilities/failedEmailCheck.test.ts | 64 +++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 express-api/tests/unit/utilities/failedEmailCheck.test.ts diff --git a/express-api/src/utilities/failedEmailCheck.ts b/express-api/src/utilities/failedEmailCheck.ts index 0e0d99c10..d5d1d4ebf 100644 --- a/express-api/src/utilities/failedEmailCheck.ts +++ b/express-api/src/utilities/failedEmailCheck.ts @@ -79,7 +79,7 @@ const failedEmailCheck = async () => { } } } catch (e) { - logger.error(`Error in failedEmailCheck: ${e}`); + logger.error(`Error in failedEmailCheck: ${(e as Error).message}`); } }; diff --git a/express-api/tests/testUtils/factories.ts b/express-api/tests/testUtils/factories.ts index a4c036f4e..678fafe8c 100644 --- a/express-api/tests/testUtils/factories.ts +++ b/express-api/tests/testUtils/factories.ts @@ -912,7 +912,10 @@ export const produceProjectNotification = (props?: Partial) => { +export const produceNotificationQueue = ( + props?: Partial, + includeProject: boolean = false, +) => { const queue: NotificationQueue = { Id: faker.number.int(), Key: randomUUID(), @@ -928,7 +931,7 @@ export const produceNotificationQueue = (props?: Partial) => 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(), diff --git a/express-api/tests/unit/utilities/failedEmailCheck.test.ts b/express-api/tests/unit/utilities/failedEmailCheck.test.ts new file mode 100644 index 000000000..4c85ad1a8 --- /dev/null +++ b/express-api/tests/unit/utilities/failedEmailCheck.test.ts @@ -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); + }); +});