I am creating a test case using JEST and RTL where I have to perform some action on button click event but I'm not able to fire a click event on that button.
My component looks like:-
const CustomButton = () => {
return (
<div data-testId="main">
<div className="buttonContainer">
// I can't put data-testId here as I'm in a situation where I don't have access to this button.
<button className="button"> Button </button>
</div>
</div>
)
}
My test case:-
it('should do some thing when button is clicked', () => {
render(
<CustomButtom />
)
// I tried accessing the button directly too
fireEvent.click(
screen
.getByTestId('main')
.querySelector('buttonContainer')
?.querySelector('button') as Element
)
// check something
})
The error I'm getting-
Unable to fire a "click" event - please provide a DOM element.