Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinOehlerkingCap committed Sep 20, 2023
1 parent dceef23 commit a020c07
Show file tree
Hide file tree
Showing 16 changed files with 295 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { BoardExternalReferenceType, ContentElementType, RichTextElementNode } from '@shared/domain';
import {
TestApiClient,
UserAndAccountTestFactory,
cardNodeFactory,
cleanupCollections,
columnBoardNodeFactory,
columnNodeFactory,
courseFactory,
TestApiClient,
UserAndAccountTestFactory,
} from '@shared/testing';
import { ServerTestModule } from '@src/modules/server/server.module';
import { AnyContentElementResponse } from '../dto';
Expand Down Expand Up @@ -83,6 +83,14 @@ describe(`content element create (api)`, () => {
expect((response.body as AnyContentElementResponse).type).toEqual(ContentElementType.FILE);
});

it('should return the created content element of type EXTERNAL_TOOL', async () => {
const { loggedInClient, cardNode } = await setup();

const response = await loggedInClient.post(`${cardNode.id}/elements`, { type: ContentElementType.EXTERNAL_TOOL });

expect((response.body as AnyContentElementResponse).type).toEqual(ContentElementType.EXTERNAL_TOOL);
});

it('should return the created content element of type SUBMISSION_CONTAINER', async () => {
const { loggedInClient, cardNode } = await setup();

Expand Down
24 changes: 22 additions & 2 deletions apps/server/src/modules/board/repo/board-do.builder-impl.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { BoardNodeType } from '@shared/domain';
import { BoardNodeType, ExternalToolElement } from '@shared/domain';
import {
cardNodeFactory,
columnBoardNodeFactory,
columnNodeFactory,
externalToolElementNodeFactory,
fileElementNodeFactory,
richTextElementNodeFactory,
submissionContainerElementNodeFactory,
setupEntities,
submissionContainerElementNodeFactory,
} from '@shared/testing';
import { BoardDoBuilderImpl } from './board-do.builder-impl';

Expand Down Expand Up @@ -185,6 +186,25 @@ describe(BoardDoBuilderImpl.name, () => {
});
});

describe('when building a external tool element', () => {
it('should work without descendants', () => {
const externalToolElementNode = externalToolElementNodeFactory.build();

const domainObject = new BoardDoBuilderImpl().buildExternalToolElement(externalToolElementNode);

expect(domainObject.constructor.name).toBe(ExternalToolElement.name);
});

it('should throw error if submissionContainerElement is not a leaf', () => {
const externalToolElementNode = externalToolElementNodeFactory.buildWithId();
const columnNode = columnNodeFactory.buildWithId({ parent: externalToolElementNode });

expect(() => {
new BoardDoBuilderImpl([columnNode]).buildExternalToolElement(externalToolElementNode);
}).toThrowError();
});
});

