Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 4x 4x 1x 3x 1x 3x 1x 1x 1x 1x 1x 1x 1x | import React, { options as preactOptions, render, h } from 'preact';
let shouldRenderShallow;
let previousVNodeHook;
// wrap the Preact.render() to make it render shallow with a vnode hook
// adapted from https://gist.github.com/robertknight/88e9d10cff9269c55d453e5fb8364f47
export function createShallowRenderer({ prefix = 'h-', context = {} } = {}) {
shouldRenderShallow = 2;
const vnodeHook = node => {
Iif (previousVNodeHook) {
previousVNodeHook(node);
}
if (typeof node.nodeName === 'string') {
return;
}
if (shouldRenderShallow <= 0) {
node.nodeName = prefix + node.nodeName.name;
}
shouldRenderShallow--;
};
preactOptions.vnode = vnodeHook;
return (vnode, parent, merge) => {
const vnodeWithContext = h(ContextWrapper, { context }, vnode);
render(vnodeWithContext, parent, merge);
preactOptions.vnode = previousVNodeHook;
};
}
// handy function to dispatch events and return a promise which is resolved after next tick
export function simulate(node, event) {
// pass a selector or a dom node
if (typeof node === 'string') {
node = document.querySelector(node);
}
// pass an event object or an event name
event = event instanceof window.Event ? event : new window.Event(event);
node.dispatchEvent(event);
// return a promise that resolves on next tick so the invoking code see the update
return new Promise(resolve => {
process.nextTick(() => {
resolve();
});
});
}
// wraps the node under test so a test context can be injected
function ContextWrapper({ context, children }) {
this.context = Object.assign(this.context, context);
return <div> {children} </div>;
}
|