Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
psachmann committed Jan 21, 2025
1 parent 9a50658 commit d5f18b0
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { EntityId } from '@shared/domain/types';
import { StorageLocation } from '@src/modules/files-storage/interface';
import { ObjectId } from '@mikro-orm/mongodb';
import { CopyElementType, CopyStatus, CopyStatusEnum } from '../../copy-helper';
import { BoardExternalReference, BoardExternalReferenceType, ColumnBoard } from '../domain';
import { BoardExternalReference, BoardExternalReferenceType, ColumnBoard, ColumnBoardProps } from '../domain';
import { BoardNodeRepo } from '../repo';
import { columnBoardFactory } from '../testing';
import { BoardNodeService } from './board-node.service';
Expand Down Expand Up @@ -131,4 +131,24 @@ describe('ColumnBoardService', () => {

expect(result).toEqual(columnBoard);
});

describe('createColumnBoard', () => {
describe('when creating new ColumnBoard', () => {
const setup = () => {
const columnBoard = columnBoardFactory.build() as unknown as ColumnBoardProps;

repo.save.mockResolvedValue();

return { columnBoard };
};

it('should call BoardNodeRepo', async () => {
const { columnBoard } = setup();

await service.createColumnBoard(columnBoard);

expect(repo.save).toHaveBeenCalledTimes(1);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@ import { faker } from '@faker-js/faker';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { CoursesClientAdapter } from '@infra/courses-client';
import { Test, TestingModule } from '@nestjs/testing';
import { BoardsClientAdapter } from '@infra/boards-client';
import type { CommonCartridgeFileParser } from '../import/common-cartridge-file-parser';
import { CommonCartridgeImportService } from './common-cartridge-import.service';

jest.mock('../import/common-cartridge-file-parser', () => {
const fileParserMock = createMock<CommonCartridgeFileParser>();

fileParserMock.getTitle.mockReturnValue(faker.lorem.words());
fileParserMock.getOrganizations.mockReturnValue([
{
pathDepth: 0,
title: faker.lorem.words(),
path: faker.system.filePath(),
identifier: faker.string.uuid(),
isInlined: true,
isResource: false,
resourcePath: faker.system.filePath(),
resourceType: faker.lorem.word(),
},
]);

return {
CommonCartridgeFileParser: jest.fn(() => fileParserMock),
Expand All @@ -19,6 +32,7 @@ describe(CommonCartridgeImportService.name, () => {
let module: TestingModule;
let sut: CommonCartridgeImportService;
let coursesClientAdapterMock: DeepMocked<CoursesClientAdapter>;
let boardsClientAdapterMock: DeepMocked<BoardsClientAdapter>;

beforeEach(async () => {
module = await Test.createTestingModule({
Expand All @@ -28,11 +42,16 @@ describe(CommonCartridgeImportService.name, () => {
provide: CoursesClientAdapter,
useValue: createMock<CoursesClientAdapter>(),
},
{
provide: BoardsClientAdapter,
useValue: createMock<BoardsClientAdapter>(),
},
],
}).compile();

sut = module.get(CommonCartridgeImportService);
coursesClientAdapterMock = module.get(CoursesClientAdapter);
boardsClientAdapterMock = module.get(BoardsClientAdapter);
});

afterEach(async () => {
Expand All @@ -49,11 +68,27 @@ describe(CommonCartridgeImportService.name, () => {

describe('importFile', () => {
describe('when importing a file', () => {
const setup = () => {
const file = Buffer.from('');

return { file };
};

it('should create a course', async () => {
await sut.importFile(Buffer.from(''));
const { file } = setup();

await sut.importFile(file);

expect(coursesClientAdapterMock.createCourse).toHaveBeenCalledWith({ title: expect.any(String) });
});

it('should create boards', async () => {
const { file } = setup();

await sut.importFile(file);

expect(boardsClientAdapterMock.createBoard).toHaveBeenCalledTimes(1);
});
});
});
});
26 changes: 26 additions & 0 deletions apps/server/src/modules/learnroom/mapper/course.mapper.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { courseFactory } from '@src/testing/factory/course.factory';
import { setupEntities } from '@src/testing/setup-entities';
import { CourseMapper } from './course.mapper';
import { CreateCourseResponse } from '../controller/dto';

describe(CourseMapper.name, () => {
beforeAll(async () => {
await setupEntities();
});

describe('mapToCreateCourseResponse', () => {
const setup = () => {
const course = courseFactory.build();

return { course };
};

it('should return CreateCourseResponse', () => {
const { course } = setup();

const result = CourseMapper.mapToCreateCourseResponse(course);

expect(result).toEqual(new CreateCourseResponse({ courseId: course.id }));
});
});
});
4 changes: 3 additions & 1 deletion apps/server/src/modules/learnroom/mapper/course.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export class CourseMapper {
}

public static mapToCreateCourseResponse(course: Course): CreateCourseResponse {
return new CreateCourseResponse({ courseId: course.id });
const response = new CreateCourseResponse({ courseId: course.id });

return response;
}
}
20 changes: 20 additions & 0 deletions apps/server/src/modules/learnroom/service/course.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,24 @@ describe('CourseService', () => {
});
});
});

describe('create', () => {
describe('when creating new Course', () => {
const setup = () => {
const course = courseFactory.buildWithId();

legacyCourseRepo.createCourse.mockResolvedValueOnce(course);

return { course };
};

it('should call createCourse from course repository', async () => {
const { course } = setup();

await expect(courseService.create(course)).resolves.not.toThrow();

expect(legacyCourseRepo.createCourse).toBeCalledWith(course);
});
});
});
});
4 changes: 3 additions & 1 deletion apps/server/src/modules/learnroom/service/course.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export class CourseService implements DeletionService, IEventHandler<UserDeleted
}

public findById(courseId: EntityId): Promise<CourseEntity> {
return this.repo.findById(courseId);
const course = this.repo.findById(courseId);

return course;
}

public async findAllCoursesByUserId(userId: EntityId): Promise<Counted<CourseEntity[]>> {
Expand Down

0 comments on commit d5f18b0

Please sign in to comment.