How to test HTML content with React testing library
Asked Answered
S

5

17

Currently I am writing a test to test the content that is inside (HTML content), but it seems I cannot test that properly with React testing library. It can find the id value of that, but how do I get the HTML content inside that element.

import React from 'react';

export const TopBar = () => {
    return (
        <div className="dashboard-title-component">
            <div>
                <div data-testid="title-content">Dashboard Menu</div>
            </div>
        </div>
    )
}

import React from "react";
import { render } from "@testing-library/react";
import { TopBar } from "./TopBar";
import { Provider } from "react-redux";
import { store } from "../../Store";
import { screen } from "@testing-library/dom";
import "@testing-library/jest-dom/extend-expect";

test("It should check if content matches", () => {
    render(
        <Provider store={store}>
            <TopBar/>
        </Provider>
    )
    const checkContent = screen.getAllByTestId("title-content");
    expect(checkContent.text()).toBe("Dashboard Menu");
});

enter image description here

Scholiast answered 2/6, 2020 at 11:38 Comment(0)
T
13

You're using "@testing-library/jest-dom/extend-expect" which provides a set of custom jest matchers that you can use, fore example you have toHaveTextContent(text: string | RegExp, options?: {normalizeWhitespace: boolean}) that you can use here:

const checkContent = screen.getByTestId("title-content");
expect(checkContent).toHaveTextContent("Dashboard Menu");
Tilford answered 2/6, 2020 at 11:53 Comment(0)
C
4

It is possible to also test whole HTML node structure this way:

const checkContent = screen.getByTestId("title-content");
expect(checkContent.outerHTML)
    .toEqual("<div data-testid=\"title-content\">Dashboard Menu</div>");

This is using standard web API Element.outerHTML

Contradict answered 28/8, 2020 at 12:41 Comment(0)
C
1

Use getByText

test("It should check if content matches", () => {
  const { getByText } = render(<Provider store={store}><TopBar /></Provider>)
  expect(getByText(/dashboard menu/i)).toBeTruthy();
});
Choreographer answered 2/6, 2020 at 11:56 Comment(0)
H
0

You can use within to get the text Dashboard Menu. Try this:

test("It should check if content matches", () => {
    const { getByTestId } = render(
        <Provider store={store}>
            <TopBar/>
        </Provider>
    )
    const { getByText } = within(getByTestId('title-content'))
    expect(getByText('Dashboard Menu')).toBeInTheDocument()
});
Harding answered 2/6, 2020 at 11:48 Comment(1)
The RTL team recommends using screen instead. screen.getByTestId vs destructuring { getByTestId } from render.Memorabilia
A
0

if you need to have access to the type of HTML element itself, in addition to the contents, you may need to use getByText method with a callback func.

test("It should check if content matches", () => {
  render(<App />);

  const checkContent = screen.getByText((content, element) => {
      return element.tag.toLowerCase() === 'div' &&  content.includes('Dashboard Menu')
  });

    expect(checkContent).toBeInTheDocument();
});
Anabolism answered 9/4, 2023 at 0:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.