I have a React component that generates a button whose content contains a <span> element like this one:
function Click(props) {
return (
<button disable={props.disable}>
<span>Click me</span>
</button>
);
}
I want to test the logic of this component with the use of react-testing-library and mocha + chai.
The problem at which I stuck at the moment is that the getByText("Click me") selector returns the <span> DOM node, but for the tests, I need to check the disable attribute of the <button> node. What is the best practice for handling such test cases? I see a couple of solutions, but all of them sound a little bit off:
- Use
data-test-idfor<button>element - Select one of the ancestors of the
<Click />component and then select the buttonwithin(...)this scope - Click on the selected element with
fireEventand check that nothing has happened
Can you suggest a better approach?
aria-disabled,.toBeDisabled()won't work. The workaround is.toHaveAttribute('aria-disabled', 'true'). See jest-dom #144.