Search code examples
reactjsreact-testing-libraryreact-test-renderer

How to Unit test the rendering of a child component in the parent component using react testing library?


I cannot post the original code here ,so i will try to explain what i want to achieve. So,my component displays message.This message is part of a child component which is being rendered in the parent component.So,i want to unit test this logic.But i don't know how to write the unit test for rendering of child component in react testing library.Example code:

function Home(props){
const message=props;
if(message==='warning')
return <Warning display={message}/>

So i want to write unit test to achieve this rendering of warning component in my parent component using react-testing-library.


Solution

  • I can't see your code so I'm kind of guessing but you need something like this:

    const props = { message: 'warning'}
    
    const { container, getByText } = render(
        <Home {...props} />,
    )
    
    expect(getByText('warning')).toBeInTheDocument()
    

    let me know if that works