All files / src/test FakePositionStore.js

95% Statements 19/20
75% Branches 6/8
77.78% Functions 7/9
94.74% Lines 18/19
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                                                      27x 27x       27x   42x         3x 3x 3x         27x 5x       29x         37x   37x           2x       29x 5x           24x         4x                       4x          
/**
* Copyright 2018, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
 
import React, { Component } from 'react';
import PropTypes from 'prop-types';
 
import createMSAStore from '../store/createMSAStore';
import MSAProvider from '../store/provider';
 
import {
  pick,
  omit,
} from 'lodash-es';
 
/**
 * Forwards all passed-in properties to an mocked `positionStore`
 *
 * Special properties:
 *  - `subscribe` (allows to overwrite the subscribe method of the mocked store)
 */
class FakePositionStore extends Component {
  constructor(props) {
    super(props);
    const positionAttributes = [
      "xPosOffset", "yPosOffset",
      "currentViewSequence", "currentViewSequencePosition", "position",
    ]
    this.positionStore = {
      actions: [],
      getState: () => ({
        ...pick(this.props, positionAttributes),
      }),
      subscribe: this.subscribe,
      dispatch: (e) => {
        this.positionStore.actions.push(e);
        Eif (this._subscribe !== undefined) {
          this._subscribe();
        }
      }
    };
    // only if defined
    if (this.props.sequences) {
      this.msaStore = createMSAStore(omit(props, positionAttributes));
    }
  }
  getChildContext() {
    return {
      positionMSAStore: this.positionStore,
    };
  }
  subscribe = (fn) => {
    this._subscribe = fn;
    // unsubscribe callback
    return () => {
      this._subscribe = undefined;
    }
  }
  componentDidUpdate() {
    // notify listeners
    Eif (this._subscribe) this._subscribe();
  }
  render() {
    // only inject the msaStore if defined
    if (this.msaStore) {
      return (<MSAProvider store={this.msaStore}>
        <div>
          { this.props.children }
        </div>
      </MSAProvider>);
    } else {
      return this.props.children;
    }
  }
}
 
FakePositionStore.defaultProps = {
  subscribe: () => {},
  yPosOffset: 0,
  xPosOffset: 0,
  currentViewSequence: 0,
  currentViewSequencePosition: 0,
  position: {
    xPos: 0,
    yPos: 0,
  },
}
 
FakePositionStore.childContextTypes = {
  positionMSAStore: PropTypes.object,
};
 
export default FakePositionStore;