实现简单计数功能的Reducer

  1. 测试工具使用的是 npm expect
  2. 测试下面结构
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

学习内容

  1. 实现增加动作
  2. 实现减少动作
  3. 实现其他动作
  4. 处理undefined情况
  5. 简化if,默认赋值
  6. 使用ES6箭头函数