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 all 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,213 changes: 6,880 additions & 7,333 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@testing-library/jest-dom": "^5.12.0",
"@testing-library/react": "^11.2.7",
"@types/jest": "^26.0.23",
"@types/redux-actions": "^2.6.2",
"babel-jest": "^26.6.3",
"babel-loader": "^8.2.2",
"codeceptjs": "^3.0.7",
Expand All @@ -40,6 +41,9 @@
},
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
"react-dom": "^17.0.2",
"react-redux": "^8.0.5",
"redux": "^4.2.1",
"redux-actions": "^3.0.0"
}
}
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 './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}
/>
);
}
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 } 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 './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}
/>
);
}
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.

32 changes: 32 additions & 0 deletions src/actions.js
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 },
// };
// }
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'),
);
36 changes: 36 additions & 0 deletions src/reducers.js
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({
Copy link
Contributor

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문 없이 구현하는 것을 의도했었어요 ㅎㅎ

Copy link
Author

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를 쓰지 않고 구현해보겠습니다 :)

[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;
44 changes: 44 additions & 0 deletions src/reducers.test.js
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);
});
});
});
Loading