2017-06-30 16 views
2

私は以下の反応成分を持っています。これは酵素と害虫を使って単体テストしようとしています。酵素試験は成分を届き、誤差を与えます

import React from 'react'; 
import { Subject } from 'rxjs/Subject'; 
import { connect } from 'react-redux'; 
import styles from './IncrementalSearch.scss'; 
import { performIncrementalStoreSearch } from './IncrementalSearchActions'; 

export class IncrementalSearch extends React.Component { 

constructor(props) { 
    console.log('constructor'); 
    super(props); 
    this.onSearch$ = new Subject(); 
    this.onChange = this.onChange.bind(this); 
} 

componentDidMount() { 
    this.subscription = this.onSearch$ 
     .debounceTime(300) 
     .subscribe(debounced => { 
      this.props.onPerformIncrementalSearch(debounced); 
     }); 
} 


componentWillUnmount() { 
    if (this.subscription) { 
     this.subscription.unsubscribe(); 
    } 
} 
onChange(e) { 
    const newText = e.target.value; 
    this.onSearch$.next(newText); 
} 

render() { 
    return (
     <div className={styles.srchBoxContaner}> 
      <input 
       className={styles.incSrchTextBox} 
       type="text" name="search" placeholder="Search.." 
       onChange={this.onChange} 
      /> 
     </div> 
    ); 
} 
} 

const mapDispatchToProps = (dispatch) => ({ 
    onPerformIncrementalSearch: (searchText) => { 
     dispatch(performIncrementalStoreSearch(searchText)); 
    } 
}); 

const IncrementalSearchComponent = connect(null, mapDispatchToProps)(IncrementalSearch); 
export default IncrementalSearchComponent; 

私のユニットテストは、私はユニットテストを実行したとき、私はcomponentDidMountですべてをコメントアウトした場合

import React from 'react'; 
import { mount } from 'enzyme'; 
import sinon from 'sinon'; 
import { IncrementalSearch } from './IncrementalSearch'; 

describe('<IncrementalSearch />',() => { 
    it('calls componentDidMount',() => { 
     sinon.spy(IncrementalSearch.prototype, 'componentDidMount'); 
     const wrapper = mount(<IncrementalSearch />); 
     expect(IncrementalSearch.prototype.constructor.calledOnce).to.equal(true); 
    }); 
}); 

は、しかし、私は次のエラー

TypeError: Cannot read property 'debounceTime' of undefined 

を取得する次のように私が手であります次のエラー

<IncrementalSearch /> › calls componentDidMount 

    TypeError: Cannot read property 'equal' of undefined 

     at Object.<anonymous> (src/components/common/incrementalSearch/IncrementalSearch.test.js:10:89) 
     at Promise.resolve.then.el (node_modules/p-map/index.js:42:16) 
     at process._tickCallback (internal/process/next_tick.js:103:7) 

    <IncrementalSearch /> 
    × calls componentDidMount (29ms) 

これを実行するために必要な追加設定がありますか?

答えて

0

私はあなたの問題は2倍だと思います。

まず、onSearch $が実際に存在し、テストで期待していることを確認します。私は何かがそこで正しく定義されていないという気持ちがあります。

第2号に関して、これは酵素を使用するときに人がする間違いです。そうexpect(...).toが故に、未定義である
http://chaijs.com/api/bdd/
https://facebook.github.io/jest/docs/expect.html

expect(...).to.be(...)は冗談が異なる期待図書館に

を使用していますあなたがここに違いを見ることができる、(チャイです)酵素で使用される構文ですto.equalは、未定義でequalを呼び出しています。それをexpect(...).toEqual(...)に変更してください。

+0

私はここで少し混乱しています、Jestはテストランナーですか?酵素はアサーションライブラリですか?これが正しいと仮定して、私が酵素を使用しているなら、私はチャイの構文の権利を使わなければならないのですか? –

+0

あなたはモカとチャイと共に酵素を使うことができます、あなたはJestを使う必要はありません。 Jestはすべてをまとめてパックし、カバレッジ用にistanbulを追加し、必要な設定をすべて削除します。 Jestはデフォルトで異なるExpectライブラリを持っています。あなたが望むならチャイを使うことができます(私は決してそれを試したとは思えません)。しかし、Jestの期待は十分でなければなりません。 – Patrick

+0

私はdebounceTimeの最初の問題をチェックしました。 OnSearch $は実際に存在しますが、debounceTimeは存在しません。どうすればよいですか?これはSubjectのメソッドではありませんか? –

関連する問題