我有以下時作出反應與debounceTime錯誤成分反應成分使用興農
探索在此輸入代碼
class IncrementalSearch extends React.Component {
constructor(props) {
super(props);
this.onSearch$ = new Subject();
this.onChange = this.onChange.bind(this);
}
componentDidMount() {
console.log(this.simpleText);
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>
);
}
}
我試圖測試這個使用酶,玩笑,和興農。我的單元測試看起來如下
it('calls componen`enter code here`tDidMount',() => {
const componentDidMountSpy = sinon.spy(IncrementalSearch.prototype, 'componentDidMount');
const wrapper = mount(<IncrementalSearch />);
expect(IncrementalSearch.prototype.componentDidMount.calledOnce).toEqual(true);
componentDidMountSpy.restore();
});
當我運行代碼,我得到以下錯誤
TypeError: this.onSearch$.debounceTime is not a function
at IncrementalSearch.componentDidMount (src/components/common/incrementalSearch/IncrementalSearch.jsx:37:13) at Function.invoke (node_modules/sinon/lib/sinon/spy.js:194:51)
但是如果我註釋掉debounceTime和其他東西不要通過。我該如何解決?
是否有使用Sinon窺探被調用方法的具體原因? Jest有自己的spyOn方法: const spy = jest.spyOn(IncrementalSearch.prototype,'componentDidMount'); const wrapper = mount( ); expect(spy).toHaveBeenCalled(); //或者其中一種方法... 修復您可能需要導入的實際問題或模擬rxJs –
Grandas