React Unit Testing using @testing-library/react
How to testing element exists using text from element?
Example component code:
import {useState} from 'react';
const Text = ({text}) => {
const [state, setState]= useState(0);
const add = () => {
setState(state+1)
};
return (
<div>
<h1>Hello World</h1>
<h2>Hello {text}</h2>
<h2>Count {state}</h2>
<button role="button" onClick={add}>Increase</button>
</div>
);
};
export default Text;
You can testing your element exist using text from your element. example testing code:
import {
render
} from "@testing-library/react";
test('check button increse exist', ()=> {
const { getByText } = render(<Text />);
const button = getByText("Increase");
expect(button).toBeTruthy()
});