Skip to content

Instantly share code, notes, and snippets.

@oroce
Created September 4, 2018 11:29
Show Gist options
  • Select an option

  • Save oroce/52c2ac791b79568785283d98f1409db9 to your computer and use it in GitHub Desktop.

Select an option

Save oroce/52c2ac791b79568785283d98f1409db9 to your computer and use it in GitHub Desktop.

Revisions

  1. oroce created this gist Sep 4, 2018.
    3 changes: 3 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    {
    "scripts": "mocha -r should '*.spec.js'"
    }
    18 changes: 18 additions & 0 deletions reducer.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    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;
    }
    };
    38 changes: 38 additions & 0 deletions reducer.spec.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    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
    });
    });
    });