# Code coverage report for lib/__tests__/Actions-test.js

## Statements: 95.37% (103 / 108) Branches: 70% (21 / 30) Functions: 92% (23 / 25) Lines: 97.85% (91 / 93) Ignored: none

All files » lib/__tests__/ » Actions-test.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200

    1

    1

    1

    8

    1

    1
    1
    1

    1
    1
    1
    8

    8
    8

    1

    1

    4

    1

    4
    8

    4

    4

    1

    1
    1
    1

    1

    1
    1

    1
    1

    1
    1
    1

    1
    1

    1
    1

    1
    1
    1

    1

    1

    1

    1

    1

    1
    1
    1
    1
    1

    1

    1
    1

    1
    1
    1
    2

    1
    1
    1
    1

    1

    1

    1
    1

    1
    1

    1

    1
    1
    1
    1

    1

    1

    1
    1
    1
    1

    1

    1

    1
    1
    1
    2

    1
    1

    1
    1
    1

    1

     "use strict";

    var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };

    var _prototypeProperties = function (child, staticProps, instanceProps) { Iif (staticProps) Object.defineProperties(child, staticProps); Eif (instanceProps) Object.defineProperties(child.prototype, instanceProps); };

    var _inherits = function (subClass, superClass) { Iif (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); Eif (superClass) subClass.__proto__ = superClass; };

    var _classCallCheck = function (instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };

    var _Flux = require("../Flux");

    var Flux = _Flux.Flux;
    var Actions = _Flux.Actions;
    var sinon = _interopRequire(require("sinon"));

    describe("Actions", function () {
      var TestActions = (function (Actions) {
        function TestActions() {
          _classCallCheck(this, TestActions);

          Eif (Actions != null) {
            Actions.apply(this, arguments);
          }
        }

        _inherits(TestActions, Actions);

        _prototypeProperties(TestActions, null, {
          getFoo: {
            value: function getFoo() {
              return { foo: "bar" };
            },
            writable: true,
            configurable: true
          },
          getBar: {
            value: function getBar() {
              return { bar: "baz" };
            },
            writable: true,
            configurable: true
          },
          getBaz: {
            value: function getBaz() {
              return;
            },
            writable: true,
            configurable: true
          },
          asyncAction: {
            value: function asyncAction(returnValue) {
              return regeneratorRuntime.async(function asyncAction$(context$3$0) {
                while (1) switch (context$3$0.prev = context$3$0.next) {
                  case 0:
                    return context$3$0.abrupt("return", returnValue);
                  case 1:
                  case "end":
                    return context$3$0.stop();
                }
              }, null, this);
            },
            writable: true,
            configurable: true
          },
          badAsyncAction: {
            value: function badAsyncAction() {
              return Promise.reject(new Error("some error"));
            },
            writable: true,
            configurable: true
          }
        });

        return TestActions;
      })(Actions);

      describe("#getActionIds / #getConstants", function () {
        it("returns strings corresponding to action method names", function () {
          var actions = new TestActions();

          var actionIds = actions.getActionIds();

          expect(actionIds.getFoo).to.be.a("string");
          expect(actionIds.getBar).to.be.a("string");

          expect(actionIds.getFoo).to.be.a("string");
          expect(actionIds.getBar).to.be.a("string");
        });
      });

      describe("#[methodName]", function () {
        it("calls Flux dispatcher", function () {
          var actions = new TestActions();

          // Attach mock flux instance
          var dispatch = sinon.spy();
          actions.dispatch = dispatch;

          actions.getFoo();
          expect(dispatch.firstCall.args[1]).to.deep.equal({ foo: "bar" });
        });

        it("warns if actions have not been added to a Flux instance", function () {
          var actions = new TestActions();
          var warn = sinon.spy(console, "warn");

          actions.getFoo();

          expect(warn.firstCall.args[0]).to.equal("You've attempted to perform the action TestActions#getFoo, but it " + "hasn't been added to a Flux instance.");

          actions.asyncAction();

          expect(warn.secondCall.args[0]).to.equal("You've attempted to perform the asynchronous action " + "TestActions#asyncAction, but it hasn't been added " + "to a Flux instance.");

          console.warn.restore();
        });

        it("sends return value to Flux dispatch", function () {
          var actions = new TestActions();
          var actionId = actions.getActionIds().getFoo;
          var dispatch = sinon.spy();
          actions.dispatch = dispatch;

          actions.getFoo();

          expect(dispatch.firstCall.args[0]).to.equal(actionId);
          expect(dispatch.firstCall.args[1]).to.deep.equal({ foo: "bar" });
        });

        it("send async return value to Flux#dispatchAsync", function callee$2$0() {
          var actions, actionId, dispatch, response;
          return regeneratorRuntime.async(function callee$2$0$(context$3$0) {
            while (1) switch (context$3$0.prev = context$3$0.next) {
              case 0:
                actions = new TestActions();
                actionId = actions.getActionIds().asyncAction;
                dispatch = sinon.stub().returns(Promise.resolve());
                actions.dispatchAsync = dispatch;

                response = actions.asyncAction("foobar");

                expect(response.then).to.be.a("function");

                context$3$0.next = 8;
                return response;
              case 8:

                expect(dispatch.firstCall.args[0]).to.equal(actionId);
                expect(dispatch.firstCall.args[1]).to.be.an.instanceOf(Promise);
              case 10:
              case "end":
                return context$3$0.stop();
            }
          }, null, this);
        });

        it("skips disptach if return value is undefined", function () {
          var actions = new TestActions();
          var dispatch = sinon.spy();
          actions.dispatch = dispatch;

          actions.getBaz();

          expect(dispatch.called).to.be["false"];
        });

        it("does not skip async dispatch, even if resolved value is undefined", function () {
          var actions = new TestActions();
          var dispatch = sinon.spy();
          actions.dispatchAsync = dispatch;

          actions.asyncAction(undefined);

          expect(dispatch.called).to.be["true"];
        });

        it("returns value from wrapped action", function callee$2$1() {
          var flux, actions;
          return regeneratorRuntime.async(function callee$2$1$(context$3$0) {
            while (1) switch (context$3$0.prev = context$3$0.next) {
              case 0:
                flux = new Flux();
                actions = flux.createActions("test", TestActions);

                expect(actions.getFoo()).to.deep.equal({ foo: "bar" });
                context$3$0.next = 5;
                return expect(actions.asyncAction("async result")).to.eventually.equal("async result");
              case 5:
              case "end":
                return context$3$0.stop();
            }
          }, null, this);
        });
      });
    });
    //# sourceMappingURL=data:application/json;base64,