All files index.js

94.44% Statements 17/18
50% Branches 6/12
87.5% Functions 7/8
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 54 55 56 57 58 59 60 61 62 63 64 651x 1x   16x 16x                               3x     12x     4x   4x   4x           4x             4x 4x   4x                 4x     4x            
import React from 'react'
import PropTypes from 'prop-types'
 
const connect = Component => {
  class RefunkState extends React.Component {
    static childContextTypes = {
      state: PropTypes.object,
      update: PropTypes.func
    }
 
    static contextTypes = {
      state: PropTypes.object,
      update: PropTypes.func
    }
 
    static propTypes = {
      mapState: PropTypes.func
    }
 
    static defaultProps = {
      mapState: n => n
    }
 
    constructor (props, context) {
      super()
 
      this.child = isChild(context)
 
      this.state = this.child ? null : {...props}
 
      this.update = this.child
        ? context.update
        : fn => this.setState(fn)
    }
 
    getChildContext () {
      return this.child ? this.context : {
        state: this.state,
        update: this.update
      }
    }
 
    render () {
      const { mapState } = this.props
      const state = this.child ? this.context.state : mapState({...this.state})
 
      return (
        <Component
          {...state}
          update={this.update}
        />
      )
    }
  }
 
  return RefunkState
}
 
const isChild = (context) => (
  typeof context.state === 'object'
  && typeof context.update === 'function'
)
 
export default connect