I'm using airbnb's enzyme library for react testing and I want to retrieve the text of the current node only, excluding any text from any child nodes.
const component = <div>hello<span>there</span></div>
If I do:
shallow(component).find('div').text() //hellothere
If I do:
shallow(component).find('span').text() //there
How do I get just hello
?
You could use .childAt()
shallow(component).find('div').childAt(0) // hello
.childAt()
will return the child at the index specified
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With