UNPKG

enzyme-adapter-preact-pure

Version:

Enzyme adapter for Preact

71 lines (70 loc) 2.7 kB
import MountRenderer from './MountRenderer.js'; import { withShallowRendering, shallowRenderVNodeTree, } from './shallow-render-utils.js'; import { childElements } from './compat.js'; import { propFromEvent } from './util.js'; export default class ShallowRenderer { constructor(options = {}) { this._mountRenderer = new MountRenderer(options); this._options = options; } render(el, context, options) { // Make all elements in the input tree, except for the root element, render // to a stub. childElements(el).forEach(el => { if (el != null && typeof el !== 'string') { shallowRenderVNodeTree(el); } }); // Make any new elements rendered by the root element render to a stub. withShallowRendering(() => { this._mountRenderer.render(el, context); const rootNode = this._mountRenderer.getNode(); if (rootNode.type === 'host') { return; } // Monkey-patch the component's `render` to make it shallow-render. const instance = rootNode.instance; const originalRender = instance.render; instance.render = function (...args) { let result; withShallowRendering(() => { result = originalRender.call(this, ...args); }); return result; }; // Monkey-patch `componentDidMount` to prevent it being called a second // time after `render` returns. React's shallow renderer does not // invoke lifecycle methods so Enzyme tries to invoke them manually. This // is not necessary for the Preact adapter because shallow rendering // works the same as normal rendering. instance.componentDidMount = () => { }; }); } simulateError(nodeHierarchy, rootNode, error) { withShallowRendering(() => { this._mountRenderer.simulateError(nodeHierarchy, rootNode, error); }); } simulateEvent(node, eventName, args) { withShallowRendering(() => { if (this._options.simulateEventsOnComponents) { const handler = node.props[propFromEvent(eventName)]; if (handler) { handler(args); } } else { this._mountRenderer.simulateEvent(node, eventName, args); } }); } unmount() { this._mountRenderer.unmount(); } getNode() { return this._mountRenderer.getNode(); } batchedUpdates(fn) { fn(); } }