Skip to content

Instantly share code, notes, and snippets.

@theKashey
Created January 17, 2018 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theKashey/9d3d0235d48811bd8e438c39b9f33372 to your computer and use it in GitHub Desktop.
Save theKashey/9d3d0235d48811bd8e438c39b9f33372 to your computer and use it in GitHub Desktop.
Enzyme 2-to-3 connector
import { mount as realMount, shallow as realShallow, render as realRender } from 'enzyme';
const proxyfy = (wrapper, settings = {}) => new Proxy(wrapper, {
get(target, prop) {
// update first
if (prop !== 'update' && !settings.child) {
if (target.update) {
target.update();
}
}
// v2 compatible children
if (prop === 'children') {
return () => {
const children = target.prop('children');
if (Array.isArray(children)) {
return children.filter(x => !!x);
}
return children;
};
}
if (prop === 'childAt') {
return n => target.wrap(target.prop('children')[n]);
}
if (prop === 'find') {
return (...args) => {
let result = Reflect.apply(target[prop], target, args);
if (typeof args[0] === 'string' && result.length > 1) {
result = result.hostNodes();
}
return proxyfy(result, { child: true });
};
}
return Reflect.get(target, prop);
}
});
export const shallow = (node, params = {}) => proxyfy(realShallow(node, { disableLifecycleMethods: true, ...params }));
export const mount = (node, params = {}) => proxyfy(realMount(node, params));
export const render = realRender;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment