实现简单计数功能的Reducer
- 测试工具使用的是 npm expect
- 测试下面结构
function counter(state, action) {
return state;
}
expect(
counter(0, { type: '增加' })
).toEqual(1);
expect(
counter(1, { type: '增加' })
).toEqual(2);
expect(
counter(2, { type: '减少' })
).toEqual(1);
expect(
counter(1, { type: '减少' })
).toEqual(0);
console.log('Test passed!');
// 最终代码: http://jsbin.com/ponujoc/edit?html,js,console
学习内容
- 实现增加动作
- 实现减少动作
- 实现其他动作
- 处理undefined情况
- 简化if,默认赋值
- 使用ES6箭头函数