zoukankan      html  css  js  c++  java
  • [Redux] Writing a Todo List Reducer (Toggling a Todo)

    Learn how to implement toggling 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
            }
          ];
        case 'TOGGLE_ITEM':
          return state.map( (todo) => {
            if(todo.id !== action.id){
              return todo;
            }else{
             return {
              ...todo,
              completed: !todo.Completed// will overwirte the todo object's completed prop
            }; 
            }
          })
        default:
          return state;
      }
    };
    
    let testTodo_addItem = () => {
      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);
    };
    
    let testTodo_toggleItem = () => {
      let stateBefore = [
        {
          text: 'Learn Redux',
          id: 0,
          completed: false
        },
        {
          text: 'Learn Angular2',
          id: 1,
          completed: false
        }
      ];
      let action = {
        type: 'TOGGLE_ITEM',
        id: 1
      };
      
      let stateAfter = [
        {
          text: 'Learn Redux',
          id: 0,
          completed: false
        },
        {
          text: 'Learn Angular2',
          id: 1,
          completed: true
        }
      ];
      
      deepFreeze(stateBefore);
      deepFreeze(action);
      
      expect(
        todo(stateBefore, action)
      ).toEqual(stateAfter);
    }
    
    testTodo_toggleItem();
    
    console.log("All tests passed!");
  • 相关阅读:
    多线程
    Flume和 Sqoop
    Struts2部分
    hibernate学习笔记(一)
    idea创建普通的java小项目教程
    IntelliJ Idea 常用快捷键列表
    idea创建springMVC框架和配置小文件
    对于Git的总结
    关于jvm运行时时区的总结
    事务的总结笔记(详解很仔细),mysql事务隔离级别演示,,,
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5011828.html
Copyright © 2011-2022 走看看