From 37f8e3706ef267e2414dc891430be506169196e4 Mon Sep 17 00:00:00 2001 From: Uwe Ilgenstein Date: Thu, 13 Jun 2024 13:34:27 +0200 Subject: [PATCH] add unit tests --- .../column-board-copy.service.spec.ts | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 apps/server/src/modules/board/service/internal/column-board-copy.service.spec.ts diff --git a/apps/server/src/modules/board/service/internal/column-board-copy.service.spec.ts b/apps/server/src/modules/board/service/internal/column-board-copy.service.spec.ts new file mode 100644 index 00000000000..20306b26244 --- /dev/null +++ b/apps/server/src/modules/board/service/internal/column-board-copy.service.spec.ts @@ -0,0 +1,111 @@ +import { createMock, DeepMocked } from '@golevelup/ts-jest'; +import { ObjectId } from '@mikro-orm/mongodb'; +import { UserService } from '@modules/user'; +import { Test, TestingModule } from '@nestjs/testing'; +import { CourseRepo } from '@shared/repo'; +import { courseFactory, setupEntities, userDoFactory } from '@shared/testing'; +import { CopyElementType, CopyStatus, CopyStatusEnum } from '@src/modules/copy-helper'; +import { FilesStorageClientAdapterService } from '@src/modules/files-storage-client'; +import { BoardExternalReferenceType, ColumnBoard } from '../../domain'; +import { columnBoardFactory } from '../../testing'; +import { BoardNodeService } from '../board-node.service'; +import { BoardNodeCopyService } from './board-node-copy.service'; +import { ColumnBoardCopyService } from './column-board-copy.service'; +import { ColumnBoardTitleService } from './column-board-title.service'; + +describe(ColumnBoardCopyService.name, () => { + let module: TestingModule; + let service: ColumnBoardCopyService; + + let boardNodeService: DeepMocked; + // let columnBoardTitleService: DeepMocked; + let courseRepo: DeepMocked; + let userService: DeepMocked; + let boardNodeCopyService: DeepMocked; + // let filesStorageClientAdapterService: DeepMocked; + + beforeAll(async () => { + module = await Test.createTestingModule({ + providers: [ + ColumnBoardCopyService, + { + provide: BoardNodeService, + useValue: createMock(), + }, + { + provide: ColumnBoardTitleService, + useValue: createMock(), + }, + { + provide: CourseRepo, + useValue: createMock(), + }, + { + provide: UserService, + useValue: createMock(), + }, + { + provide: BoardNodeCopyService, + useValue: createMock(), + }, + { + provide: FilesStorageClientAdapterService, + useValue: createMock(), + }, + ], + }).compile(); + + service = module.get(ColumnBoardCopyService); + boardNodeService = module.get(BoardNodeService); + courseRepo = module.get(CourseRepo); + userService = module.get(UserService); + boardNodeCopyService = module.get(BoardNodeCopyService); + + await setupEntities(); + }); + + afterEach(() => { + jest.resetAllMocks(); + }); + + afterAll(async () => { + await module.close(); + }); + + const setup = () => { + const userId = new ObjectId().toHexString(); + const user = userDoFactory.build({ id: userId }); + userService.findById.mockResolvedValueOnce(user); + const course = courseFactory.buildWithId(); + courseRepo.findById.mockResolvedValueOnce(course); + const originalBoard = columnBoardFactory.build({ + context: { id: course.id, type: BoardExternalReferenceType.Course }, + }); + boardNodeService.findByClassAndId.mockResolvedValueOnce(originalBoard); + const boardCopy = columnBoardFactory.build({ + context: { id: course.id, type: BoardExternalReferenceType.Course }, + }); + const status: CopyStatus = { + copyEntity: boardCopy, + type: CopyElementType.BOARD, + status: CopyStatusEnum.SUCCESS, + }; + boardNodeCopyService.copy.mockResolvedValueOnce(status); + + return { originalBoard, userId }; + }; + + it('should copy the board', async () => { + const { originalBoard, userId } = setup(); + + const result = await service.copyColumnBoard({ + originalColumnBoardId: originalBoard.id, + destinationExternalReference: originalBoard.context, + userId, + copyTitle: 'Another Title', + }); + + expect(boardNodeCopyService.copy).toHaveBeenCalled(); + expect((result.copyEntity as ColumnBoard).title).toBe('Another Title'); + }); +});