首页 > 其他 > 详细

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

时间:2015-12-02 06:31:25      阅读:289      评论:0      收藏:0      [点我收藏+]

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!");

 

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

原文:http://www.cnblogs.com/Answer1215/p/5011822.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!