Created
September 4, 2018 11:29
-
-
Save oroce/52c2ac791b79568785283d98f1409db9 to your computer and use it in GitHub Desktop.
reducer test
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
{ | |
"scripts": "mocha -r should '*.spec.js'" | |
} |
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
export default (action, state = { sum: 0 }) => { | |
switch (action.type) { | |
case 'add': { | |
return { | |
...state, | |
sum: state.sum + action.payload.num | |
}; | |
}; | |
case 'subtract': { | |
return { | |
...state, | |
sum: state.sum - action.payload.num | |
}; | |
}; | |
default: { | |
return state; | |
} | |
}; |
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
import reducer from './reducer'; | |
describe('reducer', () => { | |
it('should add the number to the sum', () => { | |
reducer({ | |
type: 'add', | |
payload: { num: 2 } | |
}, { sum: 4 }).should.eql({ | |
sum: 6 | |
}); | |
}); | |
it('should not add fail with no state', () => { | |
reducer({ | |
type: 'add', | |
payload: { num: 2 } | |
}).should.eql({ | |
sum: 2 | |
}); | |
}); | |
it('should subtract the number', () => { | |
reducer({ | |
type: 'subtract', | |
payload: { num: 2 } | |
}, { sum: 4 }).should.eql({ | |
sum: 2 | |
}); | |
}); | |
it('should not subtract fail with no state', () => { | |
reducer({ | |
type: 'subtract', | |
payload: { num: 2 } | |
}).should.eql({ | |
sum: -2 | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment