I have a component which in some circumstances should render nothing (it just returns null). It looks something like this:
const MyComponent = ({ renderNothing }) => {
if (renderNothing) {
return null
}
return <div data-testid='my-component'>Stuff</div>
}
I would like to test that when in fact renderNothing is true, that the element with the data-testid of my-component does not exist. My current test looks something like this:
it.only('should not render MyComponent when renderNothing is true ', async () => {
render(<MyComponent renderNothing={true}/>)
const myComponent = screen.getByTestId('my-component')
expect(myComponent).not.toBeInTheDocument()
});
Unfortunately this test always fails with the following message:
TestingLibraryElementError: Unable to find an element by: [data-testid="my-component"]
Is there a way to successfully do this check without triggering the error?
queryByTestIdtesting-library.com/docs/vue-testing-library/cheatsheet