Skip to content
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
wants to merge 4 commits into
base: ctaaag
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions __mocks__/react-redux.js
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({}));
14,177 changes: 6,836 additions & 7,341 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
},
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
"react-dom": "^17.0.2",
"react-redux": "^8.0.5",
"redux": "^4.2.1"
}
}
49 changes: 8 additions & 41 deletions src/App.jsx
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 />
</>

);
}
14 changes: 14 additions & 0 deletions src/App.test.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import { render } from '@testing-library/react';
import { useDispatch, useSelector } from 'react-redux';

import App from './App';

jest.mock('react-redux');

test('App', () => {
const dispatch = jest.fn();

useDispatch.mockImplementation(() => dispatch);
useSelector.mockImplementation((selector) => selector({
taskTitle: '',
tasks: [
{ id: 1, title: '아무것도 하지 않기#1' },
{ id: 2, title: '아무것도 하지 않기#2' },
],
}));

const { getByText } = render((
<App />
));
Expand Down
26 changes: 26 additions & 0 deletions src/InputContainer.jsx
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 './actions';

export default function InputContainer() {
const { taskTitle } = useSelector((state) => ({
taskTitle: state.taskTitle,
}));
const dispatch = useDispatch();
// 관심사분리
function handleChangeTitle(event) {
dispatch(updateTaskTitle(event.target.value));
}

function handleClickAddTask() {
dispatch((addTask()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기 괄호가 하나 더 들어갔군요

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗..!! 수정하겠습니다

}

return (
<Input
value={taskTitle}
onChange={handleChangeTitle}
onClick={handleClickAddTask}
/>
);
}
28 changes: 28 additions & 0 deletions src/InputContainer.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { render, fireEvent, screen } 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',
});
});
22 changes: 22 additions & 0 deletions src/ListContainer.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useDispatch, useSelector } from 'react-redux';
import { deleteTask } from './actions';
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}
/>
);
}
26 changes: 26 additions & 0 deletions src/ListContainer.test.jsx
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 => 실제 브라우저에서 사용자 테스트 실행 가능.
});
22 changes: 0 additions & 22 deletions src/Page.jsx

This file was deleted.

31 changes: 0 additions & 31 deletions src/Page.test.jsx

This file was deleted.

19 changes: 19 additions & 0 deletions src/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function updateTaskTitle(taskTitle) {
return {
type: 'updateTaskTitle',
payload: { taskTitle },
};
}

export function addTask() {
return {
type: 'addTask',
};
}

export function deleteTask(id) {
return {
type: 'deleteTask',
payload: { id },
};
}
9 changes: 8 additions & 1 deletion src/index.jsx
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'),
);
43 changes: 43 additions & 0 deletions src/reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Redux action
// - type(string)
// - payload(object)

const initialState = {
newId: 100,
taskTitle: '',
tasks: [],
};

export default function reducer(state = initialState, action) {
if (action.type === 'updateTaskTitle') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if문이나 스위치문 없이 코드를 작성할 수 있을까요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고민해서 반영해보겠습니다~!

return {
...state,
taskTitle: action.payload.taskTitle,
};
}

if (action.type === 'addTask') {
const { newId, taskTitle, tasks } = state;

if (taskTitle === '') {
return state;
}
return {
...state,
newId: newId + 1,
taskTitle: '',
tasks: [...tasks, { id: newId, title: taskTitle }],
};
}

if (action.type === 'deleteTask') {
const { tasks } = state;
return {
...state,
tasks: tasks.filter((task) => task.id !== action.payload.id),

};
}

return state;
}
45 changes: 45 additions & 0 deletions src/reducer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import reducer from './reducer';
import { updateTaskTitle, addTask, deleteTask } from './actions';

// 테스트하고자 하는 것의 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('task가 없어진다', () => {
const prevState = {
newId: 101,
taskTitle: '',
tasks: [{ id: 100, taskTitle: 'something' }],
};
const state = reducer(prevState, deleteTask(100));
expect(state.tasks).toHaveLength(0);
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reducer는 이전 상태와 액션을 받고 새로운 상태를 반환하는 함수입니다. 그러면 없어진다라고 표현하는 것보다는 다르게 표현할 수 있을거예요

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

호오.. 고민해보고 커밋보내겠습니당!

});
7 changes: 7 additions & 0 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createStore } from 'redux';

import reducer from './reducer';

const store = createStore(reducer);

export default store;