DEV Community

Discussion on: Understanding React Testing Library

dhaiwat10 profile image
Dhaiwat Pandya Author

Thanks for the reply!

I think we are on different pages here. I don't have any loading state in my component like you mentioned. By default, Testing Library doesn't actually load your image. It doesn't fire the 'onLoad' event. (That's what we are doing with fireEvent.load(), not updating any internal state.) Which is why you can test if the image is correctly displaying a loader using the code I mentioned.

Thread Thread
dhaiwat10 profile image
Dhaiwat Pandya Author

And all of these events are native, none are custom. 😁

Thread Thread
lukeshiru profile image
LUKESHIRU

That's what I'm saying 😅 ... why would you test if the native img onLoad event works? Or if it changes something? If you do that, it seems like the component has a side effect when the image is done loading (which you'll emulate from RTL with fireEvent.load). Ideally the component shouldn't do that by itself, but actually receive it from the parent (which is easier to test, because you don't need that fireEvent at all).