-
Notifications
You must be signed in to change notification settings - Fork 3
test(reviews/favorites): 리뷰·찜 서버 API 테스트 추가 #349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,164 @@ | ||
| import { createCookieStore, mockedCookies } from "@/mocks/utils/mockHeader"; | ||
| import { http, HttpResponse } from "msw"; | ||
| import { getFavorites } from "@/features/favorites/apis/server"; | ||
| import favorites from "@/mocks/data/favorites"; | ||
| import { server } from "@/mocks/server"; | ||
| import { ApiError } from "@/utils/api"; | ||
|
|
||
| const TEST_API_BASE = "http://localhost/api"; | ||
|
|
||
| beforeEach(() => { | ||
| mockedCookies.mockResolvedValue(createCookieStore()); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe("favorites server apis 테스트", () => { | ||
| describe("getFavorites", () => { | ||
| it("성공 시 찜 목록 데이터를 반환한다", async () => { | ||
| const expectedResponse = favorites.list(); | ||
|
|
||
| server.use(http.get(`${TEST_API_BASE}/favorites`, () => HttpResponse.json(expectedResponse))); | ||
|
|
||
| const result = await getFavorites(); | ||
|
|
||
| expect(Array.isArray(result.data)).toBe(true); | ||
| expect(result.data[0]).toMatchObject({ | ||
| id: expectedResponse.data[0].id, | ||
| meetingId: expectedResponse.data[0].meetingId, | ||
| userId: expectedResponse.data[0].userId, | ||
| createdAt: expectedResponse.data[0].createdAt, | ||
| }); | ||
| expect(result.data[0].meeting).toMatchObject({ | ||
| id: expectedResponse.data[0].meeting.id, | ||
| name: expectedResponse.data[0].meeting.name, | ||
| type: expectedResponse.data[0].meeting.type, | ||
| region: expectedResponse.data[0].meeting.region, | ||
| }); | ||
| expect(result.nextCursor).toBe(expectedResponse.nextCursor); | ||
| expect(result.hasMore).toBe(expectedResponse.hasMore); | ||
| }); | ||
|
|
||
| it("필터와 날짜 조건을 쿼리스트링으로 변환해 요청한다", async () => { | ||
| let capturedUrl = ""; | ||
| let authorizationHeader = ""; | ||
|
|
||
| mockedCookies.mockResolvedValue(createCookieStore({ accessToken: "access-token" })); | ||
|
|
||
| server.use( | ||
| http.get(`${TEST_API_BASE}/favorites`, ({ request }) => { | ||
| capturedUrl = request.url; | ||
| authorizationHeader = request.headers.get("authorization") ?? ""; | ||
| return HttpResponse.json(favorites.list()); | ||
| }), | ||
| ); | ||
|
|
||
| await getFavorites({ | ||
| type: "자기계발", | ||
| region: "서울특별시 광진구", | ||
| dateStart: "2026-04-01", | ||
| dateEnd: "2026-04-30", | ||
| sortBy: "participantCount", | ||
| sortOrder: "asc", | ||
| cursor: "10", | ||
| size: 5, | ||
| }); | ||
|
|
||
| const url = new URL(capturedUrl); | ||
|
|
||
| expect(url.searchParams.get("type")).toBe("자기계발"); | ||
| expect(url.searchParams.get("region")).toBe("서울특별시 광진구"); | ||
| expect(url.searchParams.get("dateStart")).toBe("2026-04-01T00:00:00+09:00"); | ||
| expect(url.searchParams.get("dateEnd")).toBe("2026-04-30T23:59:59+09:00"); | ||
| expect(url.searchParams.get("sortBy")).toBe("participantCount"); | ||
| expect(url.searchParams.get("sortOrder")).toBe("asc"); | ||
| expect(url.searchParams.get("cursor")).toBe("10"); | ||
| expect(url.searchParams.get("size")).toBe("5"); | ||
| expect(authorizationHeader).toBe("Bearer access-token"); | ||
| }); | ||
|
|
||
| it("URLSearchParams 입력을 파싱해 숫자 size만 요청에 포함한다", async () => { | ||
| let capturedUrl = ""; | ||
|
|
||
| server.use( | ||
| http.get(`${TEST_API_BASE}/favorites`, ({ request }) => { | ||
| capturedUrl = request.url; | ||
| return HttpResponse.json(favorites.list()); | ||
| }), | ||
| ); | ||
|
|
||
| await getFavorites( | ||
| new URLSearchParams({ | ||
| type: "운동/스포츠", | ||
| sortBy: "registrationEnd", | ||
| sortOrder: "desc", | ||
| size: "3", | ||
| cursor: "6", | ||
| }), | ||
| ); | ||
|
|
||
| const url = new URL(capturedUrl); | ||
|
|
||
| expect(url.searchParams.get("type")).toBe("운동/스포츠"); | ||
| expect(url.searchParams.get("sortBy")).toBe("registrationEnd"); | ||
| expect(url.searchParams.get("sortOrder")).toBe("desc"); | ||
| expect(url.searchParams.get("size")).toBe("3"); | ||
| expect(url.searchParams.get("cursor")).toBe("6"); | ||
| }); | ||
|
|
||
| it("size가 숫자가 아니면 쿼리에서 제외한다", async () => { | ||
| let capturedUrl = ""; | ||
|
|
||
| server.use( | ||
| http.get(`${TEST_API_BASE}/favorites`, ({ request }) => { | ||
| capturedUrl = request.url; | ||
| return HttpResponse.json(favorites.list()); | ||
| }), | ||
| ); | ||
|
|
||
| await getFavorites( | ||
| new URLSearchParams({ | ||
| type: "여행", | ||
| size: "not-a-number", | ||
| }), | ||
| ); | ||
|
|
||
| const url = new URL(capturedUrl); | ||
|
|
||
| expect(url.searchParams.get("type")).toBe("여행"); | ||
| expect(url.searchParams.has("size")).toBe(false); | ||
| }); | ||
|
|
||
| it("실패 시 응답 메시지와 status를 담은 ApiError를 던진다", async () => { | ||
| server.use( | ||
| http.get(`${TEST_API_BASE}/favorites`, () => | ||
| HttpResponse.json( | ||
| { message: "찜 목록을 불러오지 못했습니다.", code: "FAVORITES_FETCH_FAILED" }, | ||
| { status: 400 }, | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| await expect(getFavorites()).rejects.toMatchObject<ApiError>({ | ||
| name: "ApiError", | ||
| message: "찜 목록을 불러오지 못했습니다.", | ||
| status: 400, | ||
| code: "FAVORITES_FETCH_FAILED", | ||
| }); | ||
| }); | ||
|
|
||
| it("에러 응답 바디가 없으면 상태 코드별 기본 메시지로 ApiError를 던진다", async () => { | ||
| server.use( | ||
| http.get(`${TEST_API_BASE}/favorites`, () => new HttpResponse(null, { status: 401 })), | ||
| ); | ||
|
|
||
| await expect(getFavorites()).rejects.toMatchObject<ApiError>({ | ||
| name: "ApiError", | ||
| message: "인증이 필요합니다", | ||
| status: 401, | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or 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,218 @@ | ||
| import { createCookieStore, mockedCookies } from "@/mocks/utils/mockHeader"; | ||
| import { http, HttpResponse } from "msw"; | ||
| import { | ||
| getReviews, | ||
| getReviewsCategoriesStatistics, | ||
| getReviewsStatistics, | ||
| } from "@/features/reviews/apis/server"; | ||
| import reviews from "@/mocks/data/reviews"; | ||
| import { CATEGORY_STATISTICS, STATISTICS } from "@/mocks/data/reviews/fixtures"; | ||
| import { server } from "@/mocks/server"; | ||
| import { ApiError } from "@/utils/api"; | ||
|
|
||
| const TEST_API_BASE = "http://localhost/api"; | ||
|
|
||
| beforeEach(() => { | ||
| mockedCookies.mockResolvedValue(createCookieStore()); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe("reviews server apis 테스트", () => { | ||
| describe("getReviews", () => { | ||
| it("성공 시 리뷰 목록 데이터를 반환한다", async () => { | ||
| const expectedResponse = reviews.list(); | ||
|
|
||
| server.use(http.get(`${TEST_API_BASE}/reviews`, () => HttpResponse.json(expectedResponse))); | ||
|
|
||
| const result = await getReviews(); | ||
|
|
||
| expect(Array.isArray(result.data)).toBe(true); | ||
| expect(result.data[0]).toMatchObject({ | ||
| id: expectedResponse.data[0].id, | ||
| meetingId: expectedResponse.data[0].meetingId, | ||
| userId: expectedResponse.data[0].userId, | ||
| score: expectedResponse.data[0].score, | ||
| comment: expectedResponse.data[0].comment, | ||
| }); | ||
| expect(result.nextCursor).toBe(expectedResponse.nextCursor); | ||
| expect(result.hasMore).toBe(expectedResponse.hasMore); | ||
| }); | ||
|
|
||
| it("필터와 날짜 조건을 쿼리스트링으로 변환해 요청한다", async () => { | ||
| let capturedUrl = ""; | ||
| let authorizationHeader = ""; | ||
|
|
||
| mockedCookies.mockResolvedValue(createCookieStore({ accessToken: "access-token" })); | ||
|
|
||
| server.use( | ||
| http.get(`${TEST_API_BASE}/reviews`, ({ request }) => { | ||
| capturedUrl = request.url; | ||
| authorizationHeader = request.headers.get("authorization") ?? ""; | ||
| return HttpResponse.json(reviews.list()); | ||
| }), | ||
| ); | ||
|
|
||
| await getReviews({ | ||
| type: "운동/스포츠", | ||
| region: "서울특별시 광진구", | ||
| dateStart: "2026-04-01", | ||
| dateEnd: "2026-04-30", | ||
| registrationEndStart: "2026-03-20", | ||
| registrationEndEnd: "2026-03-25", | ||
| sortBy: "dateTime", | ||
| sortOrder: "asc", | ||
| cursor: "10", | ||
| size: 5, | ||
| }); | ||
|
|
||
| const url = new URL(capturedUrl); | ||
|
|
||
| expect(url.searchParams.get("type")).toBe("운동/스포츠"); | ||
| expect(url.searchParams.get("region")).toBe("서울특별시 광진구"); | ||
| expect(url.searchParams.get("dateStart")).toBe("2026-04-01T00:00:00+09:00"); | ||
| expect(url.searchParams.get("dateEnd")).toBe("2026-04-30T23:59:59+09:00"); | ||
| expect(url.searchParams.get("registrationEndStart")).toBe("2026-03-20T00:00:00+09:00"); | ||
| expect(url.searchParams.get("registrationEndEnd")).toBe("2026-03-25T23:59:59+09:00"); | ||
|
jay0425 marked this conversation as resolved.
|
||
| expect(url.searchParams.get("sortBy")).toBe("dateTime"); | ||
| expect(url.searchParams.get("sortOrder")).toBe("asc"); | ||
| expect(url.searchParams.get("cursor")).toBe("10"); | ||
| expect(url.searchParams.get("size")).toBe("5"); | ||
| expect(authorizationHeader).toBe("Bearer access-token"); | ||
| }); | ||
|
|
||
| it("URLSearchParams 입력을 파싱해 숫자 size만 요청에 포함한다", async () => { | ||
| let capturedUrl = ""; | ||
|
|
||
| server.use( | ||
| http.get(`${TEST_API_BASE}/reviews`, ({ request }) => { | ||
| capturedUrl = request.url; | ||
| return HttpResponse.json(reviews.list()); | ||
| }), | ||
| ); | ||
|
|
||
| await getReviews( | ||
| new URLSearchParams({ | ||
| type: "자기계발", | ||
| sortBy: "participantCount", | ||
| sortOrder: "desc", | ||
| size: "3", | ||
| cursor: "6", | ||
| }), | ||
| ); | ||
|
|
||
| const url = new URL(capturedUrl); | ||
|
|
||
| expect(url.searchParams.get("type")).toBe("자기계발"); | ||
| expect(url.searchParams.get("sortBy")).toBe("participantCount"); | ||
| expect(url.searchParams.get("sortOrder")).toBe("desc"); | ||
| expect(url.searchParams.get("size")).toBe("3"); | ||
| expect(url.searchParams.get("cursor")).toBe("6"); | ||
| }); | ||
|
|
||
| it("size가 숫자가 아니면 쿼리에서 제외한다", async () => { | ||
| let capturedUrl = ""; | ||
|
|
||
| server.use( | ||
| http.get(`${TEST_API_BASE}/reviews`, ({ request }) => { | ||
| capturedUrl = request.url; | ||
| return HttpResponse.json(reviews.list()); | ||
| }), | ||
| ); | ||
|
|
||
| await getReviews( | ||
| new URLSearchParams({ | ||
| type: "여행", | ||
| size: "not-a-number", | ||
| }), | ||
| ); | ||
|
|
||
| const url = new URL(capturedUrl); | ||
|
|
||
| expect(url.searchParams.get("type")).toBe("여행"); | ||
| expect(url.searchParams.has("size")).toBe(false); | ||
| }); | ||
|
|
||
| it("실패 시 응답 메시지와 status를 담은 ApiError를 던진다", async () => { | ||
| server.use( | ||
| http.get(`${TEST_API_BASE}/reviews`, () => | ||
| HttpResponse.json( | ||
| { message: "리뷰 목록 조회에 실패했습니다.", code: "REVIEWS_FETCH_FAILED" }, | ||
| { status: 400 }, | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| await expect(getReviews()).rejects.toMatchObject<ApiError>({ | ||
| name: "ApiError", | ||
| message: "리뷰 목록 조회에 실패했습니다.", | ||
| status: 400, | ||
| code: "REVIEWS_FETCH_FAILED", | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getReviewsStatistics", () => { | ||
| it("성공 시 전체 평점 요약을 반환한다", async () => { | ||
| server.use( | ||
| http.get(`${TEST_API_BASE}/reviews/statistics`, () => HttpResponse.json(STATISTICS)), | ||
| ); | ||
|
|
||
| await expect(getReviewsStatistics()).resolves.toEqual(STATISTICS); | ||
| }); | ||
|
|
||
| it("에러 응답 바디가 없으면 기본 메시지로 ApiError를 던진다", async () => { | ||
| server.use( | ||
| http.get( | ||
| `${TEST_API_BASE}/reviews/statistics`, | ||
| () => new HttpResponse(null, { status: 500 }), | ||
| ), | ||
| ); | ||
|
|
||
| await expect(getReviewsStatistics()).rejects.toMatchObject<ApiError>({ | ||
| name: "ApiError", | ||
| message: "리뷰 전체 통계 조회에 실패했습니다.", | ||
| status: 500, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getReviewsCategoriesStatistics", () => { | ||
| it("성공 시 카테고리별 평점 통계를 반환한다", async () => { | ||
| server.use( | ||
| http.get(`${TEST_API_BASE}/reviews/categories/statistics`, () => | ||
| HttpResponse.json(CATEGORY_STATISTICS), | ||
| ), | ||
| ); | ||
|
|
||
| const result = await getReviewsCategoriesStatistics(); | ||
|
|
||
| expect(Array.isArray(result)).toBe(true); | ||
| expect(result[0]).toMatchObject({ | ||
| type: CATEGORY_STATISTICS[0].type, | ||
| averageScore: CATEGORY_STATISTICS[0].averageScore, | ||
| totalReviews: CATEGORY_STATISTICS[0].totalReviews, | ||
| }); | ||
| expect(result).toHaveLength(CATEGORY_STATISTICS.length); | ||
| }); | ||
|
|
||
| it("실패 시 응답 메시지와 status를 담은 ApiError를 던진다", async () => { | ||
| server.use( | ||
| http.get(`${TEST_API_BASE}/reviews/categories/statistics`, () => | ||
| HttpResponse.json( | ||
| { message: "카테고리별 리뷰 통계를 불러오지 못했습니다." }, | ||
| { status: 503 }, | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| await expect(getReviewsCategoriesStatistics()).rejects.toMatchObject<ApiError>({ | ||
| name: "ApiError", | ||
| message: "카테고리별 리뷰 통계를 불러오지 못했습니다.", | ||
| status: 503, | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or 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,5 @@ | ||
| export const FAVORITED_AT_BY_MEETING_ID: Record<number, string> = { | ||
| 2: "2026-02-18T09:00:00.000Z", | ||
| 4: "2026-03-02T08:30:00.000Z", | ||
| 7: "2026-03-08T07:45:00.000Z", | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.