All files / enzyme/src Utils.js

90.22% Statements 83/92
80.95% Branches 68/84
100% Functions 19/19
93.51% Lines 72/77
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228                          1556x     1556x       2x       18x 18x     18x 18x 16x 16x 16x   9x       86x 76x 76x 52x 52x 52x 52x 39x 39x 34x 18x 9x   16x   6x 2x   4x       33x 24x     9x       22x 22x       20x       22x   22x             6x 6x             10x 10x       10x 10x           31x         294x       2x         1x   1x   1x             259x 182x 77x 2x 75x 41x         24x 70x 70x 96x   18x       78x   117x 39x     78x         38x     40x       20x       8x 12x                   14x                                                                     14x     14x 14x     14x    
/* eslint no-use-before-define:0 */
import { isEqual } from 'underscore';
import {
  isDOMComponent,
  findDOMNode,
  childrenToArray,
} from './react-compat';
import {
  REACT013,
  REACT014,
} from './version';
 
export function propsOfNode(node) {
  Iif (REACT013 && node && node._store) {
    return (node._store.props) || {};
  }
  return (node && node.props) || {};
}
 
export function getNode(node) {
  return isDOMComponent(node) ? findDOMNode(node) : node;
}
 
export function childrenEqual(a, b) {
  Iif (a === b) return true;
  Iif (!Array.isArray(a) && !Array.isArray(b)) {
    return nodeEqual(a, b);
  }
  Iif (!a && !b) return true;
  if (a.length !== b.length) return false;
  Iif (a.length === 0 && b.length === 0) return true;
  for (let i = 0; i < a.length; i++) {
    if (!nodeEqual(a[i], b[i])) return false;
  }
  return true;
}
 
export function nodeEqual(a, b) {
  if (a === b) return true;
  Iif (!a || !b) return false;
  if (a.type !== b.type) return false;
  const left = propsOfNode(a);
  const leftKeys = Object.keys(left);
  const right = propsOfNode(b);
  for (let i = 0; i < leftKeys.length; i++) {
    const prop = leftKeys[i];
    if (!(prop in right)) return false;
    if (prop === 'children') {
      if (!childrenEqual(childrenToArray(left.children), childrenToArray(right.children))) {
        return false;
      }
    } else if (right[prop] === left[prop]) {
      // continue;
    } else if (typeof right[prop] === typeof left[prop] && typeof left[prop] === 'object') {
      if (!isEqual(left[prop], right[prop])) return false;
    } else {
      return false;
    }
  }
 
  if (typeof a !== 'string' && typeof a !== 'number') {
    return leftKeys.length === Object.keys(right).length;
  }
 
  return false;
}
 
export function containsChildrenSubArray(match, node, subArray) {
  const children = childrenOfNode(node);
  return children.some((_, i) => arraysEqual(match, children.slice(i, subArray.length), subArray));
}
 
function arraysEqual(match, left, right) {
  return left.length === right.length && left.every((el, i) => match(el, right[i]));
}
 
function childrenOfNode(node) {
  const props = propsOfNode(node);
  const { children } = props;
  return childrenToArray(children);
}
 
 
// 'click' => 'onClick'
// 'mouseEnter' => 'onMouseEnter'
export function propFromEvent(event) {
  const nativeEvent = mapNativeEventNames(event);
  return `on${nativeEvent[0].toUpperCase()}${nativeEvent.substring(1)}`;
}
 
export function withSetStateAllowed(fn) {
  // NOTE(lmr):
  // this is currently here to circumvent a React bug where `setState()` is
  // not allowed without global being defined.
  let cleanup = false;
  Iif (typeof global.document === 'undefined') {
    cleanup = true;
    global.document = {};
  }
  fn();
  Iif (cleanup) {
    delete global.document;
  }
}
 
export function splitSelector(selector) {
  return selector.split(/(?=\.|\[.*\])/);
}
 
export function isSimpleSelector(selector) {
  // any of these characters pretty much guarantee it's a complex selector
  return !/[~\s:>]/.test(selector);
}
 
export function selectorError(selector) {
  return new TypeError(
    `Enzyme received a complex CSS selector ('${selector}') that it does not currently support`
  );
}
 
export const isCompoundSelector = /([a-z]\.[a-z]|[a-z]\[.*\])/i;
 
const isPropSelector = /^\[.*\]$/;
 
export const SELECTOR = {
  CLASS_TYPE: 0,
  ID_TYPE: 1,
  PROP_TYPE: 2,
};
 
export function selectorType(selector) {
  if (selector[0] === '.') {
    return SELECTOR.CLASS_TYPE;
  } else if (selector[0] === '#') {
    return SELECTOR.ID_TYPE;
  } else if (isPropSelector.test(selector)) {
    return SELECTOR.PROP_TYPE;
  }
}
 
export function AND(fns) {
  return x => {
    let i = fns.length;
    while (i--) {
      if (!fns[i](x)) return false;
    }
    return true;
  };
}
 
export function coercePropValue(propName, propValue) {
  // can be undefined
  if (propValue === undefined) {
    return propValue;
  }
 
  const trimmedValue = propValue.trim();
 
  // if propValue includes quotes, it should be
  // treated as a string
  if (/^(['"]).*\1$/.test(trimmedValue)) {
    return trimmedValue.slice(1, -1);
  }
 
  const numericPropValue = +trimmedValue;
 
  // if parseInt is not NaN, then we've wanted a number
  if (!isNaN(numericPropValue)) {
    return numericPropValue;
  }
 
  // coerce to boolean
  if (trimmedValue === 'true') return true;
  if (trimmedValue === 'false') return false;
 
  // user provided an unquoted string value
  throw new TypeError(
    `Enzyme::Unable to parse selector '[${propName}=${propValue}]'. ` +
    `Perhaps you forgot to escape a string? Try '[${propName}="${trimmedValue}"]' instead.`
  );
}
 
export function mapNativeEventNames(event) {
  const nativeToReactEventMap = {
    compositionend: 'compositionEnd',
    compositionstart: 'compositionStart',
    compositionupdate: 'compositionUpdate',
    keydown: 'keyDown',
    keyup: 'keyUp',
    keypress: 'keyPress',
    contextmenu: 'contextMenu',
    doubleclick: 'doubleClick',
    dragend: 'dragEnd',
    dragenter: 'dragEnter',
    dragexist: 'dragExit',
    dragleave: 'dragLeave',
    dragover: 'dragOver',
    dragstart: 'dragStart',
    mousedown: 'mouseDown',
    mousemove: 'mouseMove',
    mouseout: 'mouseOut',
    mouseover: 'mouseOver',
    mouseup: 'mouseUp',
    touchcancel: 'touchCancel',
    touchend: 'touchEnd',
    touchmove: 'touchMove',
    touchstart: 'touchStart',
    canplay: 'canPlay',
    canplaythrough: 'canPlayThrough',
    durationchange: 'durationChange',
    loadeddata: 'loadedData',
    loadedmetadata: 'loadedMetadata',
    loadstart: 'loadStart',
    ratechange: 'rateChange',
    timeupdate: 'timeUpdate',
    volumechange: 'volumeChange',
  };
 
  Eif (REACT014) {
    // these could not be simulated in React 0.13:
    // https://github.com/facebook/react/issues/1297
    nativeToReactEventMap.mouseenter = 'mouseEnter';
    nativeToReactEventMap.mouseleave = 'mouseLeave';
  }
 
  return nativeToReactEventMap[event] || event;
}