Skip to content

Commit

Permalink
N21-2020 fixes media board error when tool was deleted (#5058)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnegns authored Jun 10, 2024
1 parent 0d47762 commit e04ca4f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { ObjectId } from '@mikro-orm/mongodb';
import { ExternalToolService } from '@modules/tool';
import { CustomParameterScope, ToolContextType } from '@modules/tool/common/enum';
import { ContextExternalToolService } from '@modules/tool/context-external-tool';
Expand Down Expand Up @@ -66,32 +67,35 @@ describe(MediaAvailableLineService.name, () => {
});

describe('getUnusedAvailableSchoolExternalTools', () => {
const setup = () => {
const user: User = userFactory.build();
describe('when there are unused tools', () => {
const setup = () => {
const user: User = userFactory.build();

const schoolExternalTool: SchoolExternalTool = schoolExternalToolFactory
.withSchoolId(user.school.id)
.buildWithId();
const usedSchoolExternalTool: SchoolExternalTool = schoolExternalToolFactory
.withSchoolId(user.school.id)
.buildWithId();
const schoolExternalTool: SchoolExternalTool = schoolExternalToolFactory
.withSchoolId(user.school.id)
.buildWithId();
const usedSchoolExternalTool: SchoolExternalTool = schoolExternalToolFactory
.withSchoolId(user.school.id)
.buildWithId();

const usedContextExternalTool: ContextExternalTool = contextExternalToolFactory
.withSchoolExternalToolRef(usedSchoolExternalTool.id, user.school.id)
.buildWithId();
const usedContextExternalTool: ContextExternalTool = contextExternalToolFactory
.withSchoolExternalToolRef(usedSchoolExternalTool.id, user.school.id)
.buildWithId();

const mediaExternalToolElement: MediaExternalToolElement = mediaExternalToolElementFactory.build({
contextExternalToolId: usedContextExternalTool.id,
});
const board: MediaBoard = mediaBoardFactory.addChild(mediaExternalToolElement).build();
const mediaExternalToolElement: MediaExternalToolElement = mediaExternalToolElementFactory.build({
contextExternalToolId: usedContextExternalTool.id,
});
const board: MediaBoard = mediaBoardFactory.addChild(mediaExternalToolElement).build();

schoolExternalToolService.findSchoolExternalTools.mockResolvedValue([schoolExternalTool, usedSchoolExternalTool]);
contextExternalToolService.findByIdOrFail.mockResolvedValueOnce(usedContextExternalTool);
schoolExternalToolService.findSchoolExternalTools.mockResolvedValue([
schoolExternalTool,
usedSchoolExternalTool,
]);
contextExternalToolService.findById.mockResolvedValueOnce(usedContextExternalTool);

return { user, board, mediaExternalToolElement, schoolExternalTool };
};
return { user, board, mediaExternalToolElement, schoolExternalTool };
};

describe('when there are unused tools', () => {
it('should call the service to get school external tools for users school', async () => {
const { user, board } = setup();

Expand All @@ -108,7 +112,7 @@ describe(MediaAvailableLineService.name, () => {

await service.getUnusedAvailableSchoolExternalTools(user, board);

expect(contextExternalToolService.findByIdOrFail).toHaveBeenCalledWith(
expect(contextExternalToolService.findById).toHaveBeenCalledWith(
mediaExternalToolElement.contextExternalToolId
);
});
Expand All @@ -124,6 +128,56 @@ describe(MediaAvailableLineService.name, () => {
expect(schoolExternalTools).toEqual([schoolExternalTool]);
});
});

describe('when there are elements on board which has deleted context external tool', () => {
const setup = () => {
const user: User = userFactory.build();

const schoolExternalTool: SchoolExternalTool = schoolExternalToolFactory
.withSchoolId(user.school.id)
.buildWithId();
const usedSchoolExternalTool: SchoolExternalTool = schoolExternalToolFactory
.withSchoolId(user.school.id)
.buildWithId();

const usedContextExternalTool: ContextExternalTool = contextExternalToolFactory
.withSchoolExternalToolRef(usedSchoolExternalTool.id, user.school.id)
.buildWithId();

const mediaExternalToolElement: MediaExternalToolElement = mediaExternalToolElementFactory.build({
contextExternalToolId: usedContextExternalTool.id,
});
const mediaExternalToolElementWithDeletedTool: MediaExternalToolElement = mediaExternalToolElementFactory.build(
{
contextExternalToolId: new ObjectId().toHexString(),
}
);
const board: MediaBoard = mediaBoardFactory
.addChild(mediaExternalToolElement)
.addChild(mediaExternalToolElementWithDeletedTool)
.build();

schoolExternalToolService.findSchoolExternalTools.mockResolvedValue([
schoolExternalTool,
usedSchoolExternalTool,
]);
contextExternalToolService.findById.mockResolvedValueOnce(usedContextExternalTool);
contextExternalToolService.findById.mockResolvedValueOnce(null);

return { user, board, mediaExternalToolElement, schoolExternalTool };
};

it('should return the available tools', async () => {
const { user, board, schoolExternalTool } = setup();

const schoolExternalTools: SchoolExternalTool[] = await service.getUnusedAvailableSchoolExternalTools(
user,
board
);

expect(schoolExternalTools).toEqual([schoolExternalTool]);
});
});
});

describe('getAvailableExternalToolsForSchool', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,17 @@ export class MediaAvailableLineService {
}

private async getContextExternalToolsByBoard(board: MediaBoard): Promise<ContextExternalTool[]> {
const contextExternalTools: Promise<ContextExternalTool>[] = board
const contextExternalTools: Promise<ContextExternalTool | null>[] = board
.getChildrenOfType(MediaExternalToolElement)
.map((element: MediaExternalToolElement) =>
this.contextExternalToolService.findByIdOrFail(element.contextExternalToolId)
this.contextExternalToolService.findById(element.contextExternalToolId)
);

const allContextExternalTools: ContextExternalTool[] = await Promise.all(contextExternalTools);
const notNullContextExternalTools: ContextExternalTool[] = (await Promise.all(contextExternalTools)).filter(
(tool): tool is ContextExternalTool => tool !== null
);

return allContextExternalTools;
return notNullContextExternalTools;
}

public matchTools(
Expand Down

0 comments on commit e04ca4f

Please sign in to comment.