Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Oberndörfer <[email protected]>
  • Loading branch information
flo0852 committed Dec 5, 2024
1 parent 8b1e629 commit c140f65
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 2 deletions.
26 changes: 25 additions & 1 deletion apps/backend/src/app/backupData/backupData.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ILike, Repository } from 'typeorm';
import { BackupDataEntity } from './entity/backupData.entity';
import { CreateBackupDataDto } from './dto/createBackupData.dto';
import { BackupDataModule } from './backupData.module';
Expand Down Expand Up @@ -139,4 +139,28 @@ describe('BackupDataController (e2e)', () => {
where: { creationDate: expect.any(Object), type: BackupType.FULL },
});
});
it('/backupData (GET) with taskId should return backup data entries with the specified taskId', async () => {
await request(app.getHttpServer())
.get('/backupData?taskId=task-123')
.expect(200);

expect(mockBackupDataRepository.findAndCount).toBeCalledWith({
order: { creationDate: 'DESC' },
where: { taskId: { id: 'task-123' }, type: BackupType.FULL },
});
});

it('/backupData (GET) with taskName should return backup data entries with the specified taskName', async () => {
await request(app.getHttpServer())
.get('/backupData?taskName=backup-task')
.expect(200);

expect(mockBackupDataRepository.findAndCount).toBeCalledWith({
order: { creationDate: 'DESC' },
where: {
taskId: { displayName: ILike('%backup-task%') },
type: BackupType.FULL,
},
});
});
});
21 changes: 20 additions & 1 deletion apps/backend/src/app/backupData/backupData.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ describe('BackupDataService', () => {
BadRequestException
);
});

it('should create a where clause for taskId search', () => {
const filterDto: BackupDataFilterDto = { taskId: 'task-123' };
const where = service.createWhereClause(filterDto);
expect(where).toEqual({
taskId: { id: 'task-123' },
type: BackupType.FULL,
});
});

it('should create a where clause for taskName search', () => {
const filterDto: BackupDataFilterDto = { taskName: 'backup-task' };
const where = service.createWhereClause(filterDto);
expect(where).toEqual({
taskId: { displayName: ILike('%backup-task%') },
type: BackupType.FULL,
});
});
});

describe('createOrderClause', () => {
Expand Down Expand Up @@ -151,7 +169,8 @@ describe('BackupDataService', () => {

describe('create', () => {
it('should create a new backup data entity', async () => {
let createBackupDataDto: CreateBackupDataDto = new CreateBackupDataDto();
const createBackupDataDto: CreateBackupDataDto =
new CreateBackupDataDto();
Object.assign(createBackupDataDto, mockBackupDataEntity);

const result = await service.create(createBackupDataDto);
Expand Down
89 changes: 89 additions & 0 deletions apps/backend/src/app/tasks/tasks.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { TasksController } from './tasks.controller';
import { TasksService } from './tasks.service';
import { getRepositoryToken } from '@nestjs/typeorm';
import { TaskEntity } from './entity/task.entity';
import { Repository } from 'typeorm';
import { CreateTaskDto } from './dto/createTask.dto';

describe('TasksController (e2e)', () => {
let app: INestApplication;
let repository: Repository<TaskEntity>;

const mockTaskRepository = {
find: jest.fn(),
findOneBy: jest.fn(),
save: jest.fn(),
};

beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
controllers: [TasksController],
providers: [
TasksService,
{
provide: getRepositoryToken(TaskEntity),
useValue: mockTaskRepository,
},
],
}).compile();

app = moduleFixture.createNestApplication();
await app.init();
repository = moduleFixture.get<Repository<TaskEntity>>(getRepositoryToken(TaskEntity));
});

afterAll(async () => {
await app.close();
});

it('/tasks (GET) should return an array of tasks', async () => {
const result = [new TaskEntity()];
jest.spyOn(repository, 'find').mockResolvedValue(result);

const response = await request(app.getHttpServer())
.get('/tasks')
.expect(200);

expect(response.body).toEqual(result);
});

it('/tasks/:id (GET) should return a task if found', async () => {
const id = 'ea1a2f52-5cf4-44a6-b266-175ee396a18c';
const task = new TaskEntity();
jest.spyOn(repository, 'findOneBy').mockResolvedValue(task);

const response = await request(app.getHttpServer())
.get(`/tasks/${id}`)
.expect(200);

expect(response.body).toEqual(task);
});

it('/tasks/:id (GET) should throw a NotFoundException if task not found', async () => {
const id = 'ea1a2f52-5cf4-44a6-b266-175ee396a18e';
jest.spyOn(repository, 'findOneBy').mockResolvedValue(null);

await request(app.getHttpServer())
.get(`/tasks/${id}`)
.expect(404);
});

it('/tasks (POST) should create and return a task', async () => {
const createTaskDto: CreateTaskDto = {
id: 'someId',
displayName: 'someName',
};
const task = new TaskEntity();
jest.spyOn(repository, 'save').mockResolvedValue(task);

const response = await request(app.getHttpServer())
.post('/tasks')
.send(createTaskDto)
.expect(201);

expect(response.body).toEqual(task);
});
});
73 changes: 73 additions & 0 deletions apps/backend/src/app/tasks/tasks.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { NotFoundException } from '@nestjs/common';
import { Repository } from 'typeorm';
import { TasksService } from './tasks.service';
import { TaskEntity } from './entity/task.entity';

describe('TasksService', () => {
let service: TasksService;
let repository: Repository<TaskEntity>;

const mockTaskRepository = {
find: jest.fn(),
findOneBy: jest.fn(),
save: jest.fn(),
};

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
TasksService,
{
provide: getRepositoryToken(TaskEntity),
useValue: mockTaskRepository,
},
],
}).compile();

service = module.get<TasksService>(TasksService);
repository = module.get<Repository<TaskEntity>>(
getRepositoryToken(TaskEntity)
);
});

it('should be defined', () => {
expect(service).toBeDefined();
});

describe('findAll', () => {
it('should return an array of tasks', async () => {
const result = [new TaskEntity()];
jest.spyOn(repository, 'find').mockResolvedValue(result);

expect(await service.findAll()).toBe(result);
});
});

describe('findOne', () => {
it('should return a task if found', async () => {
const id = 'ea1a2f52-5cf4-44a6-b266-175ee396a18c';
const task = new TaskEntity();
jest.spyOn(repository, 'findOneBy').mockResolvedValue(task);

expect(await service.findOne(id)).toBe(task);
});

it('should throw a NotFoundException if task not found', async () => {
const id = 'ea1a2f52-5cf4-44a6-b266-175ee396a18d';
jest.spyOn(repository, 'findOneBy').mockResolvedValue(null);

await expect(service.findOne(id)).rejects.toThrow(NotFoundException);
});
});

describe('create', () => {
it('should create and return a task', async () => {
const task = new TaskEntity();
jest.spyOn(repository, 'save').mockResolvedValue(task);

expect(await service.create(task)).toBe(task);
});
});
});

0 comments on commit c140f65

Please sign in to comment.