【发布时间】:2017-12-07 08:50:09
【问题描述】:
我有以下 React 组件
在此处探索输入代码
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>
);
}
}
我正在尝试使用 Enzyme、Jest 和 Sinon 对此进行测试。我的单元测试如下所示
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 不是函数
在 IncrementalSearch.componentDidMount (src/components/common/incrementalSearch/IncrementalSearch.jsx:37:13) 在 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();期望(间谍).toHaveBeenCalled(); // 或者其中一种方法...要解决实际问题,您可能需要导入或模拟 rxJs
标签: reactjs sinon enzyme jestjs