-
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.
- Loading branch information
Showing
3 changed files
with
104 additions
and
1 deletion.
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,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'); | ||
}); | ||
}); |
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,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(); | ||
}); | ||
}); |
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