Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if React component is returning null or its children using React Testing Library?

I have a React component that returns its children to be rendered by React if the prop isTrue is truth-y. If its prop isTrue is false-y, then the component returns null, and React doesn't render anything.

I need to test it as a component, mount it, pass the prop, and test if it's children is getting rendered when the prop isTrue is truth-y, or we are getting null if isTrue is false-y.

Here is my component:

const RenderIf = ({ isTrue, children }) => {
    if (isTrue) {
        return children;
    }
    return null;
}
export default RenderIf
like image 953
Rolando Niubó Avatar asked Aug 30 '25 15:08

Rolando Niubó


1 Answers

const { container } = render(<RenderIf isTrue={false}>Content</RenderIf>)

expect(container).toBeEmptyDOMElement()
like image 129
Karina Perez Avatar answered Sep 02 '25 08:09

Karina Perez