Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get text of the current node only

Tags:

enzyme

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 ?

like image 712
Kevin Avatar asked Feb 04 '17 01:02

Kevin


1 Answers

You could use .childAt()

shallow(component).find('div').childAt(0) // hello

.childAt() will return the child at the index specified

like image 119
Showard180 Avatar answered Nov 06 '22 17:11

Showard180