All files / src/components BlockComponent.js

88.89% Statements 16/18
94.74% Branches 18/19
75% Functions 3/4
88.89% Lines 16/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 531x 1x           187x       1925x     187x 91x   96x 969x   4x     92x 956x         92x                     95x 95x     95x         95x      
import { Component } from 'react';
import PropTypes from 'prop-types/prop-types';
 
// Check to see whether two `prop` or `state` objects are roughly equal to each
// other, enough so that we don't need to re-render a node if that's all that
// changed.
function vaguelyEqual(x, y) {
  const ignoreProps = ["location", "children", "ast", "hash"];
  function ignoreProp(object, prop) {
    // There _shouldn't_ be any relevant differences between functions in `props`.
    // We hope, we hope.
    return ignoreProps.includes(prop) || typeof object[prop] === "function";
  }
  
  if (x === y) {
    return true;
  }
  for (let prop in x) {
    if (Object.prototype.hasOwnProperty.call(x,prop)
     && !ignoreProp(x, prop) && x[prop] !== y[prop]) {
      return false;
    }
  }
  for (let prop in y) {
    Iif (Object.prototype.hasOwnProperty.call(y,prop) 
      && !ignoreProp(y, prop) && y[prop] !== x[prop]) {
      return false;
    }
  }
  return true;
}
 
export default class BlockComponent extends Component {
  static propTypes = {
    node: PropTypes.object
  }
 
  shouldComponentUpdate(props, state) {
    // NOTE: don't care about the node since the patching algorithm will deal
    // with the update already
    const {node: newValue, ...newProps} = props;
    const {node: oldValue, ...oldProps} = this.props;
 
    const shouldUpdate = (
      (newValue && oldValue && newValue.hash !== oldValue.hash) ||
        !vaguelyEqual(newProps, oldProps) ||
        !vaguelyEqual(state, this.state)
    );
    
    return shouldUpdate;
  }
}