jest-dom icon indicating copy to clipboard operation
jest-dom copied to clipboard

Support buttons with checkbox role for 'toBeRequired'

Open CarlosHdez opened this issue 2 years ago • 6 comments

Describe the feature you'd like:

Hi, I'm using Radix checkboxes https://www.radix-ui.com/docs/primitives/components/checkbox#root, it renders a button with role="checkbox" and supports required by adding aria-required="true" but I can't test it because in here https://github.com/testing-library/jest-dom/blob/main/src/to-be-required.js#L6 does not include either button as a valid form tag or checkbox as a valid role. Not sure if this would be a bug or just a feature requirement

Suggested implementation:

Describe alternatives you've considered:

Teachability, Documentation, Adoption, Migration Strategy:

CarlosHdez avatar Sep 22 '22 17:09 CarlosHdez

I've run into this issue as well using shadcn which is essentially using radix components. Any updates on this one? It would be good to support this.

For now, as a workaround, I used toHaveAttribute matcher and disabled the lint rule:

 // eslint-disable-next-line jest-dom/prefer-required
expect(checkbox).toHaveAttribute('aria-required', 'true');

thewanionly avatar Jan 25 '24 20:01 thewanionly

This is a valid issue.

It seems that jest-dom does not implement this part of what's documented in MDN:

When form controls are created using non-semantic elements, such as a <div> with a role of checkbox, the aria-required attribute should be included, with a value of true, to indicate to assistive technologies that user input is required on the element for the form to be submittable. The aria-required attribute can be used with HTML form elements; it is not limited to elements that have an ARIA role assigned.

gnapse avatar Jan 26 '24 17:01 gnapse

Hey @gnapse, I would like to fix this issue.

I just wanted to confirm whether adding a new function in to-be-required file that checks if the element has aria-required with the value true should do the job, right? Is there anything else that I need to take care of (apart from test)?

PS: This is going to be my first contribution to this repo. So, any help would be appreciated :)

JatinRanka avatar Feb 27 '24 19:02 JatinRanka

I just wanted to confirm whether adding a new function in to-be-required file that checks if the element has aria-required with the value true should do the job, right?

Sounds good. As long as it does not impact the outer interface, of course that internally inside the module you can add helper functions, etc.

Is there anything else that I need to take care of (apart from test)?

Documentation, maybe. We should consider mentioning in the README the new functionality, under the section related to the toBeRequired custom matcher.

This is going to be my first contribution to this repo. So, any help would be appreciated :)

Sure. Give it a go with a PR and we'll take it from there. Feel free to request my review directly.

Looking forward to your first contribution. Thanks for taking the time to do it!

gnapse avatar Mar 04 '24 12:03 gnapse

Hi all, just came across this issue because I'm having a similar problem with my button[role="switch"] Screenshot 2024-03-07 at 11 50 26

adrianaferrugento avatar Mar 07 '24 11:03 adrianaferrugento

@gnapse I'm unable to request your review directly.

Can you please have a look at the PR: https://github.com/testing-library/jest-dom/pull/590

JatinRanka avatar Mar 12 '24 17:03 JatinRanka