Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest + Enzyme - test for component visibility to the user

Tags:

jestjs

enzyme

Currently we use this to test whether a component has ben rendered:


const someComponent = component.find('[data-test="some-component"]').at(0);
expect(someComponent.length).toBe(1);

This is fine, but it isn't actually testing that the component is visible to the user - it simply tests that the component exists. How to test that a component both exists and is visible to the user?

like image 833
JoeTidee Avatar asked Mar 27 '19 17:03

JoeTidee


Video Answer


1 Answers

Check out https://github.com/testing-library/jest-dom which offers a variety of custom DOM matchers for Jest, including toBeVisible().

With it you could write a test like this, which should work:

const someComponent = component.find('[data-test="some-component"]').at(0);
expect(someComponent.getDOMNode()).toBeVisible();

Edit: though the title of OP's post says "Jest + Enzyme" I should clarify that getDomNode() is an Enzyme function, so the above won't work if you're only using Jest. Here's the docs page for using Enzyme with Jest.

like image 173
runofthemill Avatar answered Oct 19 '22 04:10

runofthemill