-
Notifications
You must be signed in to change notification settings - Fork 0
[Feat] 팔로우 컴포넌트 테스트 코드 작성 #215
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
7 commits
Select commit
Hold shift + click to select a range
313dc3e
feat: follow 관련 test 코드 작성
wooktori 111e3c5
feat: follow search 컴포넌트 test 작성
wooktori 6cbe91e
fix: followingNone 컴포넌트 테스트 코드 수정
wooktori 0fd0d07
fix: following Search 컴포넌트 분리
wooktori 29446e3
fix: following Search 테스트 수정
wooktori c7c54a8
fix: follow 테스트 코드 수정
wooktori 18c27fc
Merge branch 'main' into hwanwook-feat/follow-test
wooktori 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,43 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
|
|
||
| import { ModalProvider } from '@/components/ui'; | ||
| import { useInfiniteScroll } from '@/hooks/use-group/use-group-infinite-list'; | ||
|
|
||
| import FollowingPage from './page'; | ||
|
|
||
| jest.mock('@/hooks/use-group/use-group-infinite-list', () => ({ | ||
| useInfiniteScroll: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('next/navigation', () => ({ | ||
| useSearchParams: () => ({ | ||
| get: () => 'following', | ||
| }), | ||
| })); | ||
|
|
||
| jest.mock('js-cookie', () => ({ | ||
| get: () => '1', | ||
| })); | ||
|
|
||
| describe('FollowingPage 테스트', () => { | ||
| beforeEach(() => { | ||
| (useInfiniteScroll as jest.Mock).mockReturnValue({ | ||
| items: [], | ||
| error: null, | ||
| fetchNextPage: jest.fn(), | ||
| hasNextPage: false, | ||
| isFetchingNextPage: false, | ||
| completedMessage: '', | ||
| }); | ||
| }); | ||
|
|
||
| test('팔로잉이 없을 경우 FollowingNone을 보여준다', async () => { | ||
| render( | ||
| <ModalProvider> | ||
| <FollowingPage /> | ||
| </ModalProvider>, | ||
| ); | ||
|
|
||
| expect(await screen.findByText('아직 팔로우 한 사람이 없어요.')).toBeInTheDocument(); | ||
| }); | ||
| }); |
79 changes: 79 additions & 0 deletions
79
src/components/pages/message/message-following-modal/index.test.tsx
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,79 @@ | ||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
| import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'; | ||
|
|
||
| import { ModalProvider } from '@/components/ui'; | ||
| import { useAddFollowers } from '@/hooks/use-follower'; | ||
|
|
||
| import { FollowingModal } from '.'; | ||
|
|
||
| // Mock 설정 | ||
| jest.mock('@/hooks/use-follower'); | ||
|
|
||
| const createQueryClient = () => | ||
| new QueryClient({ | ||
| defaultOptions: { | ||
| queries: { retry: false }, | ||
| mutations: { retry: false }, | ||
| }, | ||
| }); | ||
|
|
||
| const renderWithQueryClient = async (component: React.ReactElement) => { | ||
| const testQueryClient = createQueryClient(); | ||
| let renderResult; | ||
|
|
||
| await act(async () => { | ||
| renderResult = render( | ||
| <QueryClientProvider client={testQueryClient}> | ||
| <ModalProvider>{component}</ModalProvider> | ||
| </QueryClientProvider>, | ||
| ); | ||
| }); | ||
|
|
||
| return renderResult; | ||
| }; | ||
|
|
||
| describe('FollowingModal 테스트', () => { | ||
| const mockMutate = jest.fn(); | ||
| const mockUserId = 123; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
|
|
||
| // 기본 mock 설정 | ||
| (useAddFollowers as jest.Mock).mockReturnValue({ | ||
| mutate: mockMutate, | ||
| }); | ||
| }); | ||
|
|
||
| test('FollowingModal 렌더링 테스트', async () => { | ||
| await renderWithQueryClient(<FollowingModal userId={mockUserId} />); | ||
|
|
||
| expect(screen.getByText('팔로우 할 닉네임을 입력하세요')).toBeInTheDocument(); | ||
| expect(screen.getByPlaceholderText('nickname')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| test('닉네임 입력이 정상적으로 동작한다', async () => { | ||
| await renderWithQueryClient(<FollowingModal userId={mockUserId} />); | ||
|
|
||
| const input = screen.getByPlaceholderText('nickname'); | ||
| fireEvent.change(input, { target: { value: 'test' } }); | ||
|
|
||
| expect(input).toHaveValue('test'); | ||
| }); | ||
|
|
||
| test('Enter 키 입력 시 폼이 제출된다', async () => { | ||
| mockMutate.mockImplementation((_data, options) => { | ||
| options?.onSuccess?.(); | ||
| }); | ||
|
|
||
| await renderWithQueryClient(<FollowingModal userId={mockUserId} />); | ||
|
|
||
| const input = screen.getByRole('textbox'); | ||
| fireEvent.change(input, { target: { value: 'test' } }); | ||
| fireEvent.keyDown(input, { key: 'Enter', code: 'Enter' }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(mockMutate).toHaveBeenCalledWith({ followNickname: 'test' }, expect.any(Object)); | ||
| }); | ||
| }); | ||
| }); |
82 changes: 82 additions & 0 deletions
82
src/components/pages/message/message-following-modal/index.tsx
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,82 @@ | ||
| import { useState } from 'react'; | ||
|
|
||
| import { useForm } from '@tanstack/react-form'; | ||
|
|
||
| import { Icon } from '@/components/icon'; | ||
| import { Button, Input, ModalContent, ModalTitle, useModal } from '@/components/ui'; | ||
| import { useAddFollowers } from '@/hooks/use-follower'; | ||
|
|
||
| export const FollowingModal = ({ userId }: { userId: number }) => { | ||
| const { close } = useModal(); | ||
| const [errorMessage, setErrorMessage] = useState<string | null>(null); | ||
| const { mutate: addFollower } = useAddFollowers({ userId }); | ||
| const form = useForm({ | ||
| defaultValues: { | ||
| nickname: '', | ||
| }, | ||
| onSubmit: ({ value }) => { | ||
| const { nickname } = value; | ||
| setErrorMessage(null); | ||
|
|
||
| addFollower( | ||
| { | ||
| followNickname: nickname, | ||
| }, | ||
| { | ||
| onSuccess: () => { | ||
| close(); | ||
| }, | ||
| onError: () => { | ||
| setErrorMessage('존재하지 않는 유저입니다.'); | ||
| }, | ||
| }, | ||
| ); | ||
| }, | ||
| }); | ||
| return ( | ||
| <ModalContent className='mx-8'> | ||
| <ModalTitle className='mb-3'>팔로우 할 닉네임을 입력하세요</ModalTitle> | ||
| <form | ||
| onSubmit={(e) => { | ||
| e.preventDefault(); | ||
| form.handleSubmit(); | ||
| }} | ||
| > | ||
| <div className='mb-3'> | ||
| <form.Field | ||
| children={(field) => ( | ||
| <Input | ||
| className='text-text-sm-medium w-full rounded-3xl bg-gray-100 px-4 py-2.5 text-gray-800' | ||
| iconButton={ | ||
| <Icon id='search' className='absolute top-2.5 right-3 size-5 text-gray-500' /> | ||
| } | ||
| placeholder='nickname' | ||
| value={field.state.value} | ||
| onChange={(e) => { | ||
| field.handleChange(e.target.value); | ||
| setErrorMessage(null); | ||
| }} | ||
| onKeyDown={(e) => { | ||
| if (e.key === 'Enter') { | ||
| e.preventDefault(); | ||
| form.handleSubmit(); | ||
| } | ||
| }} | ||
| /> | ||
| )} | ||
| name='nickname' | ||
| ></form.Field> | ||
| </div> | ||
| {errorMessage && <p className='text-error-500 mb-2 ml-2 text-sm'>{errorMessage}</p>} | ||
| <div className='flex w-full flex-row gap-2'> | ||
| <Button size='sm' type='button' variant='tertiary' onClick={close}> | ||
| 취소 | ||
| </Button> | ||
| <Button size='sm' type='submit'> | ||
| 팔로우 | ||
| </Button> | ||
| </div> | ||
| </form> | ||
| </ModalContent> | ||
| ); | ||
| }; |
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
74 changes: 3 additions & 71 deletions
74
src/components/pages/message/message-following-search/index.tsx
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
message-following-search 컴포넌트를 지금봤는데, 이 파일 내부에서 모달과 팔로우 추가 버튼 둘다 관리하고 있네용
프로젝트 통일성을 위해 이 정도로 수정되면 좋을 것 같습니다!