UNPKG

enzyme-adapter-preact-pure

Version:

Enzyme adapter for Preact

70 lines (69 loc) 2.36 kB
"use strict"; /** * Helper functions to enable this library to work with different versions of * Preact. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.childElements = exports.componentForDOMNode = exports.render = void 0; const preact_1 = require("preact"); const preact10_internals_js_1 = require("./preact10-internals.js"); const util_js_1 = require("./util.js"); var preact_2 = require("preact"); Object.defineProperty(exports, "render", { enumerable: true, get: function () { return preact_2.render; } }); /** * Search a tree of Preact v10 VNodes for the one that produced a given DOM element. */ function findVNodeForDOM(vnode, el, filter) { if ((0, preact10_internals_js_1.getDOMNode)(vnode) === el && filter(vnode)) { return vnode; } // Test children of this vnode. const children = (0, preact10_internals_js_1.getChildren)(vnode); if (children) { for (const child of children) { if (typeof child === 'string') { continue; } const match = findVNodeForDOM(child, el, filter); if (match) { return match; } } } return null; } /** * Find the `Component` instance that produced a given DOM node. */ function componentForDOMNode(el) { // Search up the tree until we find the container that the root vnode was // rendered into, then traverse the vnode tree to find the component vnode // that produced the DOM element. let parentEl = el.parentNode; let rootVNode = null; while (parentEl && !rootVNode) { rootVNode = (0, preact10_internals_js_1.getLastVNodeRenderedIntoContainer)(parentEl); parentEl = parentEl.parentNode; } if (rootVNode) { const vnode = findVNodeForDOM(rootVNode, el, v => v.type !== preact_1.Fragment); if (vnode) { return (0, preact10_internals_js_1.getComponent)(vnode); } } return null; } exports.componentForDOMNode = componentForDOMNode; /** * Return the children of a VNode. */ function childElements(el) { if (typeof el.props !== 'object' || el.props == null) { return []; } if (typeof el.props.children !== 'undefined') { return (0, util_js_1.toArray)(el.props.children); } return []; } exports.childElements = childElements;