Skip to content

Instantly share code, notes, and snippets.

@oroce

oroce/package.json

Created Sep 4, 2018
Embed
What would you like to do?
reducer test
{
"scripts": "mocha -r should '*.spec.js'"
}
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;
}
};
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