Skip to content

Commit

Permalink
add uc unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
virgilchiriac committed Oct 23, 2023
1 parent e2ec1f3 commit 9f13542
Showing 1 changed file with 78 additions and 6 deletions.
84 changes: 78 additions & 6 deletions apps/server/src/modules/board/uc/submission-item.uc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { Test, TestingModule } from '@nestjs/testing';
import { BoardDoAuthorizable, BoardRoles, UserRoleEnum } from '@shared/domain';
import { BoardDoAuthorizable, BoardRoles, ContentElementType, UserRoleEnum } from '@shared/domain';
import {
fileElementFactory,
richTextElementFactory,
setupEntities,
submissionContainerElementFactory,
submissionItemFactory,
userFactory,
} from '@shared/testing';
import { Logger } from '@src/core/logger';
import { AuthorizationService, Action } from '@modules/authorization';
import { NotFoundException } from '@nestjs/common';
import { Action, AuthorizationService } from '@modules/authorization';
import { BadRequestException, ForbiddenException, NotFoundException } from '@nestjs/common';
import { BoardDoAuthorizableService, ContentElementService, SubmissionItemService } from '../service';
import { SubmissionItemUc } from './submission-item.uc';
import { isFileElementResponse, isRichTextElementResponse } from '../controller/dto';

Check failure on line 17 in apps/server/src/modules/board/uc/submission-item.uc.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint

'isFileElementResponse' is defined but never used

Check failure on line 17 in apps/server/src/modules/board/uc/submission-item.uc.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint

'isRichTextElementResponse' is defined but never used

describe(SubmissionItemUc.name, () => {
let module: TestingModule;
Expand Down Expand Up @@ -235,16 +237,86 @@ describe(SubmissionItemUc.name, () => {
const context = { action: Action.read, requiredPermissions: [] };
expect(authorizationService.checkPermission).toBeCalledWith(user, boardDoAuthorizable, context);
});
it('should throw if user is not creator of submission', async () => {
it('should throw if user is not creator of submission item', async () => {
const user2 = userFactory.buildWithId();
const { submissionItem } = setup();

await expect(uc.updateSubmissionItem(user2.id, submissionItem.id, false)).rejects.toThrow();
await expect(uc.updateSubmissionItem(user2.id, submissionItem.id, false)).rejects.toThrow(
new ForbiddenException()
);
});
it('should call service to update element', async () => {
it('should call service to update submission item', async () => {
const { submissionItem, user } = setup();
await uc.updateSubmissionItem(user.id, submissionItem.id, false);
expect(submissionItemService.update).toHaveBeenCalledWith(submissionItem, false);
});
});

// write tests
describe('createElement', () => {
const setup = () => {
const user = userFactory.buildWithId();
const submissionItem = submissionItemFactory.build({
userId: user.id,
});

submissionItemService.findById.mockResolvedValue(submissionItem);

const element = richTextElementFactory.build();
elementService.create.mockResolvedValueOnce(element);

boardDoAuthorizableService.getBoardAuthorizable.mockResolvedValue(
new BoardDoAuthorizable({
users: [{ userId: user.id, roles: [BoardRoles.READER], userRoleEnum: UserRoleEnum.STUDENT }],
id: submissionItem.id,
})
);

return { submissionItem, user };
};

it('should call service to find the submission item ', async () => {
const { submissionItem, user } = setup();
await uc.createElement(user.id, submissionItem.id, ContentElementType.RICH_TEXT);
expect(submissionItemService.findById).toHaveBeenCalledWith(submissionItem.id);
});

it('should authorize', async () => {
const { submissionItem, user } = setup();

const boardDoAuthorizable = await boardDoAuthorizableService.getBoardAuthorizable(submissionItem);

await uc.createElement(user.id, submissionItem.id, ContentElementType.RICH_TEXT);
const context = { action: Action.read, requiredPermissions: [] };
expect(authorizationService.checkPermission).toBeCalledWith(user, boardDoAuthorizable, context);
});

it('should throw if user is not creator of submission item', async () => {
const user2 = userFactory.buildWithId();
const { submissionItem } = setup();

await expect(uc.createElement(user2.id, submissionItem.id, ContentElementType.RICH_TEXT)).rejects.toThrow(
new ForbiddenException()
);
});

it('should throw if type is not file or rich text', async () => {
const { submissionItem, user } = setup();
await expect(uc.createElement(user.id, submissionItem.id, ContentElementType.LINK)).rejects.toThrow(
new BadRequestException()
);
});

it('should call service to create element', async () => {
const { submissionItem, user } = setup();
await uc.createElement(user.id, submissionItem.id, ContentElementType.RICH_TEXT);
expect(elementService.create).toHaveBeenCalledWith(submissionItem, ContentElementType.RICH_TEXT);
});

it('should return element', async () => {
const { submissionItem, user } = setup();
const element = await uc.createElement(user.id, submissionItem.id, ContentElementType.RICH_TEXT);
expect(element).toBeDefined();
});
});
});

0 comments on commit 9f13542

Please sign in to comment.