I have the following React component
explore enter code here
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>
);
}
}
I am trying to test this using Enzyme, Jest, and Sinon. My unit test looks like the following
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();
});
When I run the code I get the following error
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)
However if I comment out the debounceTime and leave everything else it passes. How do I fix this?