Skip to content

Instantly share code, notes, and snippets.

@z2015
Created May 2, 2016 02:41
Show Gist options
  • Select an option

  • Save z2015/8f99cbbf8e5996c6e49431696e4879c6 to your computer and use it in GitHub Desktop.

Select an option

Save z2015/8f99cbbf8e5996c6e49431696e4879c6 to your computer and use it in GitHub Desktop.
Redux: Writing a Counter Reducer with Tests
const counter = (state = 0, action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
}
expect(
counter(0, {type: 'INCREMENT'})
).toEqual(1);
expect(
counter(1, {type: 'INCREMENT'})
).toEqual(2);
expect(
counter(2, {type: 'DECREMENT'})
).toEqual(1);
expect(
counter(1, {type: 'DECREMENT'})
).toEqual(0);
expect(
counter(1, {type: 'DECREMENT Else'})
).toEqual(1);
expect(
counter(undefined, {})
).toEqual(0);
console.log('Tests passed!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment