리억스의 대부분 코드는 함수고 pure하기 때문에, mocking 없이 테스트하기 쉽다.
node환경(DOM에 접근할 필요가 없다)에서 실행되는 jest를 사용하자.
리듀서는 이전 상태에 action을 적용한 후에 새로운 상태를 반환해야한다.
todo 추가하는 액션이 있다. 이 액션은 텍스트를 payload로 가진다.
import { ADD_TODO } from '../constants/ActionTypes';
const initialState = [
{
text: 'Use Redux',
completed: false,
id: 0,
},
];
export default function todos(state = initialState, action) {
switch (action.type) {
case ADD_TODO:
return [
{
id: state.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) + 1,
completed: false,
text: action.text,
},
...state,
];
default:
return state;
}
}
import reducer from '../../structuring-reducers/todos';
import * as types from '../../constants/ActionTypes';
describe('todos reducer', () => {
it('should handle ADD_TODO', () => {
expect(
reducer([], {
type: types.ADD_TODO,
text: 'Run the tests',
})
).toEqual([
{
text: 'Run the tests',
completed: false,
id: 0,
},
]);
expect(
reducer(
[
{
text: 'Use Redux',
completed: false,
id: 0,
},
],
{
type: types.ADD_TODO,
text: 'Run the tests',
}
)
).toEqual([
{
text: 'Run the tests',
completed: false,
id: 1,
},
{
text: 'Use Redux',
completed: false,
id: 0,
},
]);
});
});
toEqual은 jest에서 제공해주는 함수로써, deepEqual 비교를 한다.