Skip to content

Commit

Permalink
Test: 컨트롤러, Dto 테스트 코드 작성
Browse files Browse the repository at this point in the history
- 유저 컨트롤러 테스트 코드 작성
- 유저 Dto 테스트 코드 작성
- 해시태그 컨트롤러 테스트 코드 수정
- 헬스체크 컨트롤러 테스트 코드 작성

Related to #29
  • Loading branch information
Zamoca42 committed Jan 7, 2024
1 parent 6f12f09 commit 7669c00
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/hashtag/dto/get-hastag-list.dto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('GetHashtagList', () => {

it('should be defined', () => {
expect(hashtagList).toBeDefined();
expect(mockHashtagData).toBeInstanceOf(Hashtag);
});

it('SUCCESS: Hashtag 엔티티로 GetHashtagList DTO 생성', () => {
Expand Down
75 changes: 75 additions & 0 deletions src/health.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Test, TestingModule } from '@nestjs/testing';
import { HealthController } from './health.controller';
import { HealthCheckService, TypeOrmHealthIndicator } from '@nestjs/terminus';
import { HealthCheckResult } from '@nestjs/terminus/dist/health-check/health-check-result.interface';
import { ResponseEntity } from './common/entity/response.entity';
import { HttpStatus } from '@nestjs/common';

class MockHealthCheckService {
check = jest.fn();
}

class MockTypeOrmHealthIndicator {
pingCheck = jest.fn();
}

describe('HealthController', () => {
let healthController: HealthController;
let healthCheckService: HealthCheckService;
let typeOrmHealthIndicator: TypeOrmHealthIndicator;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [HealthController],
providers: [
{ provide: HealthCheckService, useClass: MockHealthCheckService },
{
provide: TypeOrmHealthIndicator,
useClass: MockTypeOrmHealthIndicator,
},
],
}).compile();

healthController = module.get<HealthController>(HealthController);
healthCheckService = module.get<HealthCheckService>(HealthCheckService);
typeOrmHealthIndicator = module.get<TypeOrmHealthIndicator>(
TypeOrmHealthIndicator,
);
});

afterEach(() => {
jest.clearAllMocks();
});

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

describe('readiness', () => {
it('SUCCESS: 헬스체크 성공', async () => {
const mockResult: HealthCheckResult = {
status: 'ok',
details: { database: { status: 'up' } },
info: {},
error: {},
};

jest.spyOn(healthCheckService, 'check').mockResolvedValue(mockResult);

const response = await healthController.readiness();

expect(healthCheckService.check).toHaveBeenCalledWith([
expect.any(Function),
]);
expect(response.code).toEqual(HttpStatus.OK);
expect(response).toEqual(
ResponseEntity.OK_WITH<HealthCheckResult>(
expect.any(String),
mockResult,
),
);
});
});
});
31 changes: 31 additions & 0 deletions src/user/dto/get-user.dto.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { plainToInstance } from 'class-transformer';
import { GetUserList } from './get-user.dto';
import { User } from '../entity/user.entity';

describe('GetUserList', () => {
let mockUserData: User;
let getUserList: GetUserList;

beforeEach(() => {
mockUserData = plainToInstance(User, {
id: 1,
username: 'feed-me-admin1',
email: '[email protected]',
reviews: [],
});

getUserList = new GetUserList(mockUserData);
});

it('should be defined', () => {
expect(getUserList).toBeDefined();
expect(mockUserData).toBeInstanceOf(User);
});

it('SUCCESS: User 엔티티로 GetUserList DTO 생성', () => {
expect(getUserList.id).toEqual(mockUserData.id);
expect(getUserList.username).toEqual(mockUserData.username);
expect(getUserList.email).toEqual(mockUserData.email);
expect(getUserList.review).toEqual(0);
});
});
62 changes: 62 additions & 0 deletions src/user/user.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Test, TestingModule } from '@nestjs/testing';
import { UserController } from './user.controller';
import { UserService } from './user.service';
import { User } from './entity/user.entity';
import { PaginationDto } from '../common/dto/get-pagination-query.dto';
import { PageEntity } from '../common/dto/get-pagination-list.dto';
import { GetUserList } from './dto/get-user.dto';
import { ResponseEntity } from '../common/entity/response.entity';
import { HttpStatus } from '@nestjs/common';

class MockUserService {
findAllUser = jest.fn();
}

describe('UserController', () => {
let userController: UserController;
let userService: UserService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [UserController],
providers: [{ provide: UserService, useClass: MockUserService }],
}).compile();

userController = module.get<UserController>(UserController);
userService = module.get<UserService>(UserService);
});

afterEach(() => {
jest.clearAllMocks();
});

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

describe('findAllUser', () => {
it('SUCCESS: 유저 목록 반환', async () => {
const mockRequest = new PaginationDto();
const mockResponse = [new User()];
const mockCount = 2;

jest
.spyOn(userService, 'findAllUser')
.mockResolvedValue([mockResponse, mockCount]);

const result = await userController.findAllUser(mockRequest);

expect(userService.findAllUser).toHaveBeenCalledWith(
mockRequest.getPageProps(),
);
expect(result.code).toEqual(HttpStatus.OK);
expect(result).toEqual(
ResponseEntity.OK_WITH<PageEntity<GetUserList>>(
expect.any(String),
expect.any(PageEntity<GetUserList>),
),
);
});
});
});

0 comments on commit 7669c00

Please sign in to comment.