All files throughAgentFactory.js

100% Statements 20/20
100% Branches 9/9
100% Functions 7/7
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                              17x 17x 15x             22x 21x 21x   1x         14x 14x 14x           24x 24x           21x       9x   9x       7x   11x 3x         8x   8x      
import React, { Children } from 'react'
import PropTypes from 'prop-types'
 
import hasDiff from './hasDiff'
import hasComplex from './hasComplex'
import throughAgent from './throughAgent'
import Item from './Item'
 
export function createThroughAgentClass(area, key, syncUpdate) {
  class ThroughAgent extends React.Component {
    static propTypes = {
      [area]: PropTypes.object,
    }
 
    constructor(props) {
      super()
      this.configureItem(props)
      this.state = {
        configureItem: this.configureItem,
        props: {}
      }
    }
 
    static getDerivedStateFromProps(props, state) {
      if( hasDiff(props, state.props) ) {
        state.configureItem(props)
        return {...state, props}
      }
      return null
    }
 
    configureItem = syncUpdate === undefined ?
      (props) => {
        const {[area]: notused, ...data} = props
        const syncUpdate = !hasComplex(data)
        props[area].item(
          <Item {...data} />
        , syncUpdate)
      }
      :
      (props) => {
        const {[area]: notused, ...data} = props
        props[area].item(
          <Item {...data} />
        )
      }
 
    render() {
      return null
    }
  }
 
  ThroughAgent.displayName = `ThroughAgent.${area}`
 
  return ThroughAgent
}
 
// this function with *two* params was documented and may be in use
const throughAgentFactory = (area, key, syncUpdate = undefined) => {
 
  if( !(typeof area === 'string' || area instanceof String ) ) {
    throw new Error(
      "type error: throughAgentFactory(area:string, key:string|function)"
    )
  }
 
  const ThroughAgent = createThroughAgentClass(area, key, syncUpdate)
 
  return throughAgent(area, key)(ThroughAgent)
}
 
export default throughAgentFactory