Testing changes to React component state and spying on instance methods using enzyme
One can test this without relying on sinon
. By expecting that the onLoad
and onFire
event listeners are invoked,the tests check if the img
fires the load
and error
events.
Instead,simulate
img
's events using enzyme
and check that the appropriate state transition occurs:
it('has a state of LOADED if a good src prop is supplied', () => {
const wrapper = shallow(<Image
src="anyString.jpg"
width={300}
height={300}
/>);
const img = wrapper.find('img');
img.simulate('load');
const status = wrapper.state().status;
expect(status).to.equal('LOADED');
});
This also eliminates the need to mount
the component. The updated tests can be found here.