.toHaveAttribute('disabled') and .toBeDisabled() #325
-
I am using Button from @material-ui/core, and my test have different output depending of which assert method I use. Initially button is disabled when page loads, and in snapshot there is confirmation about it.
Scenarios:
Mostly i am confused with last output, because disable="" even though it is disabled (i would expect to have value of true). Any thoughts? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Bear in mind that jest-dom works with the regular HTML output of your components, and not with how this is represented in React. There are differences between JSX and HTML. One of these is that boolean attributes in HTML only rely on the attribute's presence to determine if the attribute is true. So, for instance, this JSX: <button disabled={true}>do not click me</button>
<button disabled={false}>click me</button> will result in this DOM structure: <button disabled="">do not click me</button>
<button>click me</button> |
Beta Was this translation helpful? Give feedback.
-
You want to check for the property not the attribute. The property is probably what you're actually interested in unless you need -expect(readyToEsignButton).toHaveAttribute('disabled', 'true');
+expect(readyToEsignButton).toHaveProperty('disabled', true); |
Beta Was this translation helpful? Give feedback.
-
Thank you for the response @eps1lon . It has saved my day. |
Beta Was this translation helpful? Give feedback.
You want to check for the property not the attribute. The property is probably what you're actually interested in unless you need
[disabled="true"]
CSS selectors which could be replaced with:disabled
: