Michael M. Adkins
Michael M. Adkins

Reputation: 175

Why is this is undefined when mocking event in React unit testing, using sinon.spy to mock function and enzyme.shallow render for React Component?

Whenever I try to simulate an event where I am passing the event object, I am getting that this is undefined.

const onBlur = sinon.spy(Input.prototype.handleBlur);
const wrapper = shallow(<Input handleBlur={onBlur} />);
//...
wrapper.find('input').simulate('blur', { target: {value: ''} });

Inside of the handleBlur method, this is undefined. However, if I decide not to mock the event object, then event is undefined.

Also, I tried using mount instead: const wrapper = shallow(<Input handleBlur={onBlur} />); But, it's the same thing. However, I couldn't even get to that point until I updated the npm libraries.

Upvotes: 1

Views: 275

Answers (1)

user1705722
user1705722

Reputation: 19

try using

const onBlur = sinon.spy(Input.prototype, "handleBlur");

instead

const onBlur = sinon.spy(Input.prototype.handleBlur);

Upvotes: 1

Related Questions