Skip to content

Instantly share code, notes, and snippets.

@kishanio
Created May 2, 2017 08:34
Show Gist options
  • Save kishanio/2d3790d1fe7d2d49674ce496f07a4bf0 to your computer and use it in GitHub Desktop.
Save kishanio/2d3790d1fe7d2d49674ce496f07a4bf0 to your computer and use it in GitHub Desktop.
Redux : Reducer Function Example
// Reducer for counter.
// Dependency : https://github.com/mjackson/expect
// handles default state
const counter = ( state = 0, action) => {
switch( action.type ) {
case 'INCREMENT':
return state+1;
case 'DECREMENT':
return state-1;
// handles undefined action
default:
return state;
}
}
expect(
counter(0, { type: 'INCREMENT' })
).toEqual(1);
expect(
counter(1, { type: 'INCREMENT' })
).toEqual(2);
expect(
counter(1,{ type: 'SOMETHING_ELSE' })
).toEqual(1);
expect(
counter(undefined, {})
).toEqual(0);
console.log("Test Passed");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment