-
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.
Merge pull request #563 from boostcampwm2023/BE-UnitTest-#562
[BE/#562] block user, report 서비스 테스트 코드 작성
- Loading branch information
Showing
7 changed files
with
228 additions
and
16 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
File renamed without changes.
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,83 @@ | ||
import { Repository } from 'typeorm'; | ||
import { ReportService } from './report.service'; | ||
import { ReportEntity } from '../entities/report.entity'; | ||
import { PostEntity } from '../entities/post.entity'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { getRepositoryToken } from '@nestjs/typeorm'; | ||
import { CreateReportDto } from './dto/createReport.dto'; | ||
import { HttpException } from '@nestjs/common'; | ||
|
||
const mockReportRepository = () => ({ | ||
save: jest.fn(), | ||
find: jest.fn(), | ||
findOne: jest.fn(), | ||
softDelete: jest.fn(), | ||
}); | ||
|
||
const mockPostRepository = () => ({ | ||
findOne: jest.fn(), | ||
exist: jest.fn(), | ||
}); | ||
|
||
type MockRepository<T = any> = Partial<Record<keyof Repository<T>, jest.Mock>>; | ||
|
||
describe('ReportService', function () { | ||
let service: ReportService; | ||
let reportRepository: MockRepository<ReportEntity>; | ||
let postRepository: MockRepository<PostEntity>; | ||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
ReportService, | ||
{ | ||
provide: getRepositoryToken(ReportEntity), | ||
useValue: mockReportRepository(), | ||
}, | ||
{ | ||
provide: getRepositoryToken(PostEntity), | ||
useValue: mockPostRepository(), | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<ReportService>(ReportService); | ||
reportRepository = module.get<MockRepository<ReportEntity>>( | ||
getRepositoryToken(ReportEntity), | ||
); | ||
postRepository = module.get<MockRepository<PostEntity>>( | ||
getRepositoryToken(PostEntity), | ||
); | ||
}); | ||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
describe('createReport()', function () { | ||
const body = new CreateReportDto(); | ||
body.post_id = 123; | ||
body.user_id = 'user'; | ||
body.description = 'test'; | ||
it('should bad request', function () { | ||
expect(async () => { | ||
await service.createReport(body, 'user'); | ||
}).rejects.toThrowError( | ||
new HttpException('자신의 게시글은 신고 할 수 없습니다.', 400), | ||
); | ||
}); | ||
|
||
it('should not found', function () { | ||
postRepository.exist.mockResolvedValue(false); | ||
expect(async () => { | ||
await service.createReport(body, 'user1'); | ||
}).rejects.toThrowError( | ||
new HttpException('신고할 대상이 존재 하지 않습니다.', 404), | ||
); | ||
}); | ||
|
||
it('should save', async function () { | ||
postRepository.exist.mockResolvedValue(true); | ||
await service.createReport(body, 'user1'); | ||
expect(reportRepository.save).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); |
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,136 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { getRepositoryToken } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { HttpException } from '@nestjs/common'; | ||
import { UsersBlockService } from './users-block.service'; | ||
import { BlockUserEntity } from '../entities/blockUser.entity'; | ||
import { UserEntity } from '../entities/user.entity'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
const mockBlockUserRepository = () => ({ | ||
save: jest.fn(), | ||
find: jest.fn(), | ||
findOne: jest.fn(), | ||
softDelete: jest.fn(), | ||
}); | ||
|
||
const mockUserRepository = () => ({ | ||
findOne: jest.fn(), | ||
}); | ||
type MockRepository<T = any> = Partial<Record<keyof Repository<T>, jest.Mock>>; | ||
describe('PostsBlockService', () => { | ||
let service: UsersBlockService; | ||
let blockUserRepository: MockRepository<BlockUserEntity>; | ||
let userRepository: MockRepository<UserEntity>; | ||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
UsersBlockService, | ||
{ | ||
provide: getRepositoryToken(BlockUserEntity), | ||
useValue: mockBlockUserRepository(), | ||
}, | ||
{ | ||
provide: getRepositoryToken(UserEntity), | ||
useValue: mockUserRepository(), | ||
}, | ||
{ | ||
provide: ConfigService, | ||
useValue: { get: jest.fn((key: string) => 'mocked-value') }, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<UsersBlockService>(UsersBlockService); | ||
blockUserRepository = module.get<MockRepository<BlockUserEntity>>( | ||
getRepositoryToken(BlockUserEntity), | ||
); | ||
userRepository = module.get<MockRepository<UserEntity>>( | ||
getRepositoryToken(UserEntity), | ||
); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
describe('addBlockUser()', function () { | ||
it('should not found', function () { | ||
userRepository.findOne.mockResolvedValue(null); | ||
expect(async () => { | ||
await service.addBlockUser('user', 'blocker'); | ||
}).rejects.toThrowError( | ||
new HttpException('존재하지 않는 유저입니다', 404), | ||
); | ||
}); | ||
it('should bad request', function () { | ||
userRepository.findOne.mockResolvedValue(new UserEntity()); | ||
blockUserRepository.findOne.mockResolvedValue({ delete_date: null }); | ||
expect(async () => { | ||
await service.addBlockUser('user', 'blocker'); | ||
}).rejects.toThrowError(new HttpException('이미 차단된 유저입니다', 400)); | ||
}); | ||
it('should save', async function () { | ||
userRepository.findOne.mockResolvedValue(new UserEntity()); | ||
blockUserRepository.findOne.mockResolvedValue(null); | ||
blockUserRepository.save.mockResolvedValue(new BlockUserEntity()); | ||
await service.addBlockUser('user', 'blocker'); | ||
expect(blockUserRepository.save).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe('getBlockUser', function () { | ||
it('should be user who has profile_img', async function () { | ||
const blockUsers = []; | ||
const blockUser = new BlockUserEntity(); | ||
blockUser.blocked_user = 'user'; | ||
blockUser.blockedUser = new UserEntity(); | ||
blockUser.blockedUser.nickname = 'test'; | ||
blockUser.blockedUser.profile_img = 'image_url'; | ||
blockUsers.push(blockUser); | ||
blockUserRepository.find.mockResolvedValue(blockUsers); | ||
const users = await service.getBlockUser('user'); | ||
expect(users[0].profile_img).toEqual('image_url'); | ||
}); | ||
|
||
it('should be user who has no profile_img', async function () { | ||
const blockUsers = []; | ||
const blockUser = new BlockUserEntity(); | ||
blockUser.blocked_user = 'user'; | ||
blockUser.blockedUser = new UserEntity(); | ||
blockUser.blockedUser.nickname = 'test'; | ||
blockUser.blockedUser.profile_img = null; | ||
blockUsers.push(blockUser); | ||
blockUserRepository.find.mockResolvedValue(blockUsers); | ||
const users = await service.getBlockUser('user'); | ||
expect(users[0].profile_img).toEqual('mocked-value'); | ||
}); | ||
|
||
it('should be user who leave', async function () { | ||
const blockUsers = []; | ||
const blockUser = new BlockUserEntity(); | ||
blockUser.blocked_user = 'user'; | ||
blockUser.blockedUser = null; | ||
blockUsers.push(blockUser); | ||
blockUserRepository.find.mockResolvedValue(blockUsers); | ||
const users = await service.getBlockUser('user'); | ||
expect(users[0].profile_img).toEqual(null); | ||
expect(users[0].nickname).toEqual(null); | ||
expect(users[0].user_id).toEqual('user'); | ||
}); | ||
}); | ||
|
||
describe('removeBlockUser', function () { | ||
it('should not found', function () { | ||
blockUserRepository.findOne.mockResolvedValue(null); | ||
expect(async () => { | ||
await service.removeBlockUser('user', 'blocker'); | ||
}).rejects.toThrowError(new HttpException('없는 사용자 입니다.', 404)); | ||
}); | ||
|
||
it('should delete', async function () { | ||
blockUserRepository.findOne.mockResolvedValue(new BlockUserEntity()); | ||
await service.removeBlockUser('user', 'blocker'); | ||
expect(blockUserRepository.softDelete).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); |
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