简体   繁体   中英

react tests- display:none in stylesheet doesn't work in but works when inline

I am testing if a div is set to display: none . I have set it as such in the stylesheet but the tests tell me that it is still display: block . When I change to inline CSS the tests acknowledge that it is indeed display: none .

and Yes, I did remember to import the stylesheet because the element is invisible in the browser.

Part of the react component

      <div className="navbar-buttons">
        <button type="button">Sign In</button>
      </div>

stylesheet

.navbar-buttons{
  display: none;
}

The test assertion

   expect(signinBtns).toHaveStyle('display: none')

The test result printed to terminal

    expect(element).toHaveStyle()

    - Expected

    - display: none;
    + display: block;

How do I get the tests to recognize the style attributes if they are in a stylesheet?

You can't check if the style attributes are applied via tests, unless it's inline style. You can only check for the class name if it's applied or not and assume it's the right class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM