All files / src/components subcomponent.js

100% Statements 15/15
75% Branches 3/4
100% Functions 2/2
100% Lines 15/15
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 491x 1x   1x     3x     1x 1x 3x   1x     1x       1x       1x             1x       1x         1x       1x        
import React from 'react';
import PropTypes from 'prop-types';
 
import { streamKey, storeKey } from '../constants';
 
export class Subcomponent extends React.Component {
  constructor(props, context) {
    super(props, context);
 
    const stream = context[streamKey];
    const store = context[storeKey];
    const {name, metadata, selector} = props;
 
    this[storeKey] = {
      ...store,
      getState: () => {
        return selector(store.getState());
      },
    };
 
    this[streamKey] = stream && stream.subWithMeta(name, metadata);
  }
 
  getChildContext() {
    return {
      [streamKey]: this[streamKey],
      [storeKey]: this[storeKey],
    }
  }
 
  render() {
    return React.Children.only(this.props.children);
  }
}
 
Subcomponent.propTypes = {
    name: PropTypes.any.isRequired,
    metadata: PropTypes.any,
    selector: PropTypes.func.isRequired,
};
Subcomponent.childContextTypes = {
    [streamKey]: PropTypes.any.isRequired,
    [storeKey]: PropTypes.any.isRequired,
};
Subcomponent.contextTypes = {
    [streamKey]: PropTypes.any.isRequired,
    [storeKey]: PropTypes.any.isRequired,
};