generated from amosproj/amos202Xss0Y-projname
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Florian Oberndörfer <[email protected]>
- Loading branch information
Showing
4 changed files
with
207 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |