All files / src Social.js

100% Statements 18/18
50% Branches 1/2
100% Functions 12/12
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          1x 1x 1x 1x 3x 1x   1x   1x 1x   1x   2x                                         13x   13x         13x 13x 13x 13x               13x      
import {Component, createElement} from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import {omit} from 'lodash'
 
const URL_FUNCTIONS = {
  BUFFER: (url, text) => `https://buffer.com/add?text=${text}&url=${url}`,
  DIGG: (url, text) => `http://digg.com/submit?url=${url}&title=${text}`,
  EMAIL: (url, text) => `mailto:?subject=${text}&body=${url}`,
  FACEBOOK: (url) => `https://www.facebook.com/sharer/sharer.php?u=${url}`,
  GOOGLE_PLUS: (url) => `https://plus.google.com/share?url=${url}`,
  LINKEDIN: (url, text) =>
    `https://www.linkedin.com/shareArticle?url=${url}&title=${text}`,
  PINTEREST: (url, text) =>
    `https://pinterest.com/pin/create/bookmarklet/?url=${url}&description=${text}`,
  REDDIT: (url, text) => `https://reddit.com/submit?url=${url}&title=${text}`,
  TUMBLR: (url, text) =>
    `https://www.tumblr.com/widgets/share/tool?canonicalUrl=${url}&caption=${text}`,
  TWITTER: (url, text) =>
    `https://twitter.com/intent/tweet?url=${url}&text=${text}`,
}
 
export default class Social extends Component {
  static types = URL_FUNCTIONS
 
  static propTypes = {
    type: PropTypes.func.isRequired,
    url: PropTypes.string.isRequired,
    message: PropTypes.string,
    componentClass: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
    className: PropTypes.string,
    children: PropTypes.node,
  }
 
  static defaultProps = {
    message: 'Check this out!',
    componentClass: 'a',
  }
 
  get href() {
    const {type, url, message} = this.props
 
    return type(encodeURIComponent(url), encodeURIComponent(message))
  }
 
  render() {
    // Extract props that will not pass through.
    const {className, componentClass, children, ...props} = this.props
    const componentClassName = classNames(className, 'social')
    const omittedProps = omit(props, ['url', 'message'])
    const componentProps = {
      ...omittedProps,
      href: this.href,
      rel: 'noopener noreferrer',
      target: '_blank',
      className: componentClassName,
    }
 
    return createElement(componentClass || 'a', componentProps, children)
  }
}