Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

pfchangs / redux-actions   js

Repository URL to install this package:

Version: 0.9.0-alpha 

/ lib / __tests__ / handleAction-test.js

'use strict';

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _ = require('../');

describe('handleAction()', function () {
  var type = 'TYPE';
  var prevState = { counter: 3 };

  describe('single handler form', function () {
    describe('resulting reducer', function () {
      it('returns previous state if type does not match', function () {
        var reducer = _.handleAction('NOTTYPE', function () {
          return null;
        });
        expect(reducer(prevState, { type: type })).to.equal(prevState);
      });

      it('accepts single function as handler', function () {
        var reducer = _.handleAction(type, function (state, action) {
          return _extends({}, state, {
            counter: state.counter + action.payload
          });
        });
        expect(reducer(prevState, { type: type, payload: 7 })).to.eql({
          counter: 10
        });
      });
    });
  });

  describe('map of handlers form', function () {
    describe('resulting reducer', function () {
      it('returns previous state if type does not match', function () {
        var reducer = _.handleAction('NOTTYPE', { next: function next() {
            return null;
          } });
        expect(reducer(prevState, { type: type })).to.equal(prevState);
      });

      it('uses `start()` if action signals start of action sequence', function () {
        var reducer = _.handleAction(type, {
          start: function start(state, action) {
            return _extends({}, state, {
              pending: [].concat(state.pending, [action.sequence.id])
            });
          }
        });
        var initialState = { counter: 3, pending: [] };
        var action = { type: type, sequence: { type: 'start', id: 123 } };
        expect(reducer(initialState, action)).to.eql({
          counter: 3,
          pending: [123]
        });
      });

      it('uses `next()` if action does not represent an error', function () {
        var reducer = _.handleAction(type, {
          next: function next(state, action) {
            return _extends({}, state, {
              counter: state.counter + action.payload
            });
          }
        });
        expect(reducer(prevState, { type: type, payload: 7 })).to.eql({
          counter: 10
        });
      });

      it('uses `throw()` if action represents an error', function () {
        var reducer = _.handleAction(type, {
          'throw': function _throw(state, action) {
            return _extends({}, state, {
              counter: state.counter + action.payload
            });
          }
        });
        expect(reducer(prevState, { type: type, payload: 7, error: true })).to.eql({
          counter: 10
        });
      });

      it('uses `return()` if action signals end of action sequence', function () {
        var reducer = _.handleAction(type, {
          'return': function _return(state, action) {
            return _extends({}, state, {
              pending: state.pending.filter(function (id) {
                return id !== action.sequence.id;
              })
            });
          }
        });
        var initialState = { counter: 3, pending: [123, 456, 789] };
        var action = { type: type, sequence: { type: 'return', id: 123 } };
        expect(reducer(initialState, action)).to.eql({
          counter: 3,
          pending: [456, 789]
        });
      });

      it('returns previous state if matching handler is not function', function () {
        var reducer = _.handleAction(type, { next: null, error: 123 });
        expect(reducer(prevState, { type: type, payload: 123 })).to.equal(prevState);
        expect(reducer(prevState, { type: type, payload: 123, error: true })).to.equal(prevState);
      });
    });
  });
});