describe('ensure board node types', () => {
it('should do nothing if type is correct', () => {
const card = cardNodeFactory.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { DeepMocked, createMock } from '@golevelup/ts-jest';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { EntityManager } from '@mikro-orm/mongodb';
import { Test, TestingModule } from '@nestjs/testing';
import { FileRecordParentType } from '@shared/infra/rabbitmq';
import {
columnBoardFactory,
columnFactory,
externalToolElementFactory,
fileElementFactory,
setupEntities,
submissionContainerElementFactory,
Expand Down Expand Up @@ -187,4 +188,26 @@ describe(RecursiveDeleteVisitor.name, () => {
expect(em.remove).toHaveBeenCalledWith(em.getReference(childSubmissionItem.constructor, childSubmissionItem.id));
});
});

describe('visitExternalToolElementAsync', () => {
const setup = () => {
const childExternalToolElement = externalToolElementFactory.build();
const externalToolElement = externalToolElementFactory.build({
children: [childExternalToolElement],
});

return { externalToolElement, childExternalToolElement };
};

it('should call entity remove', async () => {
const { externalToolElement, childExternalToolElement } = setup();

await service.visitExternalToolElementAsync(externalToolElement);

expect(em.remove).toHaveBeenCalledWith(em.getReference(externalToolElement.constructor, externalToolElement.id));
expect(em.remove).toHaveBeenCalledWith(
em.getReference(childExternalToolElement.constructor, childExternalToolElement.id)
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CardNode,
ColumnBoardNode,
ColumnNode,
ExternalToolElementNodeEntity,
FileElementNode,
RichTextElementNode,
SubmissionContainerElementNode,
Expand All @@ -15,8 +16,11 @@ import {
columnBoardFactory,
columnBoardNodeFactory,
columnFactory,
contextExternalToolEntityFactory,
externalToolElementFactory,
fileElementFactory,
richTextElementFactory,
setupEntities,
submissionContainerElementFactory,
submissionItemFactory,
} from '@shared/testing';
Expand All @@ -28,10 +32,12 @@ describe(RecursiveSaveVisitor.name, () => {
let em: DeepMocked<EntityManager>;
let boardNodeRepo: DeepMocked<BoardNodeRepo>;

beforeAll(() => {
beforeAll(async () => {
em = createMock<EntityManager>();
boardNodeRepo = createMock<BoardNodeRepo>();

await setupEntities();

visitor = new RecursiveSaveVisitor(em, boardNodeRepo);
});

Expand Down Expand Up @@ -178,6 +184,25 @@ describe(RecursiveSaveVisitor.name, () => {
});
});

describe('when visiting a external tool element', () => {
it('should create or update the node', () => {
const contextExternalTool = contextExternalToolEntityFactory.buildWithId();
const externalToolElement = externalToolElementFactory.build({
contextExternalToolId: contextExternalTool.id,
});
jest.spyOn(visitor, 'createOrUpdateBoardNode');

visitor.visitExternalToolElement(externalToolElement);

const expectedNode: Partial<ExternalToolElementNodeEntity> = {
id: externalToolElement.id,
type: BoardNodeType.EXTERNAL_TOOL,
contextExternalTool,
};
expect(visitor.createOrUpdateBoardNode).toHaveBeenCalledWith(expect.objectContaining(expectedNode));
});
});

describe('createOrUpdateBoardNode', () => {
describe('when the board is new', () => {
it('should persist the board node', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { ObjectId } from '@mikro-orm/mongodb';
import { InputFormat } from '@shared/domain';
import {
cardFactory,
columnBoardFactory,
columnFactory,
externalToolElementFactory,
fileElementFactory,
richTextElementFactory,
submissionContainerElementFactory,
submissionItemFactory,
} from '@shared/testing';
import { FileContentBody, RichTextContentBody } from '../controller/dto';
import { ExternalToolContentBody, FileContentBody, RichTextContentBody } from '../controller/dto';
import { ContentElementUpdateVisitor } from './content-element-update.visitor';

describe(ContentElementUpdateVisitor.name, () => {
Expand Down Expand Up @@ -107,4 +109,59 @@ describe(ContentElementUpdateVisitor.name, () => {
expect(() => updater.visitSubmissionContainerElement(submissionContainerElement)).toThrow();
});
});

describe('when visiting a external tool element', () => {
describe('when visiting a external tool element with valid content', () => {
const setup = () => {
const externalToolElement = externalToolElementFactory.build({ contextExternalToolId: undefined });
const content = new ExternalToolContentBody();
content.contextExternalToolId = new ObjectId().toHexString();
const updater = new ContentElementUpdateVisitor(content);

return { externalToolElement, updater, content };
};

it('should update the content', () => {
const { externalToolElement, updater, content } = setup();

updater.visitExternalToolElement(externalToolElement);

expect(externalToolElement.contextExternalToolId).toEqual(content.contextExternalToolId);
});
});

describe('when visiting a external tool element using the wrong content', () => {
const setup = () => {
const externalToolElement = externalToolElementFactory.build();
const content = new RichTextContentBody();
content.text = 'a text';
content.inputFormat = InputFormat.RICH_TEXT_CK5;
const updater = new ContentElementUpdateVisitor(content);

return { externalToolElement, updater };
};

it('should throw an error', () => {
const { externalToolElement, updater } = setup();

expect(() => updater.visitExternalToolElement(externalToolElement)).toThrow();
});
});

describe('when visiting a external tool element without setting a contextExternalId', () => {
const setup = () => {
const externalToolElement = externalToolElementFactory.build();
const content = new ExternalToolContentBody();
const updater = new ContentElementUpdateVisitor(content);

return { externalToolElement, updater };
};

it('should throw an error', () => {
const { externalToolElement, updater } = setup();

expect(() => updater.visitExternalToolElement(externalToolElement)).toThrow();
});
});
});
});
13 changes: 12 additions & 1 deletion apps/server/src/shared/domain/domainobject/board/card.do.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { createMock } from '@golevelup/ts-jest';
import { cardFactory, richTextElementFactory, submissionContainerElementFactory } from '@shared/testing';
import {
cardFactory,
externalToolElementFactory,
richTextElementFactory,
submissionContainerElementFactory,
} from '@shared/testing';
import { Card } from './card.do';
import { BoardCompositeVisitor, BoardCompositeVisitorAsync } from './types';

Expand All @@ -16,6 +21,12 @@ describe(Card.name, () => {
const submissionContainerElement = submissionContainerElementFactory.build();
expect(card.isAllowedAsChild(submissionContainerElement)).toBe(true);
});

it('should allow external tool element objects', () => {
const card = cardFactory.build();
const externalToolElement = externalToolElementFactory.build();
expect(card.isAllowedAsChild(externalToolElement)).toBe(true);
});
});

describe('accept', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { NotImplementedException } from '@nestjs/common';
import { ContentElementFactory } from './content-element.factory';
import { ExternalToolElement } from './external-tool-element.do';
import { FileElement } from './file-element.do';
import { RichTextElement } from './rich-text-element.do';
import { SubmissionContainerElement } from './submission-container-element.do';
import { ContentElementType } from './types/content-elements.enum';
import { ContentElementType } from './types';

describe(ContentElementFactory.name, () => {
describe('build', () => {
Expand Down Expand Up @@ -37,6 +38,14 @@ describe(ContentElementFactory.name, () => {
expect(element).toBeInstanceOf(SubmissionContainerElement);
});

it('should return element of EXTERNAL_TOOL', () => {
const { contentElementFactory } = setup();

const element = contentElementFactory.build(ContentElementType.EXTERNAL_TOOL);

expect(element).toBeInstanceOf(ExternalToolElement);
});

it('should throw NotImplementedException', () => {
const { contentElementFactory } = setup();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { createMock } from '@golevelup/ts-jest';
import { ObjectId } from '@mikro-orm/mongodb';
import { externalToolElementFactory } from '@shared/testing';
import { ExternalToolElement } from './external-tool-element.do';
import { BoardCompositeVisitor, BoardCompositeVisitorAsync } from './types';

describe(ExternalToolElement.name, () => {
describe('when trying to add a child to a external tool element', () => {
it('should throw an error ', () => {
const externalToolElement = externalToolElementFactory.build();
const externalToolElementChild = externalToolElementFactory.build();

expect(() => externalToolElement.addChild(externalToolElementChild)).toThrow();
});
});

describe('update contextExternalToolId', () => {
it('should be able to update contextExternalToolId', () => {
const externalToolElement = externalToolElementFactory.build();
const contextExternalToolId = new ObjectId().toHexString();

externalToolElement.contextExternalToolId = contextExternalToolId;

expect(externalToolElement.contextExternalToolId).toEqual(contextExternalToolId);
});
});

describe('accept', () => {
it('should call the right visitor method', () => {
const visitor = createMock<BoardCompositeVisitor>();
const externalToolElement = externalToolElementFactory.build();

externalToolElement.accept(visitor);

expect(visitor.visitExternalToolElement).toHaveBeenCalledWith(externalToolElement);
});
});

describe('acceptAsync', () => {
it('should call the right async visitor method', async () => {
const visitor = createMock<BoardCompositeVisitorAsync>();
const externalToolElement = externalToolElementFactory.build();

await externalToolElement.acceptAsync(visitor);

expect(visitor.visitExternalToolElementAsync).toHaveBeenCalledWith(externalToolElement);
});
});
});
Loading

0 comments on commit a020c07

Please sign in to comment.