enzyme-adapter-preact-pure
Version:
Enzyme adapter for Preact
77 lines (76 loc) • 3.14 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const MountRenderer_js_1 = __importDefault(require("./MountRenderer.js"));
const shallow_render_utils_js_1 = require("./shallow-render-utils.js");
const compat_js_1 = require("./compat.js");
const util_js_1 = require("./util.js");
class ShallowRenderer {
constructor(options = {}) {
this._mountRenderer = new MountRenderer_js_1.default(options);
this._options = options;
}
render(el, context, options) {
// Make all elements in the input tree, except for the root element, render
// to a stub.
(0, compat_js_1.childElements)(el).forEach(el => {
if (el != null && typeof el !== 'string') {
(0, shallow_render_utils_js_1.shallowRenderVNodeTree)(el);
}
});
// Make any new elements rendered by the root element render to a stub.
(0, shallow_render_utils_js_1.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;
(0, shallow_render_utils_js_1.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) {
(0, shallow_render_utils_js_1.withShallowRendering)(() => {
this._mountRenderer.simulateError(nodeHierarchy, rootNode, error);
});
}
simulateEvent(node, eventName, args) {
(0, shallow_render_utils_js_1.withShallowRendering)(() => {
if (this._options.simulateEventsOnComponents) {
const handler = node.props[(0, util_js_1.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();
}
}
exports.default = ShallowRenderer;