I'm trying to test that a function callback is being called for an image inside my component.
This is the component:
const ImageById = (props) => {
return (
<div>
<img src={props.url} onLoad={props.onLoad} onError={props.onError} />
</div>
);
};
And my test mounts the component and then spy the callback function:
describe('ImageById', () => {
it('should call load or error', () => {
let callbackSpy = sinon.spy();
let comp = mount(ImageById, {url: 'some-url', onLoad: callbackSpy, onError: callbackSpy});
expect(callbackSpy.called).to.equal(true);
});
});
The test fails because the inner Img tag is never calling its onload nor onerror methods. In production the code is working fine, it might be something related to the test environment. Its like Img tag doesn't react to the url being set.