Skip to content

Instantly share code, notes, and snippets.

@oroce
Created September 4, 2018 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oroce/52c2ac791b79568785283d98f1409db9 to your computer and use it in GitHub Desktop.
Save oroce/52c2ac791b79568785283d98f1409db9 to your computer and use it in GitHub Desktop.
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