仅获取当前节点的文本

Kev*_*vin 5 enzyme

我正在使用airbnb的酶库进行反应测试,我只想检索当前节点的文本,但不包括任何子节点中的任何文本。

  const component = <div>hello<span>there</span></div>
Run Code Online (Sandbox Code Playgroud)

如果我做:

  shallow(component).find('div').text()  //hellothere
Run Code Online (Sandbox Code Playgroud)

如果我做:

  shallow(component).find('span').text()  //there
Run Code Online (Sandbox Code Playgroud)

我如何获得公正hello

小智 5

你可以用 .childAt()

shallow(component).find('div').childAt(0) // hello
Run Code Online (Sandbox Code Playgroud)

.childAt() 将返回指定索引处的孩子