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

## Statements: 84.72% (61 / 72) Branches: 50% (13 / 26) Functions: 100% (16 / 16) Lines: 100% (48 / 48) Ignored: none

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

    2

    2

    2

    3

    3

    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 _defineProperty = function (obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); };

    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;

    describe("Examples:", function () {
      /**
       * A simple Flummox example
       */
      describe("Messages", function () {
        /**
         * To create some actions, create a new class that extends from the base
         * Actions class. Methods on the class's prototype will be converted into
         * actions, each with its own action id.
         *
         * In this example, calling `newMessage` will fire the dispatcher, with
         * a payload containing the passed message content. Easy!
         */
        var MessageActions = (function (Actions) {
          function MessageActions() {
            _classCallCheck(this, MessageActions);

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

          _inherits(MessageActions, Actions);

          _prototypeProperties(MessageActions, null, {
            newMessage: {
              value: function newMessage(content) {
                // The return value from the action is sent to the dispatcher.
                return content;
              },
              writable: true,
              configurable: true
            }
          });

          return MessageActions;
        })(Actions);

        /**
         * Now we need to a Store that will receive payloads from the dispatcher
         * and update itself accordingly. Like before, create a new class that
         * extends from the Store class.
         *
         * Stores are automatically registered with the dispatcher, but rather than
         * using a giant `switch` statement to check for specific action types, we
         * register handlers with action ids, or with a reference to the action
         * itself.
         *
         * Stores have a React-inspired API for managing state. Use `this.setState`
         * to update state within your handlers. Multiple calls to `this.setState`
         * within the same handler will be batched. A change event will fire after
         * the batched updates are applied. Your view controllers can listen
         * for change events using the EventEmitter API.
         */
        var MessageStore = (function (Store) {
          // Note that passing a flux instance to the constructor is not required;
          // we do it here so we have access to any action ids we're interested in.
          function MessageStore(flux) {
            _classCallCheck(this, MessageStore);

            // Don't forget to call the super constructor
            _get(Object.getPrototypeOf(MessageStore.prototype), "constructor", this).call(this);

            var messageActions = flux.getActions("messages");
            this.register(messageActions.newMessage, this.handleNewMessage);
            this.messageCounter = 0;

            this.state = {};
          }

          _inherits(MessageStore, Store);

          _prototypeProperties(MessageStore, null, {
            handleNewMessage: {
              value: function handleNewMessage(content) {
                var id = this.messageCounter++;

                this.setState(_defineProperty({}, id, {
                  content: content,
                  id: id }));
              },
              writable: true,
              configurable: true
            }
          });

          return MessageStore;
        })(Store);

        /**
         * Here's where it all comes together. Extend from the base Flummox class
         * to create a class that encapsulates your entire flux set-up.
         */
        var Flux = (function (Flummox) {
          function Flux() {
            _classCallCheck(this, Flux);

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

            // Create actions first so our store can reference them in
            // its constructor
            this.createActions("messages", MessageActions);

            // Extra arguments are sent to the store's constructor. Here, we're
            // padding a reference to this flux instance
            this.createStore("messages", MessageStore, this);
          }

          _inherits(Flux, Flummox);

          return Flux;
        })(Flummox);

        /**
         * And that's it! No need for singletons or global references -- just create
         * a new instance.
         *
         * Now let's test it.
         */

        it("creates new messages", function () {
          var flux = new Flux();
          var messageStore = flux.getStore("messages");
          var messageActions = flux.getActions("messages");

          expect(messageStore.state).to.deep.equal({});

          messageActions.newMessage("Hello, world!");
          expect(messageStore.state).to.deep.equal(_defineProperty({}, 0, {
            content: "Hello, world!",
            id: 0 }));
        });
      });
    });
    //# sourceMappingURL=data:application/json;base64,