-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
261 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,105 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { UserController } from './user.controller'; | ||
import {instance, mock, when} from 'ts-mockito'; | ||
import {UserService} from '../../app/user/user.service'; | ||
import {UserServiceImpl} from '../../app/user/user.service.impl'; | ||
import {User} from '../../domain/user/user.entity'; | ||
import {UserDto} from '../../domain/user/dto/user.dto'; | ||
|
||
describe('User Controller', () => { | ||
const mockUserService = mock(UserServiceImpl); | ||
const address = 'testAddress'; | ||
const name = 'name'; | ||
let user: User; | ||
let controller: UserController; | ||
|
||
beforeEach(() => { | ||
user = new User(address, name); | ||
}); | ||
|
||
describe('dependency resolve', () => { | ||
it('should be defined', async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [ | ||
UserController, | ||
], | ||
providers: [ | ||
{ | ||
provide: 'UserService', | ||
useValue: instance(mockUserService), | ||
}, | ||
], | ||
}).compile(); | ||
|
||
controller = module.get<UserController>(UserController); | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); | ||
describe('#get()', () => { | ||
const id = 1; | ||
|
||
it('should return user', async () => { | ||
when(mockUserService.get(id)).thenReturn(new Promise((resolve) => { | ||
resolve(user); | ||
})); | ||
controller = new UserController(instance(mockUserService)); | ||
|
||
expect(await controller.get(id)).toBe(user); | ||
|
||
}); | ||
it('should return undefined', async () => { | ||
when(mockUserService.get(id)).thenReturn(new Promise((resolve) => resolve(undefined))); | ||
controller = new UserController(instance(mockUserService)); | ||
|
||
expect(await controller.get(id)).toBe(undefined); | ||
}); | ||
}); | ||
describe('#create()', () => { | ||
let userDto: UserDto; | ||
|
||
it('should return user', async () => { | ||
userDto = new UserDto(address, name); | ||
when(mockUserService.create(userDto)).thenReturn(new Promise((resolve => { | ||
resolve(user); | ||
}))); | ||
controller = new UserController(instance(mockUserService)); | ||
|
||
expect(await controller.create(userDto)).toBe(user); | ||
|
||
}); | ||
it('should return undefined', async () => { | ||
userDto = new UserDto(); | ||
when(mockUserService.create(userDto)).thenReturn(new Promise(((resolve) => resolve(undefined)))); | ||
controller = new UserController(instance(mockUserService)); | ||
|
||
expect(await controller.create(userDto)).toBe(undefined); | ||
}); | ||
}); | ||
describe('#delete()', () => { | ||
const id = 1; | ||
|
||
it('should return undefined', async () => { | ||
when(mockUserService.delete(id)).thenReturn(new Promise((resolve => resolve(undefined)))); | ||
controller = new UserController(instance(mockUserService)); | ||
|
||
expect(await controller.delete(id)).toBe(undefined); | ||
}); | ||
}); | ||
describe('#increaseLevel()', () => { | ||
const id = 1; | ||
const amount = 10; | ||
|
||
it('should return user', async () => { | ||
when(mockUserService.increaseLevel(id, amount)).thenReturn(new Promise((resolve => resolve(user)))); | ||
controller = new UserController(instance(mockUserService)); | ||
|
||
expect(await controller.increaseLevel(id, amount)).toBe(user); | ||
}); | ||
it('should return undefined', async () => { | ||
when(mockUserService.increaseLevel(id, amount)).thenReturn(undefined); | ||
controller = new UserController(instance(mockUserService)); | ||
|
||
expect(await controller.increaseLevel(id, amount)).toBe(undefined); | ||
}); | ||
}); | ||
}); |
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,44 @@ | ||
import {BadRequestException, Body, Controller, Delete, Get, Inject, Injectable, Param, Post, Req} from '@nestjs/common'; | ||
import {User} from '../../domain/user/user.entity'; | ||
import {UserDto} from '../../domain/user/dto/user.dto'; | ||
import {DeleteResult} from 'typeorm'; | ||
import {UserService} from '../../app/user/user.service'; | ||
|
||
@Injectable() | ||
@Controller('users') | ||
export class UserController { | ||
constructor(@Inject('UserService') private service: UserService) {} | ||
|
||
@Get(':id') | ||
async get(@Param('id') id: number): Promise<User> { | ||
let user = await this.service.get(id); | ||
if (user == undefined) { | ||
throw new BadRequestException('no user with the id'); | ||
} | ||
return user; | ||
} | ||
|
||
@Post() | ||
async create(@Body() userDto: UserDto): Promise<User> { | ||
return await this.service.create(userDto); | ||
} | ||
|
||
@Delete(':id') | ||
async delete(@Param('id') id: number): Promise<DeleteResult> { | ||
return await this.service.delete(id); | ||
} | ||
|
||
@Post(':id') | ||
async increaseLevel(@Param('id') id: number, @Param('amount') amount: number): Promise<User> { | ||
return await this.service.increaseLevel(id, amount); | ||
} | ||
|
||
@Post(':id') | ||
async decreasePoint(@Param('id') id: number, @Param('amount') amount: number): Promise<User> { | ||
return await this.service.decreasePoint(id, amount); | ||
} | ||
@Post(':id') | ||
async increasePoint(@Param('id') id: number, @Param('amount') amount: number): Promise<User> { | ||
return await this.service.increasePoint(id, amount); | ||
} | ||
} |
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,81 @@ | ||
import {INestApplication} from '@nestjs/common'; | ||
import {Test} from '@nestjs/testing'; | ||
import request from 'supertest'; | ||
import {UserModule} from '../../src/port/module/user.module'; | ||
import {UserServiceImpl} from '../../src/app/user/user.service.impl'; | ||
import {User} from '../../src/domain/user/user.entity'; | ||
import createMockInstance from "jest-create-mock-instance"; | ||
import {UserService} from "../../src/app/user/user.service"; | ||
|
||
|
||
describe('User', () => { | ||
let app: INestApplication; | ||
|
||
const mockUserServiceImpl = createMockInstance(UserServiceImpl); | ||
|
||
afterAll(async () => await app.close()); | ||
|
||
describe('#get()', () => { | ||
let url: string; | ||
let user: User; | ||
let response; | ||
|
||
it('should return 200', async () => { | ||
url = '/users/1'; | ||
user = new User('testAddress', 'testName'); | ||
response = { address: 'testAddress', name: 'testName', point: 0, level: 0 }; | ||
|
||
mockUserServiceImpl.get = jest.fn().mockReturnValue(user); | ||
|
||
const module = await Test.createTestingModule({ | ||
imports: [UserModule], | ||
}) | ||
.overrideProvider('UserService') | ||
.useValue(mockUserServiceImpl) | ||
.compile(); | ||
|
||
app = module.createNestApplication(); | ||
await app.init(); | ||
|
||
return request(app.getHttpServer()) | ||
.get(url) | ||
.expect(200) | ||
.expect(response); | ||
}); | ||
it('should return 400', async () => { | ||
url = '/users/1'; | ||
response = { statusCode: 400, error: 'Bad Request', message: 'no user with the id' }; | ||
mockUserServiceImpl.get = jest.fn().mockReturnValue(undefined); | ||
const module = await Test.createTestingModule({ | ||
imports: [UserModule], | ||
}) | ||
.overrideProvider('UserService') | ||
.useValue(mockUserServiceImpl) | ||
.compile(); | ||
|
||
app = module.createNestApplication(); | ||
await app.init(); | ||
|
||
return request(app.getHttpServer()) | ||
.get(url) | ||
.expect(400) | ||
.expect(response); | ||
}); | ||
}); | ||
describe('#create()', () => { | ||
it('should return 200', async () => { | ||
|
||
}); | ||
it('should return 400', async () => { | ||
|
||
}); | ||
}); | ||
describe('#delete()', () => { | ||
it('should return 200', async () => { | ||
|
||
}); | ||
it('should return 400', async () => { | ||
|
||
}); | ||
}); | ||
}); |