1
我爲一個陣營應用中的以下測試:scryRenderedComponentsWithType返回一個空數組
it('has 7 NavBarItems',() => {
const component = ReactTestUtils.renderIntoDocument(<App />);
const navBarItems = ReactTestUtils.scryRenderedComponentsWithType(
component,
NavBarItem);
expect(navBarItems.length).toEqual(7);
});
當App
組分在非測試環境呈現,用7個NavBarItem子組件的NavBar是在頂部創建的屏幕,以方便導航整個應用程序。
的NavBarItem
類如下:
class NavBarItem extends Component {
render() {
return (
<div id={this.props.id}
className={"NavBarItem" + (this.props.active ? " active" : "") + (this.props.visible ? "" : " hidden")}
onClick={e => this.props.navBarClick(e.target.id)}>
{this.props.title}
</div>
);
}
}
然而,測試總是失敗,因爲scryRenderedComponentsWithType
總是返回一個空數組,甚至當我把添加通話jest.dontMock
兩個App
和文件從NavBarItem
是進口的。我用錯了,還是我想要的不可能?
你們每個數字都是這樣嗎? – Srmuhs
注意:scryRenderedComponentsWithType不適用於反應v0.14中引入的新無狀態組件。 – Larrydx