I'm trying to test React components with Enzyme + Sinon spies. My react code uses the property initializer syntax in my component instance:
class FixedButton extends React.Component {
handleMouseOver = (e) => {
const { fixedButtonHover = _fixedButtonHover } = this.props;
const { backgroundColor, color } = fixedButtonHover;
e.target.style.backgroundColor = backgroundColor;
e.target.style.color = color;
}
}
And I'm trying to figure out how to spy on the handleMouseOver function, although from what I understand, since the context is bound to the instance and not a property on the prototype, I cannot spy on it.
I know the function is called and I know I can make the spies work properly by modifying my syntax to standard property style. I also know that the mounted instance of the component has the method as a property.
Is this one of those cases where the two just won't work together, or is there a trick I've failed to see?
Edit: here's how I tried the suggested solution, but the spy.calledOnce returns false as does spy.called:
test('FixedButton component methods are called as expected', t => {
const wrapper = mount(<FixedButton />);
const instance = wrapper.instance();
const spy = sinon.spy(instance, 'handleMouseOver');
const button = wrapper.find('button');
button.simulate('mouseover');
t.is(spy.calledOnce, true);
})