All files / src throughAgentFactory.js

88.24% Statements 15/17
75% Branches 6/8
85.71% Functions 6/7
93.75% Lines 15/16
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          2x   1x           1x               31x       14x 14x 14x 84x   14x 6x         37x 37x           45x       1x   1x      
import React, { Children } from 'react'
import PropTypes from 'prop-types'
 
import throughAgent from './throughAgent'
 
const throughAgentFactory = (area, key) => {
 
  Iif( !(typeof area === 'string' || area instanceof String ) ) {
    throw new Error(
      "type error: throughAgentFactory(area:string, key:string|function)"
    )
  }
 
  const Item = () => null
 
  class ThroughAgent extends React.Component {
    static propTypes = {
      [area]: PropTypes.object,
    }
 
    componentWillMount() {
      this.configureItem(this.props)
    }
 
    componentWillReceiveProps(nextProps) {
      const keys = Object.keys(nextProps).concat(Object.keys(this.props))
      const skip = [area]
      const differences = keys.filter(
        k => (!skip.includes(k) && this.props[k] !== nextProps[k])
      ).length
      if( differences ) {
        this.configureItem(nextProps)
      }
    }
 
    configureItem = (props) => {
      const {[area]: notused, ...data} = props
      props[area].item(
        <Item {...data} />
      )
    }
 
    render() {
      return null
    }
  }
 
  ThroughAgent.displayName = `ThroughAgent.${area}`
 
  return throughAgent(area, key)(ThroughAgent)
}
 
export default throughAgentFactory