Last active
December 24, 2015 20:53
-
-
Save goldhand/1ee39eb201ff13095d50 to your computer and use it in GitHub Desktop.
Simple redux test pattern
This file contains hidden or 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
/* | |
* ReducerTestSkeleton | |
* Author: Will Farley <@goldhand> | |
* Skeleton for writing simple reducer tests. | |
* takes a reducer and expects it to return stateAfter when invoked with stateBefore and action. | |
* | |
*/ | |
// Example: = | |
// const dummyTest = () => reducerTestSkeleton( | |
// {counter: 1}, | |
// {counter: 2}, | |
// {type: 'INCREASE'}, | |
// dummyReducer | |
// ); | |
// describe('Dummy Reducer Tests', function() { | |
// it('1 + 1 = 2', function() { | |
// dummyTest(); | |
// }); | |
// }; | |
import expect from 'expect'; | |
import deepFreeze from 'deep-freeze'; | |
const reducerTestSkeleton = ( | |
stateBefore, | |
stateAfter, | |
action, | |
reducer, | |
) => { | |
deepFreeze(stateAfter); | |
deepFreeze(action); | |
expect(reducer(stateBefore, action) | |
).toEqual(stateAfter); | |
}; | |
export default reducerTestSkeleton; | |
This file contains hidden or 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
import expect from 'expect'; | |
import deepFreeze from 'deep-freeze'; | |
const dummyReducer = (state, action) => { | |
switch (action.type) { | |
case 'INCREASE': | |
return { | |
counter: state.counter + 1, | |
}; | |
case 'DECREASE': | |
return { | |
counter: state.counter - 1, | |
}; | |
default: | |
return state; | |
} | |
}; | |
const dummyTestIncrease = (successMsg) => { | |
const stateBefore = { | |
counter: 1, | |
}; | |
const stateAfter = { | |
counter: 2, | |
}; | |
const action = { | |
type: 'INCREASE', | |
}; | |
deepFreeze(stateBefore); | |
deepFreeze(action); | |
expect( | |
dummyReducer(stateBefore, action) | |
).toEqual(stateAfter); | |
console.info(successMsg); | |
}; | |
const dummyTestDecrease = (successMsg) => { | |
const stateBefore = { | |
counter: 2, | |
}; | |
const stateAfter = { | |
counter: 1, | |
}; | |
const action = { | |
type: 'DECREASE', | |
}; | |
deepFreeze(stateBefore); | |
deepFreeze(action); | |
expect( | |
dummyReducer(stateBefore, action) | |
).toEqual(stateAfter); | |
console.info(successMsg); | |
}; | |
const runTests = (successMsg) => { | |
dummyTestIncrease('increase test passed'); | |
dummyTestDecrease('decrease test passed'); | |
console.info(successMsg); | |
}; | |
runTests('all tests passed'); |
This file contains hidden or 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
/* | |
* Same tests as before this time using the mocha testing suite | |
*/ | |
import expect from 'expect'; | |
import deepFreeze from 'deep-freeze'; | |
const dummyReducer = (state, action) => { | |
switch (action.type) { | |
case 'INCREASE': | |
return { | |
counter: state.counter + 1, | |
}; | |
case 'DECREASE': | |
return { | |
counter: state.counter - 1, | |
}; | |
default: | |
return state; | |
} | |
}; | |
const dummyTestIncrease = () => { | |
const stateBefore = { | |
counter: 1, | |
}; | |
const stateAfter = { | |
counter: 2, | |
}; | |
const action = { | |
type: 'INCREASE', | |
}; | |
deepFreeze(stateBefore); | |
deepFreeze(action); | |
expect( | |
dummyReducer(stateBefore, action) | |
).toEqual(stateAfter); | |
}; | |
const dummyTestDecrease = () => { | |
const stateBefore = { | |
counter: 2, | |
}; | |
const stateAfter = { | |
counter: 1, | |
}; | |
const action = { | |
type: 'DECREASE', | |
}; | |
deepFreeze(stateBefore); | |
deepFreeze(action); | |
expect( | |
dummyReducer(stateBefore, action) | |
).toEqual(stateAfter); | |
}; | |
// describe and it are global functions from mocha | |
describe('Dummy Reducer Tests', function() { | |
it('1 + 1 = 2', function() { | |
dummyTestIncrease(); | |
}); | |
it('2 - 1 = 1', function() { | |
dummyTestDecrease(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment