I have the following code:
return (
<img
ref={imgRef}
src={url}
alt={alt}
onLoad={() => {
if (imgRef.current?.naturalWidth) setIsImgLoaded(true);
}}
style={!isImgLoaded ? { display: 'none' } : {}}
/>
So when the image finishes loading (by checking its naturalWidth), I update the isImgLoaded to true and display it (at the beginning, as you can see, I have display: none set for the image).
In my tests, whenever I try to do:
expect(screen.getByRole('img')).toExist()
I get an error TestingLibraryElementError: Unable to find role="img" , even if I try to use waitFor or findByRole.
What can I do?