-
Notifications
You must be signed in to change notification settings - Fork 120
5주차 코드리뷰 요청 #126
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
Open
ctaaag
wants to merge
17
commits into
CodeSoom:main
Choose a base branch
from
ctaaag:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
5주차 코드리뷰 요청 #126
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f9bae85
ADD: RegionContainer test complete
ctaaag bb99cf9
ADD: redux 구조 세팅
ctaaag a87f515
Refactor:RegionContainer 테스트 수정
ctaaag edf2463
ADD:Region컨테이너 컴포넌트 완성
ctaaag c22d949
REFACTOR: RegionContainer 리팩토링
ctaaag 49d1a8b
ADD: Region 컴포넌트 및 테스트 완성
ctaaag 75af3f8
ADD: CategoriesContainer test 완성
ctaaag 7d411ec
ADD: CategoryConainer 완성
ctaaag d326475
ADD: Category 완성
ctaaag 81e4631
ADD: RestaurantContainer 완성
ctaaag 0282f88
ADD: Restaurant 컴포넌트 완성
ctaaag 3f8015d
ADD: App 테스트 추가
ctaaag 46edcc7
ADD: reducer 테스트 완성
ctaaag 4f8ccdb
Refactor : 변수명 및 try-catch문 수정
ctaaag 9a44c87
Refactor : 변수 및 함수명 변경
ctaaag 5a892b2
ADD: fixture로 테스트 데이터 분리
ctaaag 3f1ded5
test 코드 리팩토링 및 테스트 커버리지 향상
ctaaag 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
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,3 @@ | ||
| export const useDispatch = jest.fn((dispatch) => dispatch); | ||
|
|
||
| export const useSelector = jest.fn((selector) => selector({})); |
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,4 @@ | ||
| export const regions = [{ id: 1, name: '서울' }, { id: 2, name: '부산' }]; | ||
| export const categories = [{ id: 1, name: '한식' }, { id: 2, name: '양식' }]; | ||
| export const restaurants = [{ id: 1, name: '코코식당' }, { id: 2, name: '네네식당' }]; | ||
| export const selectedRegionAndCategory = { selectedRegion: { id: 1, name: '서울' }, selectedCategory: { id: 1, name: '한식' } }; |
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 |
|---|---|---|
| @@ -1,5 +1,14 @@ | ||
| import RegionContainer from './RegionContainer'; | ||
| import CategoriesContainer from './CategoriesContainer'; | ||
| import RestaurantsContainer from './RestaurantsContainer'; | ||
|
|
||
| export default function App() { | ||
| return ( | ||
| <></> | ||
| <> | ||
| <h1>Restaurants</h1> | ||
| <RegionContainer /> | ||
| <CategoriesContainer /> | ||
| <RestaurantsContainer /> | ||
| </> | ||
| ); | ||
| } |
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,78 @@ | ||
| import { fireEvent, render } from '@testing-library/react'; | ||
| import { useDispatch, useSelector } from 'react-redux'; | ||
| import App from './App'; | ||
| import { | ||
| regions, categories, restaurants, selectedRegionAndCategory, | ||
| } from '../fixtures/data'; | ||
|
|
||
| describe('App', () => { | ||
| jest.mock('react-redux'); | ||
| jest.mock('./services/api'); | ||
|
|
||
| const dispatch = jest.fn(); | ||
|
|
||
| useDispatch.mockImplementation(() => dispatch); | ||
| useSelector.mockImplementation(() => ({ | ||
| regions, | ||
| categories, | ||
| restaurants, | ||
| selectedRegionAndCategory, | ||
| })); | ||
|
|
||
| const renderApp = () => render(<App />); | ||
|
|
||
| describe('RegionContainer가 렌더링 된다.', () => { | ||
| it('지역정보들이 input으로 보인다.', () => { | ||
| const { getByText } = renderApp(); | ||
| regions.forEach((region) => expect(getByText(new RegExp(`[${region.name}]`))).not.toBeNull()); | ||
| }); | ||
|
|
||
| it('input 클릭시 상태가 업데이트된다.', () => { | ||
| const { getByText } = renderApp(); | ||
| fireEvent.click(getByText(/서울/)); | ||
| expect(dispatch).toBeCalled(); | ||
| }); | ||
|
|
||
| it('선택된 input은 v가 표시된다.', () => { | ||
| const { getByText } = renderApp(); | ||
| expect(getByText('서울 v')).not.toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('CategoryContainer가 렌더링 된다.', () => { | ||
| it('카테고리 목록이 input으로 보인다.', () => { | ||
| const { getByText } = renderApp(); | ||
| expect(getByText(/한식/)).not.toBeNull(); | ||
| expect(getByText(/양식/)).not.toBeNull(); | ||
| }); | ||
|
|
||
| it('카테고리 input 클릭시 상태가 업데이트된다.', () => { | ||
| const { getByText } = renderApp(); | ||
| fireEvent.click(getByText(/한식/)); | ||
| expect(dispatch).toBeCalled(); | ||
| }); | ||
|
|
||
| it('선택된 input은 v가 표시된다.', () => { | ||
| const { getByText } = renderApp(); | ||
| expect(getByText('한식 v')).not.toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('RestaurantContainer가 렌더링 된다.', () => { | ||
| it('불러온 레스토랑 데이터들이 보인다.', () => { | ||
| const { getByText } = renderApp(); | ||
| expect(getByText(/코코식당/)).not.toBeNull(); | ||
| expect(getByText(/네네식당/)).not.toBeNull(); | ||
| }); | ||
| it('선택된 식당이 바뀌면 ', () => { | ||
| useSelector.mockImplementation(() => ({ | ||
| restaurants: [ | ||
| { id: 1, name: '바뀐식당' }, { id: 2, name: '무슨식당' }, | ||
| ], | ||
| })); | ||
| const { getByText } = renderApp(); | ||
| expect(getByText('바뀐식당')).not.toBeNull(); | ||
| expect(getByText('무슨식당')).not.toBeNull(); | ||
| }); | ||
| }); | ||
| }); |
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,31 @@ | ||
| import { useEffect } from 'react'; | ||
| import { useDispatch, useSelector } from 'react-redux'; | ||
| import { loadCategories, setSelectedRegionAndCategory } from './action'; | ||
| import Category from './Category'; | ||
|
|
||
| export default function CategoriesContainer() { | ||
| const dispatch = useDispatch(); | ||
| const { categories, selectedRegionAndCategory } = useSelector((state) => ({ | ||
| categories: state.categories, | ||
| selectedRegionAndCategory: state.selectedRegionAndCategory, | ||
| })); | ||
|
|
||
| useEffect(() => { | ||
| dispatch(loadCategories()); | ||
| }, []); | ||
|
|
||
| function handleClickCategory(category) { | ||
| dispatch(setSelectedRegionAndCategory({ | ||
| selectedRegion: selectedRegionAndCategory?.selectedRegion, | ||
| selectedCategory: category, | ||
| })); | ||
| } | ||
|
|
||
| return ( | ||
| <Category | ||
| categories={categories} | ||
| selectedRegionAndCategory={selectedRegionAndCategory} | ||
| onClick={handleClickCategory} | ||
| /> | ||
| ); | ||
| } | ||
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,45 @@ | ||
| import { render, fireEvent } from '@testing-library/react'; | ||
| import { useDispatch, useSelector } from 'react-redux'; | ||
| import { categories } from '../fixtures/data'; | ||
| import { setSelectedRegionAndCategory } from './action'; | ||
| import CategoriesContainer from './CategoriesContainer'; | ||
|
|
||
| describe('CategoriesContainer', () => { | ||
| const dispatch = jest.fn(); | ||
| const renderCategoriesContainer = () => render(<CategoriesContainer />); | ||
|
|
||
| jest.mock('react-redux'); | ||
| jest.mock('./services/api'); | ||
|
Comment on lines
+11
to
+12
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이건 describe위에 선언해도 좋을 것 같아요. 이게 여기 안에 있으면 테스트에 따라서 다르게 바뀔수도 있다고 오해가 생길 것 같습니다
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 알겠습니닷 |
||
|
|
||
| useSelector.mockImplementation(() => ({ categories })); | ||
|
|
||
| useDispatch.mockImplementation(() => dispatch); | ||
|
|
||
| it('데이터 받기', () => { | ||
| const { getByText } = renderCategoriesContainer(); | ||
| expect(getByText(categories[0].name)).not.toBeNull(); | ||
| }); | ||
|
|
||
| describe('button 클릭이 되면', () => { | ||
| it('클릭 된 버튼의 데이터를 state에 저장한다', () => { | ||
| const { getByText } = renderCategoriesContainer(); | ||
| // expect(dispatch).not.toBeCalled(); | ||
| fireEvent.click(getByText(categories[0].name)); | ||
| expect(dispatch).toBeCalledWith(setSelectedRegionAndCategory({ | ||
| selectedRegion: undefined, | ||
| selectedCategory: categories[0], | ||
| })); | ||
| }); | ||
| }); | ||
|
|
||
| // it('dispatch가 실행된다', () => { | ||
| // const category = categories[0]; | ||
| // renderCategoriesContainer(category); | ||
|
|
||
| // expect(dispatch).not.toBeCalled(); | ||
|
|
||
| // fireEvent.click(screen.getByText(category.name)); | ||
|
|
||
| // expect(dispatch).toBeCalledWith(selectCategory(category.id)); | ||
| // }); | ||
| }); | ||
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,15 @@ | ||
| export default function Category({ categories, selectedRegionAndCategory, onClick }) { | ||
| return ( | ||
| <> | ||
| {categories?.map((category) => ( | ||
| <button | ||
| type="button" | ||
| key={category.id} | ||
| onClick={() => onClick(category)} | ||
| > | ||
| {selectedRegionAndCategory?.selectedCategory?.name === category.name ? `${category.name} v` : category.name} | ||
| </button> | ||
| ))} | ||
| </> | ||
| ); | ||
| } |
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,29 @@ | ||
| import { render, fireEvent } from '@testing-library/react'; | ||
| import Category from './Category'; | ||
| import { categories, selectedRegionAndCategory } from '../fixtures/data'; | ||
|
|
||
| describe('Region', () => { | ||
| const onClick = jest.fn(); | ||
|
|
||
| const renderCategory = () => render( | ||
| <Category | ||
| categories={categories} | ||
| selectedRegionAndCategory={selectedRegionAndCategory} | ||
| onClick={onClick} | ||
| />, | ||
| ); | ||
|
|
||
| it('지역 input이 보인다.', () => { | ||
| const { getByText } = renderCategory(); | ||
| expect(getByText('양식')).not.toBeNull(); | ||
| }); | ||
| it('지역 input을 클릭하면 함수를 호출한다.', () => { | ||
| const { getByText } = renderCategory(); | ||
| fireEvent.click(getByText('양식')); | ||
| expect(onClick).toBeCalledWith({ id: 2, name: '양식' }); | ||
| }); | ||
| it('선택된 input 옆에는 체크표시가 된다.', () => { | ||
| const { getByText } = renderCategory(); | ||
| expect(getByText('한식 v')).not.toBeNull(); | ||
| }); | ||
| }); |
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,15 @@ | ||
| export default function Region({ regions, selectedRegionAndCategory, onClick }) { | ||
| return ( | ||
| <div> | ||
| {regions?.map((item) => ( | ||
| <button | ||
| type="button" | ||
| key={item.id} | ||
| onClick={() => onClick(item)} | ||
| > | ||
| {selectedRegionAndCategory?.selectedRegion?.name === item.name ? `${item.name} v` : item.name} | ||
| </button> | ||
| ))} | ||
| </div> | ||
| ); | ||
| } |
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,29 @@ | ||
| import { render, fireEvent } from '@testing-library/react'; | ||
| import Region from './Region'; | ||
| import { regions, selectedRegionAndCategory } from '../fixtures/data'; | ||
|
|
||
| describe('Region', () => { | ||
| const onClick = jest.fn(); | ||
|
|
||
| const renderRegion = () => render( | ||
| <Region | ||
| regions={regions} | ||
| selectedRegionAndCategory={selectedRegionAndCategory} | ||
| onClick={onClick} | ||
| />, | ||
| ); | ||
|
|
||
| it('지역 input이 보인다.', () => { | ||
| const { getByText } = renderRegion(); | ||
| expect(getByText('부산')).not.toBeNull(); | ||
| }); | ||
| it('지역 input을 클릭하면 함수를 호출한다.', () => { | ||
| const { getByText } = renderRegion(); | ||
| fireEvent.click(getByText('부산')); | ||
| expect(onClick).toBeCalledWith({ id: 2, name: '부산' }); | ||
| }); | ||
| it('선택된 input 옆에는 체크표시가 된다.', () => { | ||
| const { getByText } = renderRegion(); | ||
| expect(getByText('서울 v')).not.toBeNull(); | ||
| }); | ||
| }); |
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,32 @@ | ||
| import { useEffect } from 'react'; | ||
| import { useDispatch, useSelector } from 'react-redux'; | ||
| import { setSelectedRegionAndCategory, loadRegions } from './action'; | ||
| import Region from './Region'; | ||
|
|
||
| export default function RegionContainer() { | ||
| // 데이터 페칭하기 | ||
| const dispatch = useDispatch(); | ||
| const { regions, selectedRegionAndCategory } = useSelector((state) => ({ | ||
| regions: state.regions, | ||
| selectedRegionAndCategory: state.selectedRegionAndCategory, | ||
| })); | ||
|
|
||
| useEffect(() => { | ||
| dispatch(loadRegions()); | ||
| }, []); | ||
|
|
||
| function handleClickButton(item) { | ||
| dispatch(setSelectedRegionAndCategory({ | ||
| selectedCategory: selectedRegionAndCategory?.selectedCategory, | ||
| selectedRegion: item, | ||
| })); | ||
| } | ||
|
|
||
| return ( | ||
| <Region | ||
| regions={regions} | ||
| selectedRegionAndCategory={selectedRegionAndCategory} | ||
| onClick={handleClickButton} | ||
| /> | ||
| ); | ||
| } |
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,22 @@ | ||
| import { fireEvent, render } from '@testing-library/react'; | ||
| import { useDispatch, useSelector } from 'react-redux'; | ||
| import { regions } from '../fixtures/data'; | ||
| import RegionContainer from './RegionContainer'; | ||
|
|
||
| describe('RegionContainer', () => { | ||
| const dispatch = jest.fn(); | ||
| jest.mock('react-redux'); | ||
| jest.mock('./services/api'); | ||
| useDispatch.mockImplementation(() => dispatch); | ||
| useSelector.mockImplementation(() => ({ regions })); | ||
|
|
||
| const renderRegionContainer = () => render(<RegionContainer />); | ||
|
|
||
| it('데이터 받기', () => { | ||
| const { getByText } = renderRegionContainer(); | ||
|
|
||
| expect(getByText('서울')).not.toBeNull(); | ||
| fireEvent.click(getByText('서울')); | ||
| expect(dispatch).toBeCalled(); | ||
| }); | ||
| }); |
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,7 @@ | ||
| export default function Restaurants({ restaurants }) { | ||
| return ( | ||
| <> | ||
| {restaurants?.map((restaurant) => <li key={restaurant.id}>{restaurant.name}</li>)} | ||
| </> | ||
| ); | ||
| } |
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,13 @@ | ||
| import { render } from '@testing-library/react'; | ||
| import Restaurants from './Restaurants'; | ||
| import { restaurants } from '../fixtures/data'; | ||
|
|
||
| describe('RestaurantsContainer', () => { | ||
| const renderRestaurants = () => render(<Restaurants restaurants={restaurants} />); | ||
|
|
||
| it('식당 리스트가 보인다', () => { | ||
| const { getByText } = renderRestaurants(); | ||
| expect(getByText('코코식당')).not.toBeNull(); | ||
| expect(getByText('네네식당')).not.toBeNull(); | ||
| }); | ||
| }); |
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,29 @@ | ||
| import { useEffect } from 'react'; | ||
| import { useDispatch, useSelector } from 'react-redux'; | ||
| import { loadRestaurants } from './action'; | ||
| import Restaurants from './Restaurants'; | ||
|
|
||
| export default function RestaurantsContainer() { | ||
| const { restaurants, selectedRegion, selectedCategory } = useSelector((state) => ({ | ||
| restaurants: state.restaurants, | ||
| selectedRegion: state.selectedRegionAndCategory.selectedRegion, | ||
| selectedCategory: state.selectedRegionAndCategory.selectedCategory, | ||
| })); | ||
|
|
||
| const dispatch = useDispatch(); | ||
|
|
||
| useEffect(() => { | ||
| const isEmptyObject = (object) => Object.keys(object || {}).length === 0; | ||
|
|
||
| if (isEmptyObject(selectedRegion) || isEmptyObject(selectedCategory)) { | ||
| return; | ||
| } | ||
| dispatch(loadRestaurants(selectedRegion, selectedCategory)); | ||
| }, [selectedRegion, selectedCategory]); | ||
|
|
||
| return ( | ||
| <ul> | ||
| <Restaurants restaurants={restaurants} /> | ||
| </ul> | ||
| ); | ||
| } |
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.
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.
여기 위에 빈 줄 하나를 추가해서 구분해주면 좋을 것 같아요
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.
넵 반영하겠습니다 :)