Skip to content

Commit

Permalink
Merge branch 'BC-6871-board-refactor' of github.com:hpi-schul-cloud/s…
Browse files Browse the repository at this point in the history
…chulcloud-server into BC-6871-board-refactor
  • Loading branch information
uidp committed Jun 13, 2024
2 parents 35201e4 + c0a8c28 commit 98a799a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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<BoardNodeRepo>;
let boardNodeService: DeepMocked<BoardNodeService>;

beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
BoardCommonToolService,
{
provide: BoardNodeRepo,
useValue: createMock<BoardNodeRepo>(),
},
{
provide: BoardNodeService,
useValue: createMock<BoardNodeService>(),
},
],
}).compile();

service = module.get<BoardCommonToolService>(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);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ export class BoardCommonToolService {
async findByDescendant(boardNode: AnyBoardNode): Promise<ColumnBoard | MediaBoard> {
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;
}
}

0 comments on commit 98a799a

Please sign in to comment.