私は以下の反応成分を持っています。これは酵素と害虫を使って単体テストしようとしています。酵素試験は成分を届き、誤差を与えます
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)
これを実行するために必要な追加設定がありますか?
私はここで少し混乱しています、Jestはテストランナーですか?酵素はアサーションライブラリですか?これが正しいと仮定して、私が酵素を使用しているなら、私はチャイの構文の権利を使わなければならないのですか? –
あなたはモカとチャイと共に酵素を使うことができます、あなたはJestを使う必要はありません。 Jestはすべてをまとめてパックし、カバレッジ用にistanbulを追加し、必要な設定をすべて削除します。 Jestはデフォルトで異なるExpectライブラリを持っています。あなたが望むならチャイを使うことができます(私は決してそれを試したとは思えません)。しかし、Jestの期待は十分でなければなりません。 – Patrick
私はdebounceTimeの最初の問題をチェックしました。 OnSearch $は実際に存在しますが、debounceTimeは存在しません。どうすればよいですか?これはSubjectのメソッドではありませんか? –