At first, I thought there was something wrong with other aspects of my code. So I created a new, simplified version of the component in a newly created project and wrote the test for it but my spy is still not being called.
Here's the component I'm testing:
import React from 'react';
class TextEditor extends React.Component {
handleChange = (e) => {
console.log({ value: e.target.value });
}
render() {
return (
<div>
<input type="text" name="name" id="name" onChange={this.handleChange} />
</div>
);
}
}
export default TextEditor;
And here is the unit test:
import React from 'react';
import { shallow } from 'enzyme';
import TextEditor from '../TextEditor';
describe('TextEditor', () => {
it('handles change event', () => {
const wrapper = shallow(<TextEditor />);
const spy = jest.spyOn(wrapper.instance(), 'handleChange');
wrapper.find('input').simulate('change', { target: { value: 'test value' }});
expect(spy).toHaveBeenCalledTimes(1);
});
});
The result of running the test:
When I run this, it fails because the spy doesn't get called. But notice that the console.log statement in the handleChange function gets executed. So the test actually calls the function but the spy isn't recognized as having been called.
What could I be doing wrong? Thanks for your ideas.