Skip to content

Commit

Permalink
test: 좋아요 API 테스트 케이스 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
BangDori committed Apr 16, 2024
1 parent 881ed6e commit fd8302d
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/app/mocks/test/like.error.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, expect, it } from 'vitest';
import axios from 'axios';

const testCases = [
['GET', '피드 좋아요 조회 API'],
['POST', '피드 좋아요 API'],
['DELETE', '피드 좋아요 취소 API'],
];

describe.each(testCases)('[%s] %s', (method) => {
it('피드 아이디가 정수 타입이 아니라면 에러가 발생한다.', async () => {
const feedIds = ['hello', undefined, '[]', '{}'];

for (const feedId of feedIds) {
const response = await axios({
method,
url: `/feeds/${feedId}/likes`,
});
const { code } = response.data;

expect(code).toBe('4220');
}
});

it('피드 아이디에 해당하는 피드가 존재하지 않는다면 에러가 발생한다.', async () => {
const feedId = 5389532104234901;

const response = await axios({
method,
url: `/feeds/${feedId}/likes`,
});
const { code } = response.data;

expect(code).toBe('4040');
});
});
66 changes: 66 additions & 0 deletions src/app/mocks/test/like.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { describe, expect, it } from 'vitest';
import axios from 'axios';

async function getFeedById(feedId: number) {
const likes = await axios.get(`/feeds/${feedId}/likes`);
const { totalCount, isLike } = likes.data.like;

return { totalCount, isLike };
}

describe('좋아요가 되어있지 않은 상태에서', () => {
it('좋아요를 클릭하면, 좋아요의 수가 1 증가한다.', async () => {
// 현재 좋아요의 상태
const feedId = 1;
const { totalCount: prevTotalCount } = await getFeedById(feedId);

// 좋아요 클릭
await axios.post(`/feeds/${feedId}/likes`);

// 업데이트 된 좋아요의 상태
const { totalCount, isLike } = await getFeedById(feedId);

expect(totalCount).toBe(prevTotalCount + 1);
expect(isLike).toBeTruthy();
});

it('좋아요 취소를 클릭하면, 좋아요의 수가 변경되지 않는다.', async () => {
const feedId = 3;
const { totalCount: prevTotalCount, isLike: prevIsLike } =
await getFeedById(feedId);

await axios.delete(`/feeds/${feedId}/likes`);

const { totalCount, isLike } = await getFeedById(feedId);

expect(totalCount).toBe(prevTotalCount);
expect(isLike).toBe(prevIsLike);
});
});

describe('좋아요가 되어있는 상태에서', () => {
it('좋아요를 클릭하면, 좋아요의 수가 변동되지 않는다.', async () => {
const feedId = 2;
const { totalCount: prevTotalCount, isLike: prevIsLike } =
await getFeedById(feedId);

await axios.post(`/feeds/${feedId}/likes`);

const { totalCount, isLike } = await getFeedById(feedId);

expect(totalCount).toBe(prevTotalCount);
expect(isLike).toBe(prevIsLike);
});

it('좋아요 취소를 클릭하면, 좋아요의 수가 1 감소한다.', async () => {
const feedId = 4;
const { totalCount: prevTotalCount } = await getFeedById(feedId);

await axios.delete(`/feeds/${feedId}/likes`);

const { totalCount, isLike } = await getFeedById(feedId);

expect(totalCount).toBe(prevTotalCount - 1);
expect(isLike).toBeFalsy();
});
});
3 changes: 2 additions & 1 deletion src/setupTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ expect.extend(matchers);

// 테스트용 Mocking 서버 설정
import { setupServer } from 'msw/node';
import { likeHandlers } from './app/mocks/handler/like';

export const server = setupServer();
export const server = setupServer(...likeHandlers);

beforeAll(() => server.listen({ onUnhandledRequest: 'error' }));
afterAll(() => server.close());
Expand Down

0 comments on commit fd8302d

Please sign in to comment.