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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | 185x 192x 13x 179x 1x 178x 7x 171x 171x 10x 413x 8x 405x 405x 37x 30x 7x 48x 52x 48x 48x 48x 48x 30x 18x 12x 6x 519x 58x 58x 58x 58x 58x 54x 54x 54x 54x 72x 72x 72x 54x 4x 4x 168x 168x 150x 18x 16x 2x 54x 9x 9x 9x 38x 38x 38x 29x 29x 3x 29x 9x 94x 94x 94x 157x 2x 138x 1x 137x 12x 125x 89x 19x 19x 19x 17x 17x 14x 3x 482x 482x 482x 482x 482x 482x 465x 465x 465x 351x 351x 351x 17x 17x 482x 114x | import { isEmpty } from 'underscore'; import isSubset from 'is-subset'; import { coercePropValue, nodeEqual, propsOfNode, isSimpleSelector, splitSelector, selectorError, selectorType, isCompoundSelector, AND, SELECTOR, } from './Utils'; import { isDOMComponent, isCompositeComponent, isCompositeComponentWithType, isElement, findDOMNode, } from './react-compat'; import { REACT013, REACT014 } from './version'; export function internalInstance(inst) { return inst._reactInternalInstance; } export function getNode(inst) { if (!inst || inst._store || typeof inst === 'string') { return inst; } if (inst._currentElement) { return inst._currentElement; } if (internalInstance(inst)) { return internalInstance(inst)._currentElement; } Eif (inst._reactInternalComponent) { return inst._reactInternalComponent._currentElement; } return inst; } export function instEqual(a, b) { return nodeEqual(getNode(a), getNode(b)); } export function instHasClassName(inst, className) { if (!isDOMComponent(inst)) { return false; } const classes = findDOMNode(inst).className || ''; return ` ${classes} `.indexOf(` ${className} `) > -1; } export function instHasId(inst, id) { if (!isDOMComponent(inst)) return false; const instId = findDOMNode(inst).id || ''; return instId === id; } export function instHasType(inst, type) { switch (typeof type) { case 'string': return isDOMComponent(inst) && inst.tagName.toUpperCase() === type.toUpperCase(); case 'function': return isCompositeComponentWithType(inst, type); default: return false; } } export function instHasProperty(inst, propKey, stringifiedPropValue) { if (!isDOMComponent(inst)) return false; const node = getNode(inst); const nodeProps = propsOfNode(node); const nodePropValue = nodeProps[propKey]; const propValue = coercePropValue(propKey, stringifiedPropValue); // intentionally not matching node props that are undefined if (nodePropValue === undefined) { return false; } if (propValue) { return nodePropValue === propValue; } return nodeProps.hasOwnProperty(propKey); } // called with private inst export function renderedChildrenOfInst(inst) { return REACT013 ? inst._renderedComponent._renderedChildren : inst._renderedChildren; } // called with a private instance export function childrenOfInstInternal(inst) { Iif (!inst) { return []; } Iif (!inst.getPublicInstance) { const internal = internalInstance(inst); return childrenOfInstInternal(internal); } const publicInst = inst.getPublicInstance(); const currentElement = inst._currentElement; if (isDOMComponent(publicInst)) { const children = []; const renderedChildren = renderedChildrenOfInst(inst); let key; for (key in renderedChildren) { Iif (!renderedChildren.hasOwnProperty(key)) { continue; } Iif (REACT013 && !renderedChildren[key].getPublicInstance) { continue; } children.push(renderedChildren[key].getPublicInstance()); } return children; } else Eif ( REACT014 && isElement(currentElement) && typeof currentElement.type === 'function' ) { return childrenOfInstInternal(inst._renderedComponent); } else if ( REACT013 && isCompositeComponent(publicInst) ) { return childrenOfInstInternal(inst._renderedComponent); } return []; } export function internalInstanceOrComponent(node) { Iif (REACT013) { return node; } else if (node._reactInternalComponent) { return node._reactInternalComponent; } else if (node._reactInternalInstance) { return node._reactInternalInstance; } return node; } export function childrenOfInst(node) { return childrenOfInstInternal(internalInstanceOrComponent(node)); } export function pathToNode(node, root) { const queue = [root]; const path = []; while (queue.length) { const current = queue.pop(); const children = childrenOfInst(current); if (current === node) return path; path.push(current); if (children.length === 0) { // leaf node. if it isn't the node we are looking for, we pop. path.pop(); } queue.push.apply(queue, children); } return null; } export function parentsOfInst(inst, root) { return pathToNode(inst, root).reverse(); } export function instMatchesObjectProps(inst, props) { Iif (!isDOMComponent(inst)) return false; const node = getNode(inst); return isSubset(propsOfNode(node), props); } export function buildInstPredicate(selector) { switch (typeof selector) { case 'function': // selector is a component constructor return inst => instHasType(inst, selector); case 'string': if (!isSimpleSelector(selector)) { throw selectorError(selector); } if (isCompoundSelector.test(selector)) { return AND(splitSelector(selector).map(buildInstPredicate)); } switch (selectorType(selector)) { case SELECTOR.CLASS_TYPE: return inst => instHasClassName(inst, selector.substr(1)); case SELECTOR.ID_TYPE: return inst => instHasId(inst, selector.substr(1)); case SELECTOR.PROP_TYPE: const propKey = selector.split(/\[([a-zA-Z\-\:]*?)(=|\])/)[1]; const propValue = selector.split(/=(.*?)]/)[1]; return node => instHasProperty(node, propKey, propValue); default: // selector is a string. match to DOM tag or constructor displayName return inst => instHasType(inst, selector); } break; case 'object': if (!Array.isArray(selector) && selector !== null && !isEmpty(selector)) { return node => instMatchesObjectProps(node, selector); } throw new TypeError( 'Enzyme::Selector does not support an array, null, or empty object as a selector' ); default: throw new TypeError(`Enzyme::Selector expects a string, object, or Component Constructor`); } } // This function should be called with an "internal instance". Nevertheless, if it is // called with a "public instance" instead, the function will call itself with the // internal instance and return the proper result. function findAllInRenderedTreeInternal(inst, test) { Iif (!inst) { return []; } Iif (!inst.getPublicInstance) { const internal = internalInstance(inst); return findAllInRenderedTreeInternal(internal, test); } const publicInst = inst.getPublicInstance(); let ret = test(publicInst) ? [publicInst] : []; const currentElement = inst._currentElement; if (isDOMComponent(publicInst)) { const renderedChildren = renderedChildrenOfInst(inst); let key; for (key in renderedChildren) { Iif (!renderedChildren.hasOwnProperty(key)) { continue; } Iif (REACT013 && !renderedChildren[key].getPublicInstance) { continue; } ret = ret.concat( findAllInRenderedTreeInternal(renderedChildren[key], test) ); } } else Eif ( REACT014 && isElement(currentElement) && typeof currentElement.type === 'function' ) { ret = ret.concat( findAllInRenderedTreeInternal( inst._renderedComponent, test ) ); } else if ( REACT013 && isCompositeComponent(publicInst) ) { ret = ret.concat( findAllInRenderedTreeInternal( inst._renderedComponent, test ) ); } return ret; } // This function could be called with a number of different things technically, so we need to // pass the *right* thing to our internal helper. export function treeFilter(node, test) { return findAllInRenderedTreeInternal(internalInstanceOrComponent(node), test); } |