2017-02-04 128 views
3

我正在使用airbnb的酶庫進行反應測試,我只想檢索當前節點的文本,排除任何子節點中的任何文本。僅獲取當前節點的文本

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

如果我做的:

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

如果我做的:

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

如何得到公正hello

回答

0

你可以使用.childAt()

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

.childAt()將索引還給孩子指定