All files / src Loader.js

100% Statements 19/19
57.14% Branches 8/14
100% Functions 8/8
100% Lines 18/18

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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110          1x                                 1x             6x                   50x                 10x   10x 1x                             9x   9x   36x                     9x               9x   9x       10x   10x   9x 9x   9x              
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import {omit, gt, has, reduce, without} from 'lodash'
 
/* eslint-disable no-unused-vars */
const PROP_TYPES = {
  borderWidth: PropTypes.string,
  children: PropTypes.element,
  color: PropTypes.string,
  duration: PropTypes.string,
  huge: PropTypes.bool,
  large: PropTypes.bool,
  medium: PropTypes.bool,
  secondaryColor: PropTypes.string,
  size: PropTypes.string,
  small: PropTypes.bool,
}
 
/*
 * Size-related props.
 * Itemizes style configurations for the various size props that may be passed.
 **/
const sizeRelatedProps = ['huge', 'large', 'medium', 'size', 'small']
 
/*
 * Increment.
 * Provided a number, returns its value, incremented by one.
 **/
function inc(num = 0) {
  return num + 1
}
 
export default class Loader extends Component {
  /*
   * Sum properties in object.
   * Provided a list of attributes, and provided an object, returns an integer
   * representing the total number of attributes encompassed by the object.
   **/
  sumPropsInObj(attrs = [], obj = {}) {
    return reduce(attrs, (acc, curr) => (has(obj, curr) ? inc(acc) : acc), 0)
  }
 
  /*
   * Ensure no prop conflicts.
   * Verifies that developer has not passed any conflicting props. Provided so,
   * throws an error.
   **/
  ensureNoConflicts(props = {}) {
    const sum = this.sumPropsInObj(sizeRelatedProps, props)
 
    if (gt(sum, 1)) {
      throw Error(
        `You have specified more than one of the following size-related props:
        small, medium, large, huge, size. Only one of these props may be
        specified per each component instance.`
      )
    }
  }
 
  /*
   * Resolve class name.
   * Provided `this.props`, return a `className` that reflects only up to one of
   * our size-related props, such as `small`, `medium`, `large`, or `huge`.
   **/
  resolveClassNames(props = {}) {
    // Allocate all size-related props except `size`.
    const classes = without(sizeRelatedProps, 'size')
 
    return reduce(
      classes,
      (acc, curr) => (props[curr] ? acc.concat(`rev-Loader--${curr}`) : acc),
      ''
    )
  }
 
  /*
   * Resolve styles.
   * Provided `this.props`, return a consolidated `styles` object, using
   * `this.props.style` as overrides.
   **/
  resolveStyles(props = {}) {
    const styles = {
      animationDuration: props.duration,
      borderColor: props.secondaryColor,
      borderTopColor: props.color,
      borderWidth: props.borderWidth,
      height: props.size,
      width: props.size,
    }
    const overrides = props.style || {}
 
    return {...styles, ...overrides}
  }
 
  render() {
    const props = omit(this.props, 'className')
 
    this.ensureNoConflicts(props)
 
    const classes = this.resolveClassNames(props)
    const styles = this.resolveStyles(props)
 
    return (
      <div className={`rev-Loader ${classes}`} style={styles}>
        {props.children}
      </div>
    )
  }
}