DEV Community

Discussion on: What do you think of React Testing Library?

Collapse
 
kcvgan profile image
kcvgan

So far so good. I had one case however where I was unable to figure out one test case.
I have a button, that button has text. Upon clicking the button, the text gets replaced by a spinner. Thing is, the width of the button should stay the way it was when text was inside and not squeeze to be spinner sized. I did a simple render, put the width of the component in a variable and then fired a click event. I then tried getting the width and seeing if it matched. It was 0 all the time. I’m a bit new to testing but assumed JSDom or JestDom is being used when I render() the component and it would simulate properly. I even tried wrapping the button in a container div with preset dimensions. No luck still. For other unit tests it was a breeze. It really made me rethink how I test my Frontend.

Collapse
 
acostalima profile image
André Costa Lima

I think JSDOM only provides DOM APIs. It isn't a full-blown rendering engine which is required to determine element's visual and structural properties. You'd have to use some visual testing tool to validate the behavior you described.

Collapse
 
kcvgan profile image
kcvgan

That’s what I concluded after tests but really wanted to test this case programmatically. I guess visual testing it has to be then.

Thread Thread
 
thatzacdavis profile image
Zachary Davis

You could check the styles or classes being applied at least and trust that the CSS is doing it's thing.

Thread Thread
 
kentcdodds profile image
Kent C. Dodds

Yep, that's what I'd recommend. You could also use the style prop and verify the node.style.width is correct.

Thread Thread
 
kcvgan profile image
kcvgan

Thanks, will try!

Thread Thread
 
acostalima profile image
André Costa Lima

Would that really work in this case, i.e., when width is not specified by a CSS class?

Thread Thread
 
kentcdodds profile image
Kent C. Dodds

With JSDOM, you can't measure layout, but you can verify that the style/class name was applied and trust that in the browser's ability to lay it out properly based on that.

Thread Thread
 
acostalima profile image
André Costa Lima

Got it! Thanks! 😄