Skip to content

Instantly share code, notes, and snippets.

@sagiavinash
Last active September 4, 2018 13:20
Show Gist options
  • Save sagiavinash/7e2b41891c5230afc469a8edf8e91066 to your computer and use it in GitHub Desktop.
Save sagiavinash/7e2b41891c5230afc469a8edf8e91066 to your computer and use it in GitHub Desktop.
make enzyme collections lodash compatible
_.mixin({
enzyme: reactWrapper => (
new Proxy(reactWrapper, {
get: (wrapper, prop) => {
const isSymbol = typeof prop === 'symbol';
const isNumber = !isSymbol && !isNaN(Number(prop));
return isNumber ? wrapper.at(prop) : wrapper[prop];
},
})
),
});
const children = mount(<MyComponent/>).find(ChildComponent);
// DOESN'T WORK!
// Its a ReactWrapper object, worse it has a length property which breaks lodash isArrayLike check
const activeChild = _.find(children, (child) => child.props().isActive);
// WORKS!
const activeChild = _.find(_.enzyme(children), (child) => child.props().isActive);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment