How do I check if a component is not present, i.e. that a specific component has not been rendered?
shallow method is used to render the single component that we are testing. It does not render child components. In Enzyme version less than 3, the shallow method does not have the ability to access lifecycle methods. But in Enzyme version 3, we have this ability.
toBeInTheDocument() matcher, which can be used to assert that an element is in the body of the document, or not. This can be more meaningful than asserting truthfulness in this case. Also it would be better to get the button by role instead of text since it is a preferable way by priority.
Returns a single element with the given testId value for the data-test-id attribute, defaulting to an exact match.
Query the element. The most important thing to note when testing for non-existence is that you'll have to query items. When looking for an element, you might have used getBy or getAllBy and then something. This works fine if we know the element exists, but Jest will throw an error when these are not found.
.contains
receives a React Node or array of Nodes as an argument. Instead, use .find
:
expect(wrapper.find('selector').exists()).toBeTruthy()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With