-
Notifications
You must be signed in to change notification settings - Fork 128
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
4주차 코드리뷰 요청 #146
Open
ctaaag
wants to merge
4
commits into
CodeSoom:ctaaag
Choose a base branch
from
ctaaag:main
base: ctaaag
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
4주차 코드리뷰 요청 #146
Changes from all commits
Commits
Show all changes
4 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 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(); | ||
|
||
export const useSelector = jest.fn((selector) => selector({})); |
Large diffs are not rendered by default.
Oops, something went wrong.
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,46 +1,13 @@ | ||
import { useState } from 'react'; | ||
|
||
import Page from './Page'; | ||
import InputContainer from './InputContainer'; | ||
import ListContainer from './ListContainer'; | ||
|
||
export default function App() { | ||
const [state, setState] = useState({ | ||
newId: 100, | ||
taskTitle: '', | ||
tasks: [], | ||
}); | ||
|
||
const { newId, taskTitle, tasks } = state; | ||
|
||
function handleChangeTitle(event) { | ||
setState({ | ||
...state, | ||
taskTitle: event.target.value, | ||
}); | ||
} | ||
|
||
function handleClickAddTask() { | ||
setState({ | ||
...state, | ||
newId: newId + 1, | ||
taskTitle: '', | ||
tasks: [...tasks, { id: newId, title: taskTitle }], | ||
}); | ||
} | ||
|
||
function handleClickDeleteTask(id) { | ||
setState({ | ||
...state, | ||
tasks: tasks.filter((task) => task.id !== id), | ||
}); | ||
} | ||
|
||
return ( | ||
<Page | ||
taskTitle={taskTitle} | ||
onChangeTitle={handleChangeTitle} | ||
onClickAddTask={handleClickAddTask} | ||
tasks={tasks} | ||
onClickDeleteTask={handleClickDeleteTask} | ||
/> | ||
<> | ||
<h1>To-do</h1> | ||
<InputContainer /> | ||
<ListContainer /> | ||
</> | ||
|
||
); | ||
} |
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,26 @@ | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import Input from './Input'; | ||
import { updateTaskTitle, addTask } from './reducers'; | ||
|
||
export default function InputContainer() { | ||
const { taskTitle } = useSelector((state) => ({ | ||
taskTitle: state.taskTitle, | ||
})); | ||
const dispatch = useDispatch(); | ||
// 관심사분리 | ||
function handleChangeTitle(event) { | ||
dispatch(updateTaskTitle({ taskTitle: event.target.value })); | ||
} | ||
|
||
function handleClickAddTask() { | ||
dispatch(addTask()); | ||
} | ||
|
||
return ( | ||
<Input | ||
value={taskTitle} | ||
onChange={handleChangeTitle} | ||
onClick={handleClickAddTask} | ||
/> | ||
); | ||
} |
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,28 @@ | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
|
||
import InputContainer from './InputContainer'; | ||
|
||
jest.mock('react-redux'); | ||
|
||
test('InputContainer', () => { | ||
const dispatch = jest.fn(); | ||
|
||
useDispatch.mockImplementation(() => dispatch); | ||
|
||
useSelector.mockImplementation((selector) => selector({ | ||
taskTitle: 'new Title', | ||
})); | ||
|
||
const { getByText, getByDisplayValue } = render(( | ||
<InputContainer /> | ||
)); | ||
|
||
expect(getByText(/추가/)).not.toBeNull(); | ||
expect(getByDisplayValue(/new Title/)).not.toBeNull(); | ||
|
||
fireEvent.click(getByText('추가')); | ||
expect(dispatch).toBeCalledWith({ | ||
type: 'addTask', | ||
}); | ||
}); |
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 { useDispatch, useSelector } from 'react-redux'; | ||
import { deleteTask } from './reducers'; | ||
import List from './List'; | ||
|
||
export default function ListContainer() { | ||
const { tasks } = useSelector((state) => ({ | ||
tasks: state.tasks, | ||
})); | ||
const dispatch = useDispatch(); | ||
// 관심사분리 | ||
|
||
function handleClickDeleteTask(id) { | ||
dispatch((deleteTask({ id }))); | ||
} | ||
|
||
return ( | ||
<List | ||
tasks={tasks} | ||
onClickDelete={handleClickDeleteTask} | ||
/> | ||
); | ||
} |
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 { render } from '@testing-library/react'; | ||
import { useSelector } from 'react-redux'; | ||
|
||
import ListContainer from './ListContainer'; | ||
|
||
jest.mock('react-redux'); | ||
|
||
test('ListContainer', () => { | ||
const tasks = [ | ||
{ id: 1, title: '아무것도 하지 않기#1' }, | ||
{ id: 2, title: '아무것도 하지 않기#2' }, | ||
]; | ||
|
||
useSelector.mockImplementation((selector) => selector({ | ||
tasks, | ||
})); | ||
|
||
const { getByText } = render(( | ||
<ListContainer /> | ||
)); | ||
|
||
expect(getByText(/아무것도 하지 않기#1/)).not.toBeNull(); | ||
|
||
// TODO: 통합 테스트 코드 작성 | ||
// CodeceptJS => 실제 브라우저에서 사용자 테스트 실행 가능. | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,32 @@ | ||
import { createAction } from 'redux-actions'; | ||
|
||
const UPDATE_TASK_TITLE = 'task/UPDATE_TASK_TITLE'; | ||
const ADD_TASK = 'task/ADD_TASK'; | ||
const DELETE_TASK = 'task/DELETE_TASK'; | ||
|
||
export const updateTaskTitle = createAction(UPDATE_TASK_TITLE); | ||
export const addTask = createAction(ADD_TASK); | ||
export const deleteTask = createAction(DELETE_TASK); | ||
|
||
// export const setDiff = diff => ({ type: SET_DIFF, diff }); | ||
// export const increase = () => ({ type: INCREASE }); | ||
// export const decrease = () => ({ type: DECREASE }); | ||
// function updateTaskTitle(taskTitle) { | ||
// return { | ||
// type: 'UPDATE_TASK_TITLE', | ||
// payload: { taskTitle }, | ||
// }; | ||
// } | ||
|
||
// export function addTask() { | ||
// return { | ||
// type: 'ADD_TASK', | ||
// }; | ||
// } | ||
|
||
// export function deleteTask(id) { | ||
// return { | ||
// type: 'DELETE_TASK', | ||
// payload: { id }, | ||
// }; | ||
// } |
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,8 +1,15 @@ | ||
import ReactDOM from 'react-dom'; | ||
|
||
import { Provider } from 'react-redux'; | ||
import App from './App'; | ||
|
||
import store from './store'; | ||
|
||
ReactDOM.render( | ||
<App />, | ||
( | ||
<Provider store={store}> | ||
<App /> | ||
</Provider> | ||
), | ||
document.getElementById('app'), | ||
); |
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 { handleActions, createAction } from 'redux-actions'; | ||
|
||
const UPDATE_TASK_TITLE = 'task/UPDATE_TASK_TITLE'; | ||
const ADD_TASK = 'task/ADD_TASK'; | ||
const DELETE_TASK = 'task/DELETE_TASK'; | ||
|
||
export const updateTaskTitle = createAction(UPDATE_TASK_TITLE); | ||
export const addTask = createAction(ADD_TASK); | ||
export const deleteTask = createAction(DELETE_TASK); | ||
|
||
const initialState = { newId: 100, taskTitle: '', tasks: [] }; | ||
|
||
const reducer = handleActions({ | ||
[UPDATE_TASK_TITLE]: (state, { payload }) => ({ | ||
...state, | ||
taskTitle: payload.taskTitle, | ||
}), | ||
[ADD_TASK]: (state) => { | ||
const { newId, tasks, taskTitle } = state; | ||
return { | ||
...state, | ||
newId: newId + 1, | ||
taskTitle: '', | ||
tasks: [...tasks, { id: newId, title: taskTitle }], | ||
}; | ||
}, | ||
[DELETE_TASK]: (state, { payload }) => { | ||
const { tasks } = state; | ||
return { | ||
...state, | ||
tasks: tasks.filter((task) => task.id !== payload.id), | ||
}; | ||
}, | ||
}, initialState); | ||
|
||
export default reducer; |
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,44 @@ | ||
import reducer, { updateTaskTitle, addTask, deleteTask } from './reducers'; | ||
|
||
// 테스트하고자 하는 것의 input과 output을 명확하게 해야하지 | ||
// input? 변경 전 state,action | ||
// output? 변경 후 state | ||
|
||
describe('reducer', () => { | ||
describe('taskTitle을 변경한다', () => { | ||
it('taskTitle이 변경된 상태를 반환한다.', () => { | ||
const prevState = { newId: 100, taskTitle: '', tasks: [] }; | ||
const state = reducer(prevState, updateTaskTitle('change')); | ||
expect(state.taskTitle).toBe('change'); | ||
}); | ||
}); | ||
describe('taskTitle을 추가한다', () => { | ||
context('taskTitle이 없는 경우', () => { | ||
const prevState = { | ||
newId: 100, taskTitle: '', tasks: [], | ||
}; | ||
const state = reducer(prevState, addTask()); | ||
expect(state.tasks).toHaveLength(0); | ||
}); | ||
context('taskTitle이 있는 경우', () => { | ||
const prevState = { | ||
newId: 100, taskTitle: 'something', tasks: [], | ||
}; | ||
const state = reducer(prevState, addTask()); | ||
expect(state.tasks[0].title).toBe('something'); | ||
expect(state.tasks).toHaveLength(1); | ||
expect(state.tasks[0].id).toBe(100); | ||
}); | ||
}); | ||
describe('task를 삭제한다', () => { | ||
it('tasks에서 해당 값을 제외한 상태를 반환한다.', () => { | ||
const prevState = { | ||
newId: 101, | ||
taskTitle: '', | ||
tasks: [{ id: 100, taskTitle: 'something' }], | ||
}; | ||
const state = reducer(prevState, deleteTask(100)); | ||
expect(state.tasks).toHaveLength(0); | ||
}); | ||
}); | ||
}); |
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.
reduc-actions를 사용하셨군요. redux-actions를 의도한 것은 아니었지만, 이런식으로 객체를 사용해서 if문이나 switch문 없이 구현하는 것을 의도했었어요 ㅎㅎ
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.
헛 그랬군요..!! redux-actions를 활용하지 않으면 객체형태로 하는게 어렵지 않을까 생각했었는데 아니였군요!
과제 2에서는 redux-actions를 쓰지 않고 구현해보겠습니다 :)