-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FE] 리뷰 상세페이지 query 훅 분리 및 HTTP 요청 테스트 진행 (#216)
* refactor: DetailedPage/index.tsx 리팩토링 - early return를 사용해 코드의 가독성을 높임 * feat: useGetDetailedReview 훅 생성 및 DetailedReviewPageContents에 적용 * feat: useSearchParamAndQuery 훅 생성 및 DetailedPageContent에 적용 * refactor: 리뷰 상세페이지에서 id라고 사용했던 key값, params의 key를 reviewId로 변경 - DetailedReview의 router param을 id에서 reviewId로 변경 * ci: dependencies에서 jest 삭제 및 ts-jest 설치 * ci: jest에서 절대 경로 사용할 수 있도록 jest.config.js 추가 * chore: eslint적용 제외 파일에 jest.config.js, tsconfig.json 추가 * ci: jest의 testEnvioronment를 jsdom으로 설정 * refactor: useGetDetailedReview에서 query 결과를 모두 반환하는 방식으로 변경 * fix: jest에서 msw ver2를 목서버로 사용 시 생기는 오류 수정 1. msw/node 를 읽지 못함 - jest.config.js의 testEnvironment 빈문자열 2. ReferentError: TextEnCoder is not defined - 해결 : jest.polyfills.js 추가 및 undici 설치 3. ReferenceError: ReadableStream is not defined - 해결 : undici 다운 그레이드 undici": "^6.19.5", -> "^5.0.0" * ci : jest에서 env 파일 읽을 수 있도록 dotenv 설치 및 jest에 적용 * fix: mock 핸들러인 getDetailedReview 에서 중복된 쿼리 매개 변수 사용 수정 - 오류 상황: jest에서 msw 사용 시, get의 url에 파라미터 사용 시 중복된 쿼리 매개 변수 오류가 남 - 오류 메세지 ::Found a redundant usage of query parameters in the request handler - 해결: 리뷰 상세보기 페이지의 reviews까지의 url 상수를 만들고, get에서는 이 상수를 활용한 정규표현식으로 리뷰 상세보기 페이지로 오는 모든 요청을 가로챌 수 있도록 함 * refactor: getWrongDetailedReview 목서버 핸들러 및 관련 상수 삭제 - getDetailedReview에서 request를 분석해 http오류 여부를 결정함 * feat: queryClientWrapper 생성 - queryClientWrapper : msw를 사용한 jest 테스트에 queryWrapper로 사용 * test:리뷰 상세 페이지 api 요청 성공에 대한 테스트 추가 * fix: groupAccessCodeAtom의 기본값 원래대로 복구 * chore:queryClientWrapper 네이밍 표기법을 파스칼 케이스로 변경 * fix: 머지 충돌 방지를 위해 yarn.lock 삭제 * fix: 머지 시 yarn.lock 충돌 해결
- Loading branch information
1 parent
4039156
commit 36ebd7a
Showing
11 changed files
with
127 additions
and
59 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
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export { default as useSidebar } from './useSidebar'; | ||
export { default as useModalClose } from './useModalClose'; | ||
export { default as useGroupAccessCode } from './useGroupAccessCode'; | ||
export { default as useGetDetailedReview } from './review/useGetDetailedReview'; | ||
export { default as useSearchParamAndQuery } from './useSearchParamAndQuery'; |
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,33 @@ | ||
import { useSuspenseQuery } from '@tanstack/react-query'; | ||
|
||
import { getDetailedReviewApi } from '@/apis/review'; | ||
import { REVIEW_QUERY_KEYS } from '@/constants'; | ||
import { DetailReviewData } from '@/types'; | ||
|
||
interface UseGetDetailedReviewProps { | ||
reviewId: number; | ||
memberId: number; | ||
groupAccessCode: string; | ||
} | ||
|
||
interface FetchDetailedReviewParams { | ||
reviewId: number; | ||
memberId: number; | ||
groupAccessCode: string; | ||
} | ||
|
||
const useGetDetailedReview = ({ reviewId, memberId, groupAccessCode }: UseGetDetailedReviewProps) => { | ||
const fetchDetailedReview = async ({ reviewId, memberId, groupAccessCode }: FetchDetailedReviewParams) => { | ||
const result = await getDetailedReviewApi({ reviewId, memberId, groupAccessCode }); | ||
return result; | ||
}; | ||
|
||
const result = useSuspenseQuery<DetailReviewData>({ | ||
queryKey: [REVIEW_QUERY_KEYS.detailedReview, reviewId, memberId], | ||
queryFn: () => fetchDetailedReview({ reviewId, memberId, groupAccessCode }), | ||
}); | ||
|
||
return result; | ||
}; | ||
|
||
export default useGetDetailedReview; |
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,26 @@ | ||
import { renderHook, waitFor } from '@testing-library/react'; | ||
|
||
import { DETAILED_PAGE_MOCK_API_SETTING_VALUES } from '@/mocks/mockData/detailedReviewMockData'; | ||
import QueryClientWrapper from '@/queryTestSetup/QueryClientWrapper'; | ||
|
||
import useGetDetailedReview from '.'; | ||
// 아래의 테스트는 로그인이 유효하다는 가정하에서 진행 | ||
|
||
describe('리뷰 상세페이지 데이터 요청 테스트', () => { | ||
const GROUND_ACCESS_CODE = '1234'; | ||
|
||
it('유효힌 id,memberId 사용해야 라뷰 상세 페이지 데이터를 불러온다.', async () => { | ||
const { reviewId, memberId } = DETAILED_PAGE_MOCK_API_SETTING_VALUES; | ||
|
||
const { result } = renderHook( | ||
() => useGetDetailedReview({ reviewId, memberId, groupAccessCode: GROUND_ACCESS_CODE }), | ||
{ wrapper: QueryClientWrapper }, | ||
); | ||
|
||
await waitFor(() => { | ||
expect(result.current.status).toBe('success'); | ||
}); | ||
|
||
expect(result.current.data).toBeDefined(); | ||
}); | ||
}); |
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,22 @@ | ||
import { useLocation, useParams } from 'react-router'; | ||
|
||
interface UseSearchParamAndQueryProps { | ||
paramKey: string; | ||
queryStringKey?: string; | ||
} | ||
/** | ||
* url에서 원하는 param, queryString의 값을 가져온다. | ||
* @param paramKey: 가져오고 싶은 param의 key | ||
* @param queryStringKey: 가져오고 싶은 queryString의 key (옵셔널) | ||
*/ | ||
const useSearchParamAndQuery = ({ paramKey, queryStringKey }: UseSearchParamAndQueryProps) => { | ||
const location = useLocation(); | ||
const queryParams = new URLSearchParams(location.search); | ||
|
||
return { | ||
param: useParams()[`${paramKey}`], | ||
queryString: queryStringKey ? queryParams.get(queryStringKey) : null, | ||
}; | ||
}; | ||
|
||
export default useSearchParamAndQuery; |
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
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
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
30 changes: 9 additions & 21 deletions
30
frontend/src/pages/DetailedReviewPage/components/DetailedReviewPageContents/index.tsx
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
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
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,11 @@ | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
|
||
import { EssentialPropsWithChildren } from '@/types'; | ||
|
||
const queryClient = new QueryClient(); | ||
|
||
const QueryClientWrapper = ({ children }: EssentialPropsWithChildren) => ( | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
); | ||
|
||
export default QueryClientWrapper; |