From 29069f1af301f5adf6fc8130be0c289221624bb1 Mon Sep 17 00:00:00 2001 From: GoncaloCanteiro Date: Tue, 14 Mar 2023 15:31:12 +0000 Subject: [PATCH 01/19] test: add slack-responsible.consumer test --- .../slack-responsible.consumer.spec.ts | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 backend/src/modules/communication/consumers/slack-responsible.consumer.spec.ts diff --git a/backend/src/modules/communication/consumers/slack-responsible.consumer.spec.ts b/backend/src/modules/communication/consumers/slack-responsible.consumer.spec.ts new file mode 100644 index 000000000..342568582 --- /dev/null +++ b/backend/src/modules/communication/consumers/slack-responsible.consumer.spec.ts @@ -0,0 +1,55 @@ +import { DeepMocked, createMock } from '@golevelup/ts-jest'; +import { Test, TestingModule } from '@nestjs/testing'; +import { ResponsibleApplicationInterface } from '../interfaces/responsible.application.interface'; +import { TYPES } from 'src/modules/communication/interfaces/types'; +import { SlackResponsibleConsumer } from './slack-responsible.consumer'; +import { ChangeResponsibleType } from '../dto/types'; +import { Job } from 'bull'; + +const changeResponsibleMock = { + id: 1, + data: { + newResponsibleEmail: 'newResponsible@gmail.com', + previousResponsibleEmail: 'previousResponsible@gmail.com', + subTeamChannelId: 'C99CQSJC1M5', + email: 'someEmail@gmail.com', + teamNumber: 2, + responsiblesChannelId: 'C03CLDK1M2', + mainChannelId: 'C03CQSJC1M2' + } +}; + +describe('SlackResponsibleConsumer', () => { + let consumer: SlackResponsibleConsumer; + let responsibleApplicationMock: DeepMocked; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + SlackResponsibleConsumer, + { + provide: TYPES.application.SlackResponsibleApplication, + useValue: createMock + } + ] + }).compile(); + consumer = module.get(SlackResponsibleConsumer); + responsibleApplicationMock = module.get(TYPES.application.SlackResponsibleApplication); + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should be defined', () => { + expect(consumer).toBeDefined(); + }); + + it('should call sendMessageApplication.execute once with job.data', async () => { + await consumer.communication(changeResponsibleMock as unknown as Job); + expect(responsibleApplicationMock.execute).toHaveBeenNthCalledWith( + 1, + changeResponsibleMock.data + ); + }); +}); From 8dfec49c19f9389550304b4ba8ae511c23b767fe Mon Sep 17 00:00:00 2001 From: GoncaloCanteiro Date: Tue, 14 Mar 2023 15:32:15 +0000 Subject: [PATCH 02/19] refactor: message from slack-send-message it --- .../communication/consumers/slack-send-message.consumer.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/modules/communication/consumers/slack-send-message.consumer.spec.ts b/backend/src/modules/communication/consumers/slack-send-message.consumer.spec.ts index 4f3e99e12..89a960a73 100644 --- a/backend/src/modules/communication/consumers/slack-send-message.consumer.spec.ts +++ b/backend/src/modules/communication/consumers/slack-send-message.consumer.spec.ts @@ -39,7 +39,7 @@ describe('SlackSendMessageConsumer', () => { expect(consumer).toBeDefined(); }); - it('should call sendMessageApplication.execute with job.data 1 time', async () => { + it('should call sendMessageApplication.execute once with job.data', async () => { await consumer.communication(slackMessageMock as unknown as Job); expect(sendMessageApplicationMock.execute).toHaveBeenNthCalledWith(1, slackMessageMock.data); }); From 35f6f6accde6b5030869a8c8d76a67f16fb0d638 Mon Sep 17 00:00:00 2001 From: GoncaloCanteiro Date: Tue, 14 Mar 2023 15:38:00 +0000 Subject: [PATCH 03/19] refactor: fix typo in file name --- ...er-channel.consummer.ts => slack-add-user-channel.consumer.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename backend/src/modules/communication/consumers/{slack-add-user-channel.consummer.ts => slack-add-user-channel.consumer.ts} (100%) diff --git a/backend/src/modules/communication/consumers/slack-add-user-channel.consummer.ts b/backend/src/modules/communication/consumers/slack-add-user-channel.consumer.ts similarity index 100% rename from backend/src/modules/communication/consumers/slack-add-user-channel.consummer.ts rename to backend/src/modules/communication/consumers/slack-add-user-channel.consumer.ts From dae2268dd5533cad4a7a8a176e5f14857195c204 Mon Sep 17 00:00:00 2001 From: GoncaloCanteiro Date: Tue, 14 Mar 2023 15:38:57 +0000 Subject: [PATCH 04/19] fix: change import path --- backend/src/modules/communication/communication.module.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/modules/communication/communication.module.ts b/backend/src/modules/communication/communication.module.ts index e24ebfba7..5452cf1d9 100644 --- a/backend/src/modules/communication/communication.module.ts +++ b/backend/src/modules/communication/communication.module.ts @@ -21,7 +21,7 @@ import { SlackArchiveChannelConsumer } from 'src/modules/communication/consumers import { SlackCommunicationConsumer } from 'src/modules/communication/consumers/slack-communication.consumer'; import { SlackArchiveChannelProducer } from 'src/modules/communication/producers/slack-archive-channel.producer'; import { SlackCommunicationProducer } from 'src/modules/communication/producers/slack-communication.producer'; -import { SlackAddUserToChannelConsumer } from './consumers/slack-add-user-channel.consummer'; +import { SlackAddUserToChannelConsumer } from './consumers/slack-add-user-channel.consumer'; import { SlackMergeBoardConsumer } from './consumers/slack-merge-board.consumer'; import { SlackResponsibleConsumer } from './consumers/slack-responsible.consumer'; import { SlackSendMessageConsumer } from './consumers/slack-send-message.consumer'; From 2a59a796a4306233be355237c9158848f4e4a8f4 Mon Sep 17 00:00:00 2001 From: GoncaloCanteiro Date: Tue, 14 Mar 2023 15:45:00 +0000 Subject: [PATCH 05/19] fix: missing parentheses in createMock --- .../communication/consumers/slack-responsible.consumer.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/modules/communication/consumers/slack-responsible.consumer.spec.ts b/backend/src/modules/communication/consumers/slack-responsible.consumer.spec.ts index 342568582..d33162708 100644 --- a/backend/src/modules/communication/consumers/slack-responsible.consumer.spec.ts +++ b/backend/src/modules/communication/consumers/slack-responsible.consumer.spec.ts @@ -29,7 +29,7 @@ describe('SlackResponsibleConsumer', () => { SlackResponsibleConsumer, { provide: TYPES.application.SlackResponsibleApplication, - useValue: createMock + useValue: createMock() } ] }).compile(); From 553d45241c88c6d4788ebcdf15768d85fb619c3d Mon Sep 17 00:00:00 2001 From: GoncaloCanteiro Date: Tue, 14 Mar 2023 17:47:37 +0000 Subject: [PATCH 06/19] test: add slack-add-user-channel.consumer test --- .../slack-add-user-channel.consumer.spec.ts | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 backend/src/modules/communication/consumers/slack-add-user-channel.consumer.spec.ts diff --git a/backend/src/modules/communication/consumers/slack-add-user-channel.consumer.spec.ts b/backend/src/modules/communication/consumers/slack-add-user-channel.consumer.spec.ts new file mode 100644 index 000000000..25c25638c --- /dev/null +++ b/backend/src/modules/communication/consumers/slack-add-user-channel.consumer.spec.ts @@ -0,0 +1,65 @@ +import { DeepMocked, createMock } from '@golevelup/ts-jest'; +import { Test, TestingModule } from '@nestjs/testing'; +import { TYPES } from 'src/modules/communication/interfaces/types'; +import { AddUserMainChannelType } from '../dto/types'; +import { Job } from 'bull'; +import { SlackAddUserToChannelConsumer } from './slack-add-user-channel.consumer'; +import { AddUserIntoChannelApplicationInterface } from '../interfaces/communication.application.interface copy'; +import { Logger } from '@nestjs/common'; + +const changeResponsibleMock = { + id: 1, + data: { + email: 'someEmail@gmail.com' + } +}; + +describe('SlackResponsibleConsumer', () => { + let consumer: SlackAddUserToChannelConsumer; + let addUserIntoChannelAppMock: DeepMocked; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + SlackAddUserToChannelConsumer, + { + provide: TYPES.application.SlackAddUserIntoChannelApplication, + useValue: createMock() + } + ] + }).compile(); + consumer = module.get(SlackAddUserToChannelConsumer); + addUserIntoChannelAppMock = module.get(TYPES.application.SlackAddUserIntoChannelApplication); + }); + + beforeEach(() => { + jest.clearAllMocks(); + jest.restoreAllMocks(); + }); + + it('should be defined', () => { + expect(consumer).toBeDefined(); + }); + + describe('communication', () => { + it('should call sendMessageApplication.execute once with job.data', async () => { + await consumer.communication(changeResponsibleMock as unknown as Job); + expect(addUserIntoChannelAppMock.execute).toHaveBeenNthCalledWith( + 1, + changeResponsibleMock.data.email + ); + }); + }); + + describe('onCompleted', () => { + it('should call Logger with string containing a email ', async () => { + const spyLogger = jest.spyOn(Logger.prototype, 'verbose'); + const result: boolean[] = [true]; + await consumer.onCompleted( + changeResponsibleMock as unknown as Job, + result + ); + expect(spyLogger).toHaveBeenNthCalledWith(1, expect.stringContaining('someEmail@gmail.com')); + }); + }); +}); From 6aa68e0bcf5a7f1c5371a0eb7f0a85f8d70091bd Mon Sep 17 00:00:00 2001 From: GoncaloCanteiro Date: Tue, 14 Mar 2023 17:48:16 +0000 Subject: [PATCH 07/19] fix: remove saveLog This method is not relevant anymore --- .../communication/consumers/slack-add-user-channel.consumer.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/backend/src/modules/communication/consumers/slack-add-user-channel.consumer.ts b/backend/src/modules/communication/consumers/slack-add-user-channel.consumer.ts index ee5a3bd74..e77554472 100644 --- a/backend/src/modules/communication/consumers/slack-add-user-channel.consumer.ts +++ b/backend/src/modules/communication/consumers/slack-add-user-channel.consumer.ts @@ -41,8 +41,5 @@ export class SlackAddUserToChannelConsumer extends SlackCommunicationEventListen !result[0] ? 'not' : '' } added to the main channel` ); - this.saveLog( - `User with email: ${job.data.email} was ${!result[0] ? 'not' : ''} added to the main channel` - ); } } From 42471244ab31177c4ca44f5781f7b3da945ec8f7 Mon Sep 17 00:00:00 2001 From: GoncaloCanteiro Date: Wed, 15 Mar 2023 09:34:04 +0000 Subject: [PATCH 08/19] fix: changed names --- .../slack-add-user-channel.consumer.spec.ts | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/backend/src/modules/communication/consumers/slack-add-user-channel.consumer.spec.ts b/backend/src/modules/communication/consumers/slack-add-user-channel.consumer.spec.ts index 25c25638c..d6098e2f6 100644 --- a/backend/src/modules/communication/consumers/slack-add-user-channel.consumer.spec.ts +++ b/backend/src/modules/communication/consumers/slack-add-user-channel.consumer.spec.ts @@ -7,14 +7,14 @@ import { SlackAddUserToChannelConsumer } from './slack-add-user-channel.consumer import { AddUserIntoChannelApplicationInterface } from '../interfaces/communication.application.interface copy'; import { Logger } from '@nestjs/common'; -const changeResponsibleMock = { +const newUserMock = { id: 1, data: { email: 'someEmail@gmail.com' } }; -describe('SlackResponsibleConsumer', () => { +describe('SlackAddUserToChannelConsumer', () => { let consumer: SlackAddUserToChannelConsumer; let addUserIntoChannelAppMock: DeepMocked; @@ -43,11 +43,8 @@ describe('SlackResponsibleConsumer', () => { describe('communication', () => { it('should call sendMessageApplication.execute once with job.data', async () => { - await consumer.communication(changeResponsibleMock as unknown as Job); - expect(addUserIntoChannelAppMock.execute).toHaveBeenNthCalledWith( - 1, - changeResponsibleMock.data.email - ); + await consumer.communication(newUserMock as unknown as Job); + expect(addUserIntoChannelAppMock.execute).toHaveBeenNthCalledWith(1, newUserMock.data.email); }); }); @@ -55,10 +52,7 @@ describe('SlackResponsibleConsumer', () => { it('should call Logger with string containing a email ', async () => { const spyLogger = jest.spyOn(Logger.prototype, 'verbose'); const result: boolean[] = [true]; - await consumer.onCompleted( - changeResponsibleMock as unknown as Job, - result - ); + await consumer.onCompleted(newUserMock as unknown as Job, result); expect(spyLogger).toHaveBeenNthCalledWith(1, expect.stringContaining('someEmail@gmail.com')); }); }); From f87c5c5e6cf12ab73fb7dc68fc4f11753642475a Mon Sep 17 00:00:00 2001 From: GoncaloCanteiro Date: Wed, 15 Mar 2023 10:18:07 +0000 Subject: [PATCH 09/19] refactor: change tests descriptions --- .../consumers/slack-add-user-channel.consumer.spec.ts | 2 +- .../communication/consumers/slack-responsible.consumer.spec.ts | 2 +- .../communication/consumers/slack-send-message.consumer.spec.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/src/modules/communication/consumers/slack-add-user-channel.consumer.spec.ts b/backend/src/modules/communication/consumers/slack-add-user-channel.consumer.spec.ts index d6098e2f6..78cc114fc 100644 --- a/backend/src/modules/communication/consumers/slack-add-user-channel.consumer.spec.ts +++ b/backend/src/modules/communication/consumers/slack-add-user-channel.consumer.spec.ts @@ -42,7 +42,7 @@ describe('SlackAddUserToChannelConsumer', () => { }); describe('communication', () => { - it('should call sendMessageApplication.execute once with job.data', async () => { + it('should call AddUserIntoChannelApplication.execute once with job.data', async () => { await consumer.communication(newUserMock as unknown as Job); expect(addUserIntoChannelAppMock.execute).toHaveBeenNthCalledWith(1, newUserMock.data.email); }); diff --git a/backend/src/modules/communication/consumers/slack-responsible.consumer.spec.ts b/backend/src/modules/communication/consumers/slack-responsible.consumer.spec.ts index d33162708..62a5e2802 100644 --- a/backend/src/modules/communication/consumers/slack-responsible.consumer.spec.ts +++ b/backend/src/modules/communication/consumers/slack-responsible.consumer.spec.ts @@ -45,7 +45,7 @@ describe('SlackResponsibleConsumer', () => { expect(consumer).toBeDefined(); }); - it('should call sendMessageApplication.execute once with job.data', async () => { + it('should call application.execute once with job.data', async () => { await consumer.communication(changeResponsibleMock as unknown as Job); expect(responsibleApplicationMock.execute).toHaveBeenNthCalledWith( 1, diff --git a/backend/src/modules/communication/consumers/slack-send-message.consumer.spec.ts b/backend/src/modules/communication/consumers/slack-send-message.consumer.spec.ts index 89a960a73..4b980ebaa 100644 --- a/backend/src/modules/communication/consumers/slack-send-message.consumer.spec.ts +++ b/backend/src/modules/communication/consumers/slack-send-message.consumer.spec.ts @@ -39,7 +39,7 @@ describe('SlackSendMessageConsumer', () => { expect(consumer).toBeDefined(); }); - it('should call sendMessageApplication.execute once with job.data', async () => { + it('should call application.execute once with job.data', async () => { await consumer.communication(slackMessageMock as unknown as Job); expect(sendMessageApplicationMock.execute).toHaveBeenNthCalledWith(1, slackMessageMock.data); }); From 079d7fb7fa55b14c5c38f0e0fa03317d81d8fb27 Mon Sep 17 00:00:00 2001 From: GoncaloCanteiro Date: Wed, 15 Mar 2023 10:29:32 +0000 Subject: [PATCH 10/19] test: add slack-archive-channel.consumer test --- .../slack-archive-channel.consumer.spec.ts | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 backend/src/modules/communication/consumers/slack-archive-channel.consumer.spec.ts diff --git a/backend/src/modules/communication/consumers/slack-archive-channel.consumer.spec.ts b/backend/src/modules/communication/consumers/slack-archive-channel.consumer.spec.ts new file mode 100644 index 000000000..4e091c9cd --- /dev/null +++ b/backend/src/modules/communication/consumers/slack-archive-channel.consumer.spec.ts @@ -0,0 +1,51 @@ +import { DeepMocked, createMock } from '@golevelup/ts-jest'; +import { Test, TestingModule } from '@nestjs/testing'; +import { TYPES } from 'src/modules/communication/interfaces/types'; +import { ArchiveChannelData } from '../dto/types'; +import { Job } from 'bull'; +import { SlackArchiveChannelConsumer } from './slack-archive-channel.consumer'; +import { ArchiveChannelApplicationInterface } from '../interfaces/archive-channel.application.interface'; + +const archiveChannelDataMock = { + id: 1, + data: { + type: 'CHANNEL_ID', + data: { id: '1', slackChannelId: 'someSlackId' }, + cascade: true + } +}; + +describe('SlackArchiveChannelConsumer', () => { + let consumer: SlackArchiveChannelConsumer; + let addUserIntoChannelAppMock: DeepMocked; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + SlackArchiveChannelConsumer, + { + provide: TYPES.application.SlackArchiveChannelApplication, + useValue: createMock() + } + ] + }).compile(); + consumer = module.get(SlackArchiveChannelConsumer); + addUserIntoChannelAppMock = module.get(TYPES.application.SlackArchiveChannelApplication); + }); + + beforeEach(() => { + jest.clearAllMocks(); + jest.restoreAllMocks(); + }); + + it('should be defined', () => { + expect(consumer).toBeDefined(); + }); + + describe('communication', () => { + it('should call ArchiveChannelApplication.execute once with job.data', async () => { + await consumer.communication(archiveChannelDataMock as unknown as Job); + expect(addUserIntoChannelAppMock.execute).toHaveBeenCalledTimes(1); + }); + }); +}); From de920aeaa0ea41157f289116d1129a6c0cc7d174 Mon Sep 17 00:00:00 2001 From: GoncaloCanteiro Date: Wed, 15 Mar 2023 10:42:02 +0000 Subject: [PATCH 11/19] fix: change expect to toHaveBeenNthCalledWith --- .../consumers/slack-archive-channel.consumer.spec.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/backend/src/modules/communication/consumers/slack-archive-channel.consumer.spec.ts b/backend/src/modules/communication/consumers/slack-archive-channel.consumer.spec.ts index 4e091c9cd..b383b23c8 100644 --- a/backend/src/modules/communication/consumers/slack-archive-channel.consumer.spec.ts +++ b/backend/src/modules/communication/consumers/slack-archive-channel.consumer.spec.ts @@ -45,7 +45,10 @@ describe('SlackArchiveChannelConsumer', () => { describe('communication', () => { it('should call ArchiveChannelApplication.execute once with job.data', async () => { await consumer.communication(archiveChannelDataMock as unknown as Job); - expect(addUserIntoChannelAppMock.execute).toHaveBeenCalledTimes(1); + expect(addUserIntoChannelAppMock.execute).toHaveBeenNthCalledWith( + 1, + archiveChannelDataMock.data + ); }); }); }); From 509400fd06cf85325755ae28aa11e9deb46801fd Mon Sep 17 00:00:00 2001 From: GoncaloCanteiro Date: Wed, 15 Mar 2023 10:43:41 +0000 Subject: [PATCH 12/19] test: add slack-merge-board.consumer test --- .../slack-merge-board.consumer.spec.ts | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 backend/src/modules/communication/consumers/slack-merge-board.consumer.spec.ts diff --git a/backend/src/modules/communication/consumers/slack-merge-board.consumer.spec.ts b/backend/src/modules/communication/consumers/slack-merge-board.consumer.spec.ts new file mode 100644 index 000000000..bdfe96c33 --- /dev/null +++ b/backend/src/modules/communication/consumers/slack-merge-board.consumer.spec.ts @@ -0,0 +1,51 @@ +import { DeepMocked, createMock } from '@golevelup/ts-jest'; +import { Test, TestingModule } from '@nestjs/testing'; +import { TYPES } from 'src/modules/communication/interfaces/types'; +import { MergeBoardType } from '../dto/types'; +import { Job } from 'bull'; +import { SlackMergeBoardConsumer } from './slack-merge-board.consumer'; +import { MergeBoardApplicationInterface } from '../interfaces/merge-board.application.interface'; + +const mergeBoardTypeMock = { + id: 1, + data: { + teamNumber: 1, + responsiblesChannelId: 'someResponsiblesChannelId', + isLastSubBoard: true, + boardId: 'someBoardId', + mainBoardId: 'mainBoardId' + } +}; + +describe('SlackMergeBoardConsumer', () => { + let consumer: SlackMergeBoardConsumer; + let responsibleApplicationMock: DeepMocked; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + SlackMergeBoardConsumer, + { + provide: TYPES.application.SlackMergeBoardApplication, + useValue: createMock() + } + ] + }).compile(); + consumer = module.get(SlackMergeBoardConsumer); + responsibleApplicationMock = module.get(TYPES.application.SlackMergeBoardApplication); + }); + + beforeEach(() => { + jest.clearAllMocks(); + jest.restoreAllMocks(); + }); + + it('should be defined', () => { + expect(consumer).toBeDefined(); + }); + + it('should call application.execute once with job.data', async () => { + await consumer.communication(mergeBoardTypeMock as unknown as Job); + expect(responsibleApplicationMock.execute).toHaveBeenNthCalledWith(1, mergeBoardTypeMock.data); + }); +}); From 11a7e88124a77b92c535c6e219d02da2af8ce6b6 Mon Sep 17 00:00:00 2001 From: GoncaloCanteiro Date: Wed, 15 Mar 2023 11:02:13 +0000 Subject: [PATCH 13/19] test: add Logger spys in slack-archive --- .../slack-archive-channel.consumer.spec.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/backend/src/modules/communication/consumers/slack-archive-channel.consumer.spec.ts b/backend/src/modules/communication/consumers/slack-archive-channel.consumer.spec.ts index b383b23c8..d3e46094b 100644 --- a/backend/src/modules/communication/consumers/slack-archive-channel.consumer.spec.ts +++ b/backend/src/modules/communication/consumers/slack-archive-channel.consumer.spec.ts @@ -5,6 +5,7 @@ import { ArchiveChannelData } from '../dto/types'; import { Job } from 'bull'; import { SlackArchiveChannelConsumer } from './slack-archive-channel.consumer'; import { ArchiveChannelApplicationInterface } from '../interfaces/archive-channel.application.interface'; +import { Logger } from '@nestjs/common'; const archiveChannelDataMock = { id: 1, @@ -45,10 +46,25 @@ describe('SlackArchiveChannelConsumer', () => { describe('communication', () => { it('should call ArchiveChannelApplication.execute once with job.data', async () => { await consumer.communication(archiveChannelDataMock as unknown as Job); + expect(addUserIntoChannelAppMock.execute).toHaveBeenNthCalledWith( 1, archiveChannelDataMock.data ); }); + + it('should call Logger when type CHANNEL_ID', async () => { + const spyLogger = jest.spyOn(Logger.prototype, 'verbose'); + //Type 'CHANNEL_ID' + await consumer.communication(archiveChannelDataMock as unknown as Job); + expect(spyLogger).toBeCalledTimes(1); + }); + it('should call Logger when type BOARD', async () => { + const spyLogger = jest.spyOn(Logger.prototype, 'verbose'); + archiveChannelDataMock.data.type = 'BOARD'; + //Type 'BOARD' + await consumer.communication(archiveChannelDataMock as unknown as Job); + expect(spyLogger).toBeCalledTimes(1); + }); }); }); From a825b7e9c34d99dd9725a3a53f28b08d682de546 Mon Sep 17 00:00:00 2001 From: GoncaloCanteiro Date: Wed, 15 Mar 2023 11:25:15 +0000 Subject: [PATCH 14/19] fix: applicationMocks names --- .../consumers/slack-archive-channel.consumer.spec.ts | 9 +++------ .../consumers/slack-merge-board.consumer.spec.ts | 6 +++--- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/backend/src/modules/communication/consumers/slack-archive-channel.consumer.spec.ts b/backend/src/modules/communication/consumers/slack-archive-channel.consumer.spec.ts index d3e46094b..c800166aa 100644 --- a/backend/src/modules/communication/consumers/slack-archive-channel.consumer.spec.ts +++ b/backend/src/modules/communication/consumers/slack-archive-channel.consumer.spec.ts @@ -18,7 +18,7 @@ const archiveChannelDataMock = { describe('SlackArchiveChannelConsumer', () => { let consumer: SlackArchiveChannelConsumer; - let addUserIntoChannelAppMock: DeepMocked; + let archiveChannelAppMock: DeepMocked; beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ @@ -31,7 +31,7 @@ describe('SlackArchiveChannelConsumer', () => { ] }).compile(); consumer = module.get(SlackArchiveChannelConsumer); - addUserIntoChannelAppMock = module.get(TYPES.application.SlackArchiveChannelApplication); + archiveChannelAppMock = module.get(TYPES.application.SlackArchiveChannelApplication); }); beforeEach(() => { @@ -47,10 +47,7 @@ describe('SlackArchiveChannelConsumer', () => { it('should call ArchiveChannelApplication.execute once with job.data', async () => { await consumer.communication(archiveChannelDataMock as unknown as Job); - expect(addUserIntoChannelAppMock.execute).toHaveBeenNthCalledWith( - 1, - archiveChannelDataMock.data - ); + expect(archiveChannelAppMock.execute).toHaveBeenNthCalledWith(1, archiveChannelDataMock.data); }); it('should call Logger when type CHANNEL_ID', async () => { diff --git a/backend/src/modules/communication/consumers/slack-merge-board.consumer.spec.ts b/backend/src/modules/communication/consumers/slack-merge-board.consumer.spec.ts index bdfe96c33..6c8f4aa9d 100644 --- a/backend/src/modules/communication/consumers/slack-merge-board.consumer.spec.ts +++ b/backend/src/modules/communication/consumers/slack-merge-board.consumer.spec.ts @@ -19,7 +19,7 @@ const mergeBoardTypeMock = { describe('SlackMergeBoardConsumer', () => { let consumer: SlackMergeBoardConsumer; - let responsibleApplicationMock: DeepMocked; + let applicationMock: DeepMocked; beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ @@ -32,7 +32,7 @@ describe('SlackMergeBoardConsumer', () => { ] }).compile(); consumer = module.get(SlackMergeBoardConsumer); - responsibleApplicationMock = module.get(TYPES.application.SlackMergeBoardApplication); + applicationMock = module.get(TYPES.application.SlackMergeBoardApplication); }); beforeEach(() => { @@ -46,6 +46,6 @@ describe('SlackMergeBoardConsumer', () => { it('should call application.execute once with job.data', async () => { await consumer.communication(mergeBoardTypeMock as unknown as Job); - expect(responsibleApplicationMock.execute).toHaveBeenNthCalledWith(1, mergeBoardTypeMock.data); + expect(applicationMock.execute).toHaveBeenNthCalledWith(1, mergeBoardTypeMock.data); }); }); From b761864ca654748738082b16ab15160d7ef59ccd Mon Sep 17 00:00:00 2001 From: GoncaloCanteiro Date: Wed, 15 Mar 2023 15:22:07 +0000 Subject: [PATCH 15/19] test: add slack-communication-consumer --- .../slack-communication.consumer.spec.ts | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 backend/src/modules/communication/consumers/slack-communication.consumer.spec.ts diff --git a/backend/src/modules/communication/consumers/slack-communication.consumer.spec.ts b/backend/src/modules/communication/consumers/slack-communication.consumer.spec.ts new file mode 100644 index 000000000..26c64fc0c --- /dev/null +++ b/backend/src/modules/communication/consumers/slack-communication.consumer.spec.ts @@ -0,0 +1,77 @@ +import { DeepMocked, createMock } from '@golevelup/ts-jest'; +import { Test, TestingModule } from '@nestjs/testing'; +import { TYPES } from 'src/modules/communication/interfaces/types'; +import { TYPES as BOARD_TYPES } from 'src/modules/boards/interfaces/types'; +import { BoardType } from '../dto/types'; +import { Job } from 'bull'; +import { SlackCommunicationConsumer } from './slack-communication.consumer'; +import { CommunicationApplicationInterface } from '../interfaces/communication.application.interface'; +import { UpdateBoardServiceInterface } from 'src/modules/boards/interfaces/services/update.board.service.interface'; +import { Logger } from '@nestjs/common'; + +const mergeBoardTypeMock = { + id: 1, + data: { + id: 'someId', + title: 'someTitle', + isSubBoard: true, + dividedBoards: 'BoardType[]', + team: null, + users: 'UserRoleType[]', + slackChannelId: 'someSlackChannelId', + boardNumber: 1 + } +}; + +describe('SlackCommunicationConsumer', () => { + let consumer: SlackCommunicationConsumer; + let communicationAppMock: DeepMocked; + //let updateBoardServiceMock: DeepMocked; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + SlackCommunicationConsumer, + { + provide: TYPES.application.SlackCommunicationApplication, + useValue: createMock() + }, + { + provide: BOARD_TYPES.services.UpdateBoardService, + useValue: createMock() + } + ] + }).compile(); + consumer = module.get(SlackCommunicationConsumer); + communicationAppMock = module.get(TYPES.application.SlackCommunicationApplication); + }); + + beforeEach(() => { + jest.clearAllMocks(); + jest.restoreAllMocks(); + }); + + it('should be defined', () => { + expect(consumer).toBeDefined(); + }); + + describe('communication', () => { + it('should call application.execute once with job.data', async () => { + await consumer.communication(mergeBoardTypeMock as unknown as Job); + expect(communicationAppMock.execute).toHaveBeenNthCalledWith(1, mergeBoardTypeMock.data); + }); + + it('should call Logger when type BOARD', async () => { + const spyLogger = jest.spyOn(Logger.prototype, 'verbose'); + await consumer.communication(mergeBoardTypeMock as unknown as Job); + expect(spyLogger).toBeCalledTimes(1); + }); + }); + + // describe('onCompleted',()=> { + // it('should call application.execute once with job.data', async () => { + // await consumer.onCompleted(mergeBoardTypeMock as unknown as Job); + // //expect(responsibleApplicationMock.execute).toHaveBeenNthCalledWith(1, mergeBoardTypeMock.data); + // }); + // }); +}); From 1999a6708634d0c805ed2e908b709854f37b95f4 Mon Sep 17 00:00:00 2001 From: GoncaloCanteiro Date: Wed, 15 Mar 2023 16:44:24 +0000 Subject: [PATCH 16/19] fix: remove saveLog from communication listener --- .../slack-communication-event-listeners.ts | 14 +------------- .../consumers/slack-communication.consumer.ts | 1 - 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/backend/src/modules/communication/consumers/slack-communication-event-listeners.ts b/backend/src/modules/communication/consumers/slack-communication-event-listeners.ts index eae9cc304..482d37d0f 100644 --- a/backend/src/modules/communication/consumers/slack-communication-event-listeners.ts +++ b/backend/src/modules/communication/consumers/slack-communication-event-listeners.ts @@ -2,7 +2,6 @@ import { OnQueueCompleted, OnQueueError, OnQueueFailed, Process } from '@nestjs/bull'; import { Logger } from '@nestjs/common'; import { Job } from 'bull'; -import { createWriteStream } from 'fs'; export class SlackCommunicationEventListeners { constructor(public logger: Logger) {} @@ -13,9 +12,8 @@ export class SlackCommunicationEventListeners { // https://github.com/OptimalBits/bull/blob/develop/REFERENCE.md#events @OnQueueCompleted() - async onCompleted(job: Job, result: R[]) { + async onCompleted(job: Job, _result: R[]) { this.logger.verbose(`Completed Job id: "${job.id}"`); - this.saveLog(result); } @OnQueueFailed() @@ -27,14 +25,4 @@ export class SlackCommunicationEventListeners { onError(error: Error) { this.logger.error(`Error on queue: "${JSON.stringify(error)}"`); } - - saveLog(data: any) { - try { - const stream = createWriteStream('slackLog.txt', { flags: 'a' }); - stream.write(`${JSON.stringify(data)}\n`); - stream.end(); - } catch (error) { - this.logger.error(error); - } - } } diff --git a/backend/src/modules/communication/consumers/slack-communication.consumer.ts b/backend/src/modules/communication/consumers/slack-communication.consumer.ts index 257847a08..095f46254 100644 --- a/backend/src/modules/communication/consumers/slack-communication.consumer.ts +++ b/backend/src/modules/communication/consumers/slack-communication.consumer.ts @@ -43,6 +43,5 @@ export class SlackCommunicationConsumer extends SlackCommunicationEventListeners override async onCompleted(job: Job, result: TeamDto[]) { this.logger.verbose(`Completed Job id: "${job.id}"`); this.updateBoardService.updateChannelId(result); - this.saveLog(result); } } From dd1b5573fa19f69dd4f4936df905595f06c15c64 Mon Sep 17 00:00:00 2001 From: GoncaloCanteiro Date: Wed, 15 Mar 2023 16:45:40 +0000 Subject: [PATCH 17/19] test: add slack-communication.consumer test --- .../slack-communication.consumer.spec.ts | 43 +++++++++++++++---- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/backend/src/modules/communication/consumers/slack-communication.consumer.spec.ts b/backend/src/modules/communication/consumers/slack-communication.consumer.spec.ts index 26c64fc0c..2ead7eeba 100644 --- a/backend/src/modules/communication/consumers/slack-communication.consumer.spec.ts +++ b/backend/src/modules/communication/consumers/slack-communication.consumer.spec.ts @@ -8,6 +8,8 @@ import { SlackCommunicationConsumer } from './slack-communication.consumer'; import { CommunicationApplicationInterface } from '../interfaces/communication.application.interface'; import { UpdateBoardServiceInterface } from 'src/modules/boards/interfaces/services/update.board.service.interface'; import { Logger } from '@nestjs/common'; +import { UserFactory } from 'src/libs/test-utils/mocks/factories/user-factory'; +import { TeamDto } from 'src/modules/communication/dto/team.dto'; const mergeBoardTypeMock = { id: 1, @@ -17,16 +19,29 @@ const mergeBoardTypeMock = { isSubBoard: true, dividedBoards: 'BoardType[]', team: null, - users: 'UserRoleType[]', + users: '', slackChannelId: 'someSlackChannelId', boardNumber: 1 } }; +const result = [ + { + name: 'someName', + normalName: 'normalName', + boardId: 'someBoardId', + channelId: 'someChannelId', + type: 'team', + for: 'member', + participants: UserFactory.createMany(2), + teamNumber: 2 + } +]; + describe('SlackCommunicationConsumer', () => { let consumer: SlackCommunicationConsumer; let communicationAppMock: DeepMocked; - //let updateBoardServiceMock: DeepMocked; + let updateBoardServiceMock: DeepMocked; beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ @@ -44,6 +59,7 @@ describe('SlackCommunicationConsumer', () => { }).compile(); consumer = module.get(SlackCommunicationConsumer); communicationAppMock = module.get(TYPES.application.SlackCommunicationApplication); + updateBoardServiceMock = module.get(BOARD_TYPES.services.UpdateBoardService); }); beforeEach(() => { @@ -68,10 +84,21 @@ describe('SlackCommunicationConsumer', () => { }); }); - // describe('onCompleted',()=> { - // it('should call application.execute once with job.data', async () => { - // await consumer.onCompleted(mergeBoardTypeMock as unknown as Job); - // //expect(responsibleApplicationMock.execute).toHaveBeenNthCalledWith(1, mergeBoardTypeMock.data); - // }); - // }); + describe('onCompleted', () => { + it('should call Logger once', async () => { + const spyLogger = jest.spyOn(Logger.prototype, 'verbose'); + await consumer.onCompleted( + mergeBoardTypeMock as unknown as Job, + result as unknown as TeamDto[] + ); + expect(spyLogger).toBeCalledTimes(1); + }); + it('should call updateBoardService once', async () => { + await consumer.onCompleted( + mergeBoardTypeMock as unknown as Job, + result as unknown as TeamDto[] + ); + expect(updateBoardServiceMock.updateChannelId).toBeCalledTimes(1); + }); + }); }); From 9a44f493b96aac1581b66b65ac9e8a2e210477de Mon Sep 17 00:00:00 2001 From: GoncaloCanteiro Date: Wed, 15 Mar 2023 17:57:19 +0000 Subject: [PATCH 18/19] test: add slack-communication-event-listeners test --- ...lack-communication-event-listeners.spec.ts | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 backend/src/modules/communication/consumers/slack-communication-event-listeners.spec.ts diff --git a/backend/src/modules/communication/consumers/slack-communication-event-listeners.spec.ts b/backend/src/modules/communication/consumers/slack-communication-event-listeners.spec.ts new file mode 100644 index 000000000..6b087cc99 --- /dev/null +++ b/backend/src/modules/communication/consumers/slack-communication-event-listeners.spec.ts @@ -0,0 +1,53 @@ +import { Logger } from '@nestjs/common'; +import { Test, TestingModule } from '@nestjs/testing'; +import { Job } from 'bull'; +import { BoardType } from '../dto/types'; +import { SlackCommunicationEventListeners } from './slack-communication-event-listeners'; +import { TeamDto } from 'src/modules/communication/dto/team.dto'; + +const BoardTypeMock = { + id: 1, + data: {} +}; + +describe('SlackCommunicationEventListeners', () => { + let listener: SlackCommunicationEventListeners; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [SlackCommunicationEventListeners] + }).compile(); + listener = module.get>( + SlackCommunicationEventListeners + ); + + listener.logger = new Logger(); + }); + + beforeEach(() => { + jest.clearAllMocks(); + jest.restoreAllMocks(); + }); + + it('should be defined', () => { + expect(listener).toBeDefined(); + }); + + it('should call logger.verbose onCompleted', () => { + const spyLogger = jest.spyOn(Logger.prototype, 'verbose'); + listener.onCompleted(BoardTypeMock as unknown as Job, undefined); + expect(spyLogger).toHaveBeenNthCalledWith(1, expect.stringContaining('1')); + }); + + it('should call logger.verbose onFailed', () => { + const spyLogger = jest.spyOn(Logger.prototype, 'error').mockImplementation(jest.fn()); + listener.onFailed(BoardTypeMock as unknown as Job, 'someError'); + expect(spyLogger).toHaveBeenCalledTimes(1); + }); + + it('should call logger.error onError', () => { + const spyLogger = jest.spyOn(Logger.prototype, 'error').mockImplementation(jest.fn()); + listener.onError(new Error('someError')); + expect(spyLogger).toHaveBeenCalledTimes(1); + }); +}); From 687e76006202e1cb31fa3e871aac4e25137eff55 Mon Sep 17 00:00:00 2001 From: GoncaloCanteiro Date: Wed, 15 Mar 2023 17:58:02 +0000 Subject: [PATCH 19/19] fix: change slack-communication boardType name --- .../consumers/slack-communication.consumer.spec.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/backend/src/modules/communication/consumers/slack-communication.consumer.spec.ts b/backend/src/modules/communication/consumers/slack-communication.consumer.spec.ts index 2ead7eeba..fd940d018 100644 --- a/backend/src/modules/communication/consumers/slack-communication.consumer.spec.ts +++ b/backend/src/modules/communication/consumers/slack-communication.consumer.spec.ts @@ -11,7 +11,7 @@ import { Logger } from '@nestjs/common'; import { UserFactory } from 'src/libs/test-utils/mocks/factories/user-factory'; import { TeamDto } from 'src/modules/communication/dto/team.dto'; -const mergeBoardTypeMock = { +const BoardTypeMock = { id: 1, data: { id: 'someId', @@ -73,13 +73,13 @@ describe('SlackCommunicationConsumer', () => { describe('communication', () => { it('should call application.execute once with job.data', async () => { - await consumer.communication(mergeBoardTypeMock as unknown as Job); - expect(communicationAppMock.execute).toHaveBeenNthCalledWith(1, mergeBoardTypeMock.data); + await consumer.communication(BoardTypeMock as unknown as Job); + expect(communicationAppMock.execute).toHaveBeenNthCalledWith(1, BoardTypeMock.data); }); it('should call Logger when type BOARD', async () => { const spyLogger = jest.spyOn(Logger.prototype, 'verbose'); - await consumer.communication(mergeBoardTypeMock as unknown as Job); + await consumer.communication(BoardTypeMock as unknown as Job); expect(spyLogger).toBeCalledTimes(1); }); }); @@ -88,14 +88,14 @@ describe('SlackCommunicationConsumer', () => { it('should call Logger once', async () => { const spyLogger = jest.spyOn(Logger.prototype, 'verbose'); await consumer.onCompleted( - mergeBoardTypeMock as unknown as Job, + BoardTypeMock as unknown as Job, result as unknown as TeamDto[] ); expect(spyLogger).toBeCalledTimes(1); }); it('should call updateBoardService once', async () => { await consumer.onCompleted( - mergeBoardTypeMock as unknown as Job, + BoardTypeMock as unknown as Job, result as unknown as TeamDto[] ); expect(updateBoardServiceMock.updateChannelId).toBeCalledTimes(1);