Created
February 10, 2016 18:35
-
-
Save rowlando/171c670e6901dfd1f425 to your computer and use it in GitHub Desktop.
Tiny Redux app with tests for count reducer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A counter reducer | |
const counter = ( state = 0, action) => { | |
switch(action.type) { | |
case 'INCREMENT': | |
return state + 1; | |
case 'DECREMENT': | |
return state - 1; | |
default: | |
return state; | |
} | |
} | |
// Tests for reducer | |
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: 'SOMETHING_ELSE'}) | |
).toEqual(1); | |
console.log('Test passed!'); | |
// Tiny Redux App | |
const { createStore } = Redux; | |
const store = createStore(counter); | |
const render = () => { | |
document.body.innerText = store.getState(); | |
} | |
store.subscribe(render); | |
render(); | |
document.addEventListener('click', () => { | |
store.dispatch({type: 'INCREMENT'}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment