[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!");
原文地址:https://www.cnblogs.com/Answer1215/p/5011822.html