2017-02-15 89 views
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是进口的。我用错了,还是我想要的不可能?

+1

你们每个数字都是这样吗? – Srmuhs

+0

注意:scryRenderedComponentsWithType不适用于反应v0.14中引入的新无状态组件。 – Larrydx

回答

0
  1. 请确保您已导入两个reactreact-dom测试,以及AppNavBarItem组件。如果我们无法看到整个文件,则很难提供帮助。

...如果没有帮助: 您是否尝试过其他方法?像:

scryRenderedDOMComponentsWithClass( component, NavBarItem)