1

Having this component:

import { StyledContainer } from './.styles';
import { MyProps } from './my.types';

export const MyComponent = ({
  titleText
}: TitleProps) => {
  return (
    <StyledContainer>
      <h1>
        {titleText}
      </h1>
    </StyledContainer>
  );
};

that receives as props:

const mockData = { titleText: <div>My title</div> }; // the props can be React.ReactNode

I want to check if the sent props contains div element so this is the test case:

it('should render component', () => {
  render(<MyComponent titleText={mockData.titleText} />);

  const titleText = screen.getByRole('heading', { level: 1 });
  expect(titleText).toBeDefined();
  expect(titleText).toContain('div');
});

The test fails and says:

Expected value:  "div"
Received object: <h1><div>My title</div></h1>

is there a way to check if const titleText contains a div HTML tag?

Leo Messi
  • 5,157
  • 14
  • 63
  • 125
  • This might be helpful: https://stackoverflow.com/questions/54234515/get-by-html-element-with-react-testing-library – Oblosys Nov 21 '22 at 17:50

0 Answers0