仅获取当前节点的文本

仅获取当前节点的文本

问题描述:

我正在使用airbnb的酶库进行反应测试,我只想检索当前节点的文本,排除任何子节点中的任何文本。仅获取当前节点的文本

const component = <div>hello<span>there</span></div> 

如果我做的:

shallow(component).find('div').text() //hellothere 

如果我做的:

shallow(component).find('span').text() //there 

如何得到公正hello

你可以使用.childAt()

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

.childAt()将索引还给孩子指定