[Redux] Writing a Todo List Reducer (Toggling a Todo)
2015-12-02 02:18
459 查看
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!");
相关文章推荐
- HTML学习笔记
- CMAKE 交叉编译
- 一行代码实现headView弹簧拉伸效果
- 第十四周 11.30---12.6
- ULK --- Chap 4: Nested Execution of Exception and Interrupt Handlers
- Filter(过滤器)常见应用
- android学习笔记:Intent,Intent Filter
- codeforces #334A,B
- cocos2d-x札记 (一)----HelloWorld浅析
- Linux (编程)摘要
- [Redux] Writing a Todo List Reducer (Adding a Todo)
- java垃圾回收机制
- linux install jdk
- BZOJ4032 : [HEOI2015]最短不公共子串
- java编程知识--初始化
- NumberBox( 数值输入框) 组件
- 如何实现内核旁路(Kernel bypass)?
- 使用__FILE__和__LINE__定位错误
- 初觉新奇,细思在理的刷三观之作 ——读《人类简史·从动物到上帝》有感
- oracle 查看表空间的脚本