2017-07-12 5 views
0

私はreact-dom/test-utilsを使用して反応成分をテストします。react-dom/test-utils、TypeError:未定義の 'children'プロパティを読み取れません

しかしscryRenderedDOMComponentsWithClassの戻り値にはprops

props.children性質を持っていません。私は何か間違ったことをやったのですか?ここ

は私のテストコードです:ここ

it('renders item count correctly',() => { 
    const $$searchList = TestUtils.renderIntoDocument(<SearchList items={datas}/>) 
    const list = TestUtils.scryRenderedDOMComponentsWithClass($$searchList, 'list'); 

    expect(list.props.children).toHaveLength(4); 
    expect(items).toHaveLength(4); 

    }); 

は、テスト結果です:

● component - SearchList test suites › renders item count correctly 

    TypeError: Cannot read property 'children' of undefined 

     at Object.<anonymous> (src/components/List/__tests__/SearchList.test.jsx:17:26) 
     at Promise.resolve.then.el (node_modules/p-map/index.js:42:16) 
     at process._tickCallback (internal/process/next_tick.js:109:7) 

    component - SearchList test suites 
    ✓ loads without error (43ms) 
    ✕ renders item count correctly (18ms) 

- アップデート -

ここでは私のコンポーネントによってレンダリングされる:

return (
     <div className="list"> 
     { 
      items.map((item: Book, idx: number): React.ReactElement<IListProps<Book>> => { 
      return (
       <ListItem onClick={() => this.onItemClick(item, idx)} key={item.id} item={item}/> 
      ); 
      }) 
     } 
     </div> 
    ); 

答えて

0

scryRenderedDOMComp onentsWithClassでは、scryRenderedComponentsWithTypeの処理中に、見つかったコンポーネントの小道具をアサートすることはできません。それ以上のことが見つかることがありますhere

対処する前に、0.14、scry/find ... WithClassがDOMコンポーネントを返しました。リリース0.14では、これらのメソッドはDOMノード自体を返します。どちらの場合でも、複合コンポーネントの小道具はアクセス可能ではありません。

また、浅いレンダリングと、サブコンポーネントとその小道具を見つけて、反応テスト用のutils(scryRenderedDOMComponentsWithClass、scryRenderedDOMComponentsWithTag、およびscryRenderedComponentsWithType)の構文を使用するより簡単な構文を持つ酵素を使用することもできます。 あなたは理由と理由を読むことができますabout it here

+0

私は私の質問を更新しますが、まだやり方は分かりません。 – novaline

関連する問題