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

## Statements: 90.91% (110 / 121) Branches: 53.33% (16 / 30) Functions: 100% (23 / 23) Lines: 100% (97 / 97) Ignored: none

All files » lib/addons/__tests__/ » FluxComponent-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

    2

    16

    2

    3

    24

    1

    1
    1
    1
    1

    1
    1

    1
    1
    1
    8

    8
    8

    1

    1

    6

    1

    1
    1
    8

    8

    8
    8

    8

    1

    1

    6

    1

    1
    1
    8

    8

    8
    8

    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

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

    var Flummox = _Flux.Flummox;
    var Store = _Flux.Store;
    var Actions = _Flux.Actions;
    var React = _interopRequire(require("react/addons"));

    var TestUtils = React.addons.TestUtils;
    var FluxComponent = _interopRequire(require("../FluxComponent"));

    describe("FluxComponent", 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);
        }

        _inherits(Flux, Flummox);

        return Flux;
      })(Flummox);

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

        React.withContext({ flux: flux }, function () {
          contextComponent = TestUtils.renderIntoDocument(React.createElement(FluxComponent, null));
        });

        propsComponent = TestUtils.renderIntoDocument(React.createElement(FluxComponent, { flux: flux }));

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

      it("passes connectToStore prop to FluxMixin's connectToStores()", function () {
        var flux = new Flux();
        var actions = flux.getActions("test");

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

        actions.getSomething("something good");
        expect(component.state.something).to.deep.equal("something good");
        actions.getSomething("something else");
        expect(component.state.something).to.deep.equal("something else");
      });

      it("injects children with flux prop", function () {
        var flux = new Flux();
        var actions = flux.getActions("test");

        var tree = TestUtils.renderIntoDocument(React.createElement(
          FluxComponent,
          { flux: flux },
          React.createElement("div", null)
        ));

        var div = TestUtils.findRenderedDOMComponentWithTag(tree, "div");

        expect(div.props.flux).to.equal(flux);
      });

      it("injects children with props corresponding to component state", function () {
        var flux = new Flux();
        var actions = flux.getActions("test");

        var tree = TestUtils.renderIntoDocument(React.createElement(
          FluxComponent,
          { flux: flux, connectToStores: "test" },
          React.createElement("div", null)
        ));

        var div = TestUtils.findRenderedDOMComponentWithTag(tree, "div");

        actions.getSomething("something good");
        expect(div.props.something).to.equal("something good");
        actions.getSomething("something else");
        expect(div.props.something).to.equal("something else");
      });

      it("injects children with any extra props", function () {
        var flux = new Flux();

        var tree = TestUtils.renderIntoDocument(React.createElement(
          FluxComponent,
          { flux: flux, extraProp: "hello" },
          React.createElement("div", null)
        ));

        var div = TestUtils.findRenderedDOMComponentWithTag(tree, "div");

        expect(div.props.extraProp).to.equal("hello");
        expect(Object.keys(div.props)).to.deep.equal(["flux", "extraProp"]);
      });

      it("wraps multiple children in span tag", function () {
        var flux = new Flux();

        var tree = TestUtils.renderIntoDocument(React.createElement(
          FluxComponent,
          { flux: flux },
          React.createElement("div", null),
          React.createElement("div", null)
        ));

        var wrapper = TestUtils.findRenderedDOMComponentWithTag(tree, "span");
        var divs = TestUtils.scryRenderedDOMComponentsWithTag(tree, "div");

        expect(divs.length).to.equal(2);
      });

      it("does not wrap single child in span tag", function () {
        var flux = new Flux();

        var tree = TestUtils.renderIntoDocument(React.createElement(
          FluxComponent,
          { flux: flux },
          React.createElement("div", null)
        ));

        expect(TestUtils.findRenderedDOMComponentWithTag.bind(TestUtils, tree, "span")).to["throw"]("Did not find exactly one match for tag:span");
      });

      it("allows for nested FluxComponents", function () {
        var flux = new Flux();
        var actions = flux.getActions("test");

        var tree = TestUtils.renderIntoDocument(React.createElement(
          FluxComponent,
          { flux: flux, connectToStores: "test" },
          React.createElement(
            FluxComponent,
            null,
            React.createElement("div", null)
          )
        ));

        var div = TestUtils.findRenderedDOMComponentWithTag(tree, "div");

        actions.getSomething("something good");
        expect(div.props.something).to.equal("something good");
        actions.getSomething("something else");
        expect(div.props.something).to.equal("something else");
      });
    });
    //# sourceMappingURL=data:application/json;base64,