All files index.js

100% Statements 16/16
91.67% Branches 11/12
100% Functions 7/7
100% Lines 14/14
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 561x 1x   8x 4x                     12x     4x   4x   4x   1x       5x             5x 5x                   2x     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
    }
 
    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 state = this.child ? this.context.state : this.state
      return (
        <Component
          {...this.props}
          {...state}
          update={this.update}
        />
      )
    }
  }
 
  return RefunkState
}
 
const isChild = (context) => (
  typeof context.state === 'object'
  && typeof context.update === 'function'
)
 
export default connect