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

## Statements: 94.98% (208 / 219) Branches: 53.33% (16 / 30) Functions: 100% (52 / 52) Lines: 100% (195 / 195) Ignored: none

All files » lib/addons/__tests__/ » fluxMixin-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
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476

    3

    48

    2

    3

    64

    1

    1

    1
    1
    1
    1

    1

    1
    1

    1
    1
    1
    16

    16
    16

    1

    1

    9

    1

    1
    1
    32

    32

    32
    32

    32

    1

    1

    18

    1

    1
    1
    16

    16

    16
    16
    16

    1

    1

    1

    22

    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

    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

    2

    2

    2

    2

    1

    1
    1

    1
    1

    1
    1

    1

    2

    3

    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

    1

    1

    1

    1
    1
    1

    1

    1
    1

    1
    1
    1

    1
    1
    1

    1

    1

    2

    2

    1

    1

    1
    1

    1

    1

     "use strict";

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

    var _get = function get(object, property, receiver) { var desc = Object.getOwnPropertyDescriptor(object, property); Iif (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else Eif ("value" in desc && desc.writable) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };

    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 fluxMixin = _interopRequire(require("../fluxMixin"));

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

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

    var React = _interopRequire(require("react/addons"));

    var PropTypes = React.PropTypes;
    var TestUtils = React.addons.TestUtils;

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

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

        _inherits(TestActions, Actions);

        _prototypeProperties(TestActions, null, {
          getSomething: {
            value: function getSomething(something) {
              return something;
            },
            writable: true,
            configurable: true
          }
        });

        return TestActions;
      })(Actions);

      var TestStore = (function (Store) {
        function TestStore(flux) {
          _classCallCheck(this, TestStore);

          _get(Object.getPrototypeOf(TestStore.prototype), "constructor", this).call(this);

          var testActions = flux.getActions("test");
          this.register(testActions.getSomething, this.handleGetSomething);

          this.state = {
            something: null
          };
        }

        _inherits(TestStore, Store);

        _prototypeProperties(TestStore, null, {
          handleGetSomething: {
            value: function handleGetSomething(something) {
              this.setState({ something: something });
            },
            writable: true,
            configurable: true
          }
        });

        return TestStore;
      })(Store);

      var Flux = (function (Flummox) {
        function Flux() {
          _classCallCheck(this, Flux);

          _get(Object.getPrototypeOf(Flux.prototype), "constructor", this).call(this);

          this.createActions("test", TestActions);
          this.createStore("test", TestStore, this);
          this.createStore("test2", TestStore, this);
        }

        _inherits(Flux, Flummox);

        return Flux;
      })(Flummox);

      var ComponentWithFluxMixin = React.createClass({
        displayName: "ComponentWithFluxMixin",
        mixins: [fluxMixin()],

        render: function render() {
          return null;
        }
      });

      it("gets flux from either props or context", function () {
        var flux = new Flux();
        var contextComponent = undefined,
            propsComponent = undefined;

        var ContextComponent = addContext(ComponentWithFluxMixin, { flux: flux }, { flux: React.PropTypes.instanceOf(Flummox) });

        var tree = TestUtils.renderIntoDocument(React.createElement(ContextComponent, { keys: "test" }));

        contextComponent = TestUtils.findRenderedComponentWithType(tree, ComponentWithFluxMixin);

        propsComponent = TestUtils.renderIntoDocument(React.createElement(ComponentWithFluxMixin, { key: "test", flux: flux }));

        expect(contextComponent.flux).to.be.an["instanceof"](Flummox);
        expect(propsComponent.flux).to.be.an["instanceof"](Flummox);
      });

      it("exposes flux as context", function () {
        var flux = new Flux();

        var ChildComponent = React.createClass({
          displayName: "ChildComponent",
          contextTypes: {
            flux: PropTypes.instanceOf(Flummox) },

          render: function render() {
            return React.createElement("div", null);
          }
        });

        var Component = React.createClass({
          displayName: "Component",
          mixins: [fluxMixin()],

          render: function render() {
            return React.createElement(
              "div",
              null,
              React.createElement(ChildComponent, { key: "test" })
            );
          }
        });

        var tree = TestUtils.renderIntoDocument(React.createElement(Component, { flux: flux }));

        var childComponent = TestUtils.findRenderedComponentWithType(tree, ChildComponent);

        expect(childComponent.context.flux).to.equal(flux);
      });

      it("throws error if neither props or context is set", function () {
        var flux = new Flux();

        expect(TestUtils.renderIntoDocument.bind(null, React.createElement(ComponentWithFluxMixin, null))).to["throw"]("fluxMixin: Could not find Flux instance. Ensure that your component " + "has either `this.context.flux` or `this.props.flux`.");
      });

      it("ignores change event after unmounted", function () {
        var flux = new Flux();
        flux.getActions("test").getSomething("foo");

        var getterMap = {
          test: function (store) {
            return { something: store.state.something };
          }
        };
        var Component = React.createClass({
          displayName: "Component",
          mixins: [fluxMixin(getterMap)],

          render: function render() {
            return null;
          }
        });

        var container = document.createElement("div");
        var component = React.render(React.createElement(Component, { flux: flux }), container);
        var listener = flux.getStore("test").listeners("change")[0];

        React.unmountComponentAtNode(container);

        flux.getActions("test").getSomething("bar");
        listener();

        expect(component.state.something).to.equal("foo");
      });

      it("uses #connectToStores() to get initial state", function () {
        var flux = new Flux();

        flux.getActions("test").getSomething("foobar");

        var getterMap = {
          test: function (store) {
            return {
              something: store.state.something,
              custom: true };
          } };

        var mixin = fluxMixin(getterMap);

        var connectToStores = sinon.spy(mixin, "connectToStores");

        var Component = React.createClass({
          displayName: "Component",
          mixins: [mixin],

          getInitialState: function getInitialState() {
            return {
              foobar: "baz" };
          },

          render: function render() {
            return null;
          }
        });

        var component = TestUtils.renderIntoDocument(React.createElement(Component, { key: "test", flux: flux }));

        expect(connectToStores.calledOnce).to.be["true"];
        expect(connectToStores.firstCall.args[0]).to.equal(getterMap);

        expect(flux.getStore("test").listeners("change")).to.have.length(1);

        expect(component.state).to.deep.equal({
          something: "foobar",
          custom: true,
          foobar: "baz" });
      });

      describe("#connectToStores", function () {
        it("returns initial state", function () {
          var flux = new Flux();

          var component = TestUtils.renderIntoDocument(React.createElement(ComponentWithFluxMixin, { key: "test", flux: flux }));

          var initialState = component.connectToStores("test");

          expect(initialState).to.deep.equal({
            something: null });
        });

        it("merges store state with component state on change", function () {
          var flux = new Flux();

          var component = TestUtils.renderIntoDocument(React.createElement(ComponentWithFluxMixin, { key: "test", flux: flux }));

          component.setState({ otherThing: "barbaz" });

          component.connectToStores("test");
          flux.getActions("test").getSomething("foobar");

          expect(component.state).to.deep.equal({
            something: "foobar",
            otherThing: "barbaz" });
        });

        it("uses custom state getter, if given", function () {
          var flux = new Flux();

          var component = TestUtils.renderIntoDocument(React.createElement(ComponentWithFluxMixin, { key: "test", flux: flux }));

          component.setState({ otherThing: "barbaz" });

          component.connectToStores("test", function (store) {
            return {
              something: store.state.something,
              custom: true };
          });

          flux.getActions("test").getSomething("foobar");

          expect(component.state).to.deep.equal({
            something: "foobar",
            otherThing: "barbaz",
            custom: true });
        });

        it("binds state getter to component", function () {
          var flux = new Flux();

          var Component = React.createClass({
            displayName: "Component",
            mixins: [fluxMixin({
              test: function (store) {
                this.someComponentMethod("some arg");

                return {
                  something: store.state.something,
                  custom: true };
              } })],

            render: function render() {
              return null;
            },

            someComponentMethod: function someComponentMethod(string) {
              return string;
            }
          });

          var component = TestUtils.renderIntoDocument(React.createElement(Component, { key: "test", flux: flux }));

          var someComponentMethod = sinon.spy(component, "someComponentMethod");
          flux.getActions("test").getSomething("foobar");

          expect(someComponentMethod.calledOnce).to.be["true"];
          expect(someComponentMethod.firstCall.args[0]).to.equal("some arg");
        });

        it("syncs with store after prop change", function () {
          var flux = new Flux();

          var Component = React.createClass({
            displayName: "Component",
            mixins: [fluxMixin({
              test: function (store) {
                return {
                  foo: "foo is " + this.props.foo };
              } })],

            render: function render() {
              return null;
            }
          });

          var component = TestUtils.renderIntoDocument(React.createElement(Component, { key: "test", flux: flux, foo: "bar" }));

          expect(component.state.foo).to.equal("foo is bar");

          component.setProps({ foo: "baz" });

          expect(component.state.foo).to.equal("foo is baz");
        });

        it("accepts object of keys to state getters", function () {
          var flux = new Flux();

          var component = TestUtils.renderIntoDocument(React.createElement(ComponentWithFluxMixin, { key: "test", flux: flux }));

          component.setState({ otherThing: "barbaz" });

          component.connectToStores({
            test: function (store) {
              return {
                something: store.state.something,
                custom: true };
            } });

          flux.getActions("test").getSomething("foobar");

          expect(component.state).to.deep.equal({
            something: "foobar",
            otherThing: "barbaz",
            custom: true });
        });

        it("calls default state getter once with array of stores", function () {
          var flux = new Flux();

          flux.getStore("test2").setState({ otherThing: "barbaz" });

          var component = TestUtils.renderIntoDocument(React.createElement(ComponentWithFluxMixin, { key: "test", flux: flux }));

          component.connectToStores(["test", "test2"]);

          flux.getActions("test").getSomething("foobar");

          expect(component.state).to.deep.equal({
            something: "foobar",
            otherThing: "barbaz"
          });
        });

        it("calls custom state getter once with array of stores", function () {
          var flux = new Flux();
          var testStore = flux.getStore("test");
          var test2Store = flux.getStore("test2");

          testStore._testId = "test";
          test2Store._testId = "test2";

          var component = TestUtils.renderIntoDocument(React.createElement(ComponentWithFluxMixin, { key: "test", flux: flux }));

          var stateGetter = sinon.stub().returns({ foo: "bar" });
          var state = component.connectToStores(["test", "test2"], stateGetter);

          expect(stateGetter.calledOnce).to.be["true"];
          // Use _testId as unique identifier on store.
          expect(stateGetter.firstCall.args[0][0]._testId).to.equal("test");
          expect(stateGetter.firstCall.args[0][1]._testId).to.equal("test2");

          expect(state).to.deep.equal({
            foo: "bar"
          });
        });

        it("uses default getter if null is passed as getter", function () {
          var flux = new Flux();

          var component = TestUtils.renderIntoDocument(React.createElement(ComponentWithFluxMixin, { key: "test", flux: flux }));

          component.setState({ otherThing: "barbaz" });

          component.connectToStores("test", null);

          flux.getActions("test").getSomething("foobar");

          expect(component.state).to.deep.equal({
            something: "foobar",
            otherThing: "barbaz" });
        });

        it("removes listener before unmounting", function () {
          var flux = new Flux();
          var div = document.createElement("div");

          var component = React.render(React.createElement(ComponentWithFluxMixin, { flux: flux }), div);

          var store = flux.getStore("test");
          component.connectToStores("test");

          expect(store.listeners("change").length).to.equal(1);
          React.unmountComponentAtNode(div);
          expect(store.listeners("change").length).to.equal(0);
        });
      });

      describe("#getStoreState", function () {
        it("gets combined state of connected stores", function () {
          var flux = new Flux();

          var component = TestUtils.renderIntoDocument(React.createElement(ComponentWithFluxMixin, { key: "test", flux: flux }));

          component.connectToStores({
            test: function (store) {
              return {
                foo: "bar" };
            },
            test2: function (store) {
              return {
                bar: "baz"
              };
            }
          });

          component.setState({ baz: "foo" });

          expect(component.getStoreState()).to.deep.equal({
            foo: "bar",
            bar: "baz"
          });
        });
      });
    });

    function addContext(Component, context, contextTypes) {
      return React.createClass({
        childContextTypes: contextTypes,

        getChildContext: function getChildContext() {
          return context;
        },

        render: function render() {
          return React.createElement(Component, this.props);
        }
      });
    }

    //# sourceMappingURL=data:application/json;base64,