Home > OS >  How to write unit test for button component title using material ui in react
How to write unit test for button component title using material ui in react

Time:12-31

I'm beginner to unit testing, i'm trying to test name for button component using material ui framework.

import Button from '@mui/material/Button';
import { screen } from '@testing-library/react';
import {render} from 'react-dom';

test("display button title", () => {
render(<Button/>);
const button = screen.getByRole('button', {name:/create user/i});
expect(button).toBeInTheDocument();
})

Can anyone help me in this. It is showing failed. I don't know where i'm writing wrong.

I'm expecting that the button component name should pass

CodePudding user response:

Perhaps using the findBy query from react testing library can help. This will allow any rendering state updates, etc., to finish before any assertion is made.

test("display button title", async () => {
 render(<Button/>);
 const button = await screen.findByRole('button', {name:/create user/i});
 expect(button).toBeInTheDocument();
})
  • Related