[Redux] Writing a Todo List Reducer (Adding a Todo)

Learn how to implement adding a todo in a todo list application reducer.

 

let todo = (state = [], action) => {
  
  switch(action.type){
    case 'ADD_ITEM':
      return state = [
        ...state,
        {
          text: action.text,
          id: action.id,
          completed: false
        }
      ];
    default:
      return state;
  }
};

let testTodo = () => {
  let stateBefore = [];
  let action = {
    type: 'ADD_ITEM',
    text: 'Learn Redux',
    id: 0
  };
  let stateAfter = [
    {
      text: 'Learn Redux',
      id: 0,
      completed: false,
    }
  ];
  
  deepFreeze(stateBefore);
  deepFreeze(action);
  
  expect(
    todo(stateBefore, action)
  ).toEqual(stateAfter);
};

testTodo();

console.log("All tests passed!");

 

posted @ 2015-12-02 01:53  Zhentiw  阅读(160)  评论(0编辑  收藏  举报