码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-12-02 06:31:46      阅读:144      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!