-
Notifications
You must be signed in to change notification settings - Fork 1
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 #391 from shunny822/be/test/reactions-e2e
[BE] Reactions e2e 테스트
- Loading branch information
Showing
1 changed file
with
245 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
import * as request from 'supertest'; | ||
import { INestApplication } from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
import { AppModule } from 'src/app.module'; | ||
import { DataSource, QueryRunner } from 'typeorm'; | ||
import { Diary } from 'src/diaries/entity/diary.entity'; | ||
import { SocialType } from 'src/users/entity/socialType'; | ||
import { DiaryStatus } from 'src/diaries/entity/diaryStatus'; | ||
import { DiariesRepository } from 'src/diaries/diaries.repository'; | ||
import { ReactionsRepository } from 'src/reactions/reactions.repository'; | ||
import { UsersRepository } from 'src/users/users.repository'; | ||
import { MoodDegree } from 'src/diaries/utils/diaries.constant'; | ||
import * as cookieParser from 'cookie-parser'; | ||
import { User } from 'src/users/entity/user.entity'; | ||
import { testLogin } from 'test/utils/testLogin'; | ||
|
||
describe('ReactionsController (e2e)', () => { | ||
let app: INestApplication; | ||
let queryRunner: QueryRunner; | ||
let reactionsRepository: ReactionsRepository; | ||
let diariesRepository: DiariesRepository; | ||
let usersRepository: UsersRepository; | ||
|
||
let user: User; | ||
let accessToken: string; | ||
let diary: Diary; | ||
let url: string; | ||
|
||
beforeAll(async () => { | ||
const module = await Test.createTestingModule({ | ||
imports: [AppModule], | ||
}).compile(); | ||
|
||
reactionsRepository = module.get<ReactionsRepository>(ReactionsRepository); | ||
diariesRepository = module.get<DiariesRepository>(DiariesRepository); | ||
usersRepository = module.get<UsersRepository>(UsersRepository); | ||
|
||
const dataSource = module.get<DataSource>(DataSource); | ||
queryRunner = dataSource.createQueryRunner(); | ||
dataSource.createQueryRunner = jest.fn(); | ||
queryRunner.release = jest.fn(); | ||
(dataSource.createQueryRunner as jest.Mock).mockReturnValue(queryRunner); | ||
|
||
app = module.createNestApplication(); | ||
app.use(cookieParser()); | ||
await app.init(); | ||
|
||
jest.spyOn(reactionsRepository, 'save'); | ||
jest.spyOn(reactionsRepository, 'remove'); | ||
|
||
const userInfo = { | ||
socialId: '1234', | ||
socialType: SocialType.NAVER, | ||
nickname: 'test', | ||
email: 'test@abc.com', | ||
profileImage: 'profile image', | ||
}; | ||
|
||
user = await usersRepository.save(userInfo); | ||
accessToken = await testLogin(user); | ||
|
||
const diaryInfo = { | ||
author: user, | ||
title: '제목', | ||
content: '<p>내용</p>', | ||
thumbnail: null, | ||
emotion: '🥰', | ||
tagNames: ['일기', '안녕'], | ||
status: DiaryStatus.PUBLIC, | ||
summary: '일기 요약', | ||
mood: MoodDegree.SO_SO, | ||
}; | ||
|
||
diary = await diariesRepository.save(diaryInfo); | ||
url = `/reactions/${diary.id}`; | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.close(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await queryRunner.startTransaction(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await queryRunner.rollbackTransaction(); | ||
}); | ||
|
||
describe('/reactions/:diaryId (GET)', () => { | ||
it('특정 일기의 리액션 조회', async () => { | ||
// given | ||
const friendInfo = { | ||
socialId: '12345', | ||
socialType: SocialType.NAVER, | ||
nickname: 'friend', | ||
email: 'friend@abc.com', | ||
profileImage: 'profile image', | ||
}; | ||
const friend = await usersRepository.save(friendInfo); | ||
|
||
await reactionsRepository.save({ user, diary, reaction: '🔥' }); | ||
await reactionsRepository.save({ user: friend, diary, reaction: '🥰' }); | ||
|
||
// when | ||
const response = await request(app.getHttpServer()) | ||
.get(url) | ||
.set('Cookie', [`utk=${accessToken}`]); | ||
|
||
// then | ||
expect(response.statusCode).toEqual(200); | ||
expect(response.body.reactionList).toHaveLength(2); | ||
expect(response.body.reactionList[0].reaction).toEqual('🔥'); | ||
}); | ||
|
||
it('일기의 리액션 없는 경우 빈 배열 반환', async () => { | ||
// when | ||
const response = await request(app.getHttpServer()) | ||
.get(url) | ||
.set('Cookie', [`utk=${accessToken}`]); | ||
|
||
// then | ||
expect(response.statusCode).toEqual(200); | ||
expect(response.body.reactionList).toEqual([]); | ||
}); | ||
}); | ||
|
||
describe('/reactions/:diaryId (POST)', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('리액션 저장', async () => { | ||
// when | ||
const response = await request(app.getHttpServer()) | ||
.post(url) | ||
.set('Cookie', [`utk=${accessToken}`]) | ||
.send({ reaction: '🥰' }); | ||
|
||
// then | ||
expect(response.statusCode).toEqual(201); | ||
expect(reactionsRepository.save).toHaveBeenCalled(); | ||
}); | ||
|
||
it('해당 일기에 이미 리액션을 남긴 경우 예외 발생', async () => { | ||
// given | ||
await reactionsRepository.save({ user, diary, reaction: '🔥' }); | ||
|
||
// when | ||
const response = await request(app.getHttpServer()) | ||
.post(url) | ||
.set('Cookie', [`utk=${accessToken}`]) | ||
.send({ reaction: '🥰' }); | ||
|
||
// then | ||
expect(response.statusCode).toEqual(400); | ||
expect(response.body.message).toEqual('이미 해당 글에 리액션을 남겼습니다.'); | ||
expect(reactionsRepository.save).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe('/reactions/:diaryId (PUT)', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('리액션 수정', async () => { | ||
// given | ||
await reactionsRepository.save({ user, diary, reaction: '🔥' }); | ||
|
||
// when | ||
const response = await request(app.getHttpServer()) | ||
.put(url) | ||
.set('Cookie', [`utk=${accessToken}`]) | ||
.send({ reaction: '🥰' }); | ||
|
||
// then | ||
expect(response.statusCode).toEqual(200); | ||
expect(reactionsRepository.save).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('리액션이 존재하지 않는데 해당 요청을 보낸 경우 예외 발생', async () => { | ||
// when | ||
const response = await request(app.getHttpServer()) | ||
.put(url) | ||
.set('Cookie', [`utk=${accessToken}`]) | ||
.send({ reaction: '🥰' }); | ||
|
||
// then | ||
expect(response.statusCode).toEqual(400); | ||
expect(response.body.message).toEqual('리액션 기록이 존재하지 않습니다.'); | ||
expect(reactionsRepository.save).toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
|
||
describe('/reactions/:diaryId (DELETE)', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('리액션 삭제', async () => { | ||
// given | ||
await reactionsRepository.save({ user, diary, reaction: '🔥' }); | ||
|
||
// when | ||
const response = await request(app.getHttpServer()) | ||
.delete(url) | ||
.set('Cookie', [`utk=${accessToken}`]) | ||
.send({ reaction: '🔥' }); | ||
|
||
// then | ||
expect(response.statusCode).toEqual(200); | ||
expect(reactionsRepository.remove).toHaveBeenCalled(); | ||
}); | ||
|
||
it('삭제하려는 리액션이 존재하지 않는 경우 예외 발생', async () => { | ||
// when | ||
const response = await request(app.getHttpServer()) | ||
.delete(url) | ||
.set('Cookie', [`utk=${accessToken}`]) | ||
.send({ reaction: '🔥' }); | ||
|
||
// then | ||
expect(response.statusCode).toEqual(400); | ||
expect(response.body.message).toEqual('이미 삭제된 리액션 정보입니다.'); | ||
expect(reactionsRepository.remove).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('기존에 남긴 리액션과 삭제하려는 리액션이 일치하지 않는 경우 예외 발생', async () => { | ||
// given | ||
await reactionsRepository.save({ user, diary, reaction: '🔥' }); | ||
|
||
// when | ||
const response = await request(app.getHttpServer()) | ||
.delete(url) | ||
.set('Cookie', [`utk=${accessToken}`]) | ||
.send({ reaction: '🥰' }); | ||
|
||
// then | ||
expect(response.statusCode).toEqual(400); | ||
expect(response.body.message).toEqual('이미 삭제된 리액션 정보입니다.'); | ||
expect(reactionsRepository.remove).toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
}); |