From ff52c783cf47a55ac9f65e7974b016537ad5bcd1 Mon Sep 17 00:00:00 2001 From: virgilchiriac Date: Thu, 13 Jun 2024 14:04:53 +0200 Subject: [PATCH] fix bug and add test for bord common tool --- .../service/board-common-tool.service.spec.ts | 91 +++++++++++++++++++ .../service/board-common-tool.service.ts | 4 +- 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 apps/server/src/modules/board/service/board-common-tool.service.spec.ts diff --git a/apps/server/src/modules/board/service/board-common-tool.service.spec.ts b/apps/server/src/modules/board/service/board-common-tool.service.spec.ts new file mode 100644 index 00000000000..22c07d0f01d --- /dev/null +++ b/apps/server/src/modules/board/service/board-common-tool.service.spec.ts @@ -0,0 +1,91 @@ +import { createMock, DeepMocked } from '@golevelup/ts-jest'; +import { Test, TestingModule } from '@nestjs/testing'; +import { NotFoundException } from '@nestjs/common'; +import { contextExternalToolFactory } from '@modules/tool/context-external-tool/testing'; +import { BoardCommonToolService } from './board-common-tool.service'; +import { BoardNodeRepo } from '../repo'; +import { BoardNodeService } from './board-node.service'; +import { ColumnBoard, MediaBoard, AnyBoardNode } from '../domain'; + +import { columnBoardFactory, mediaBoardFactory } from '../testing'; + +describe('BoardCommonToolService', () => { + let service: BoardCommonToolService; + let boardNodeRepo: DeepMocked; + let boardNodeService: DeepMocked; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + BoardCommonToolService, + { + provide: BoardNodeRepo, + useValue: createMock(), + }, + { + provide: BoardNodeService, + useValue: createMock(), + }, + ], + }).compile(); + + service = module.get(BoardCommonToolService); + boardNodeRepo = module.get(BoardNodeRepo); + boardNodeService = module.get(BoardNodeService); + }); + + beforeEach(() => { + jest.resetAllMocks(); + }); + + describe('countBoardUsageForExternalTools', () => { + const setup = () => { + const contextExternalTools = [contextExternalToolFactory.build(), contextExternalToolFactory.build()]; + + const boardNodes: AnyBoardNode[] = [ + { rootId: '1' } as AnyBoardNode, + { rootId: '2' } as AnyBoardNode, + { rootId: '1' } as AnyBoardNode, + ]; + boardNodeRepo.findByContextExternalToolIds.mockResolvedValue(boardNodes); + + return { contextExternalTools }; + }; + it('should count board usage for external tools', async () => { + const { contextExternalTools } = setup(); + const result = await service.countBoardUsageForExternalTools(contextExternalTools); + + expect(result).toBe(2); + }); + }); + + describe('findByDescendant', () => { + it('should return the root node when it is a ColumnBoard', async () => { + const boardNode: AnyBoardNode = { id: '1', rootId: '2' } as AnyBoardNode; + const rootNode: ColumnBoard = columnBoardFactory.build(); + boardNodeService.findRoot.mockResolvedValue(rootNode); + + const result = await service.findByDescendant(boardNode); + + expect(result).toBe(rootNode); + }); + + it('should return the root node when it is a MediaBoard', async () => { + const boardNode: AnyBoardNode = { id: '1', rootId: '2' } as AnyBoardNode; + const rootNode: MediaBoard = mediaBoardFactory.build(); + boardNodeService.findRoot.mockResolvedValue(rootNode); + + const result = await service.findByDescendant(boardNode); + + expect(result).toBe(rootNode); + }); + + it('should throw NotFoundException when root node is not a ColumnBoard or MediaBoard', async () => { + const boardNode: AnyBoardNode = { id: '1', rootId: '2' } as AnyBoardNode; + const rootNode: AnyBoardNode = { id: '2' } as AnyBoardNode; + boardNodeService.findRoot.mockResolvedValue(rootNode); + + await expect(service.findByDescendant(boardNode)).rejects.toThrow(NotFoundException); + }); + }); +}); diff --git a/apps/server/src/modules/board/service/board-common-tool.service.ts b/apps/server/src/modules/board/service/board-common-tool.service.ts index cb717ccb529..684b7e39849 100644 --- a/apps/server/src/modules/board/service/board-common-tool.service.ts +++ b/apps/server/src/modules/board/service/board-common-tool.service.ts @@ -26,10 +26,10 @@ export class BoardCommonToolService { async findByDescendant(boardNode: AnyBoardNode): Promise { const rootNode = await this.boardNodeService.findRoot(boardNode); - if (!isColumnBoard(boardNode) && !isMediaBoard(boardNode)) { + if (!isColumnBoard(rootNode) && !isMediaBoard(rootNode)) { throw new NotFoundException(`There is no board with this id`); } - return rootNode as ColumnBoard | MediaBoard; + return rootNode; } }