Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toBeRequired() method #109

Merged
merged 7 commits into from
May 22, 2019
Merged

Conversation

hiwelo
Copy link
Contributor

@hiwelo hiwelo commented May 21, 2019

What:
This pull request is adding .toBeRequired() and .toBeOptional() methods allowing folks to test if a form element is optional or required.

These methods are testing if the form element is having either aria-required or required attributes.
In the case of a conflict between these two attributes, the value of required is having precedence.

Why:

This change is following the discussion within the issue #108.

How:

I added a new file in src/ with the different tests inspired from to-be-disabled.js.
I added the script to src/index.js.
I added a test in src/__tests__/.

Checklist:

  • Documentation
  • Tests
  • Updated Type Definitions
  • Ready to be merged
  • Added myself to contributors table

Copy link
Member

@gnapse gnapse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good. However I do have some questions about the strictness of validating the tag name. See my comments.

Regarding the type definitions, this certainly needs work on that as well. It's simple though, probably just needing to add these couple of lines to extend-expect.d.ts in the root of the repository.

     toBeDisabled(): R
     toBeEnabled(): R
+    toBeOptional(): R
+    toBeRequired(): R
     toContainElement(element: HTMLElement | SVGElement | null): R

src/to-be-required.js Show resolved Hide resolved

function isElementRequiredByARIA(element) {
return (
FORM_TAGS.includes(getTag(element)) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand I suspect that aria-selected is not as restricted, and may be usable even in div elements with certain roles (ref). Though again, I'm not suggesting we go as far as ensuring that if present the element has one of these roles (unless you think is easily doable and makes sense). But at least we should probably not enforce that the element tag is one of these.

Copy link
Contributor Author

@hiwelo hiwelo May 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could enforce that the attribute is present on either:

  • a block having role="combobox", role="gridcell", role="radiogroup", role="spinbutton" or role="tree"
  • a block having or a form element corresponding to the role="listbox" (so any <select>)
  • an block having or a form element corresponding to the role="textbox" (so any <input> and <textarea>)

@hiwelo
Copy link
Contributor Author

hiwelo commented May 22, 2019

Also, I wonder if I will not remove .toBeOptional. It is just a reverse check but if with the full ARIA implementation I am not sure it makes still sense. Because the lack of required or aria-required="true" does not mean it is optional. An form requirement enforcement can still occur server-side.
But .toBeOptional is a good match for the CSS :optional pseudo-class.
What are your thoughts on this @gnapse?

This update follows the discussion in the [PR testing-library#109](testing-library#109).

`.toBeRequired()` now checks:

* the type of input to not return true on unsupported input types
* the roles of a DOM node to not return true on unsupported roles
* a reduced number of supported inputs following the HTML specification
Document these two methods according to the new test scenarii
@hiwelo
Copy link
Contributor Author

hiwelo commented May 22, 2019

@gnapse New commits updating the PR with the requested changes.
I also added the different scenarios in the tests and the documentation.

Also, I checked the code coverage & added the type definition.

Copy link
Member

@gnapse gnapse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good now. I too tend to think as you do, that we can leave toBeOptional out. One can always use .not.toBeRequired. The reason we provided .toBeEnabled is because the word "disabled" is a negation, so .not.toBeDisabled tends to sound like a double negation. But this is not the case with the word "required".

Also, leaving it out now leaves the door open in the future to provide it. Whereas if we provide it now and later decide it was too much, taking it back is harder to do.

Please remove it, and then we can merge.

@hiwelo
Copy link
Contributor Author

hiwelo commented May 22, 2019

@gnapse I just pushed the requested changes. Basically, I removed .toBeOptional from:

  • the initial script file
  • the README.md file
  • the type declaration
  • the jest tests

Please let me know if something else is needed 👍

@hiwelo hiwelo changed the title Add toBeRequired() and toBeOptional() methods Add toBeRequired() method May 22, 2019
Copy link
Member

@gnapse gnapse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Thanks!

@gnapse gnapse merged commit d3f5855 into testing-library:master May 22, 2019
@gnapse
Copy link
Member

gnapse commented May 22, 2019

🎉 This PR is included in version 3.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants