diff --git a/backend/test/reactions/reactions.e2e-spec.ts b/backend/test/reactions/reactions.e2e-spec.ts new file mode 100644 index 0000000..08e6df0 --- /dev/null +++ b/backend/test/reactions/reactions.e2e-spec.ts @@ -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); + diariesRepository = module.get(DiariesRepository); + usersRepository = module.get(UsersRepository); + + const dataSource = module.get(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: '

๋‚ด์šฉ

', + 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); + }); + }); +});