Created
January 17, 2018 03:51
-
-
Save theKashey/9d3d0235d48811bd8e438c39b9f33372 to your computer and use it in GitHub Desktop.
Enzyme 2-to-3 connector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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