2017-02-15 5 views
1

を返す:App成分は非試験環境でレンダリングされるときscryRenderedComponentsWithType私が反応アプリケーションのために以下の試験を持って空の配列

it('has 7 NavBarItems',() => { 
    const component = ReactTestUtils.renderIntoDocument(<App />); 
    const navBarItems = ReactTestUtils.scryRenderedComponentsWithType(
     component, 
     NavBarItem); 

    expect(navBarItems.length).toEqual(7); 
}); 

、7つのNavBarItem子成分とナビゲーションバーは、最上部に作成されアプリ全体のナビゲーションを容易にすることができます。

次のように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はいつも私がAppとからNavBarItemファイルの両方のためにjest.dontMockに呼び出しを追加置くでも空の配列を返すので、インポートされます。私はそれを間違って使っていますか?

+1

あなたはこれをすべて理解しましたか? – Srmuhs

+0

注:scryRenderedComponentsWithTypeは、反応v0.14で導入された新しいステートレスコンポーネントでは機能しません。 – Larrydx

答えて

0
  1. あなたがテストにreactreact-domの両方をインポートし、同様にAppNavBarItemコンポーネントれていることを確認します。ファイル全体が見えない場合は、助けがさらに難しくなります。

...それが役に立たなかった場合: 他の方法を試しましたか? Like:

scryRenderedDOMComponentsWithClass( component, NavBarItem)

関連する問題