Last active
September 4, 2018 13:20
-
-
Save sagiavinash/7e2b41891c5230afc469a8edf8e91066 to your computer and use it in GitHub Desktop.
make enzyme collections lodash compatible
This file contains hidden or 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
_.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