You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
add aria-disabled in the toBeDisabled matcherΒ #144
i have custom buttons (SVG's with click handlers) which i want to ensure are disabled under certain circumstances. Since these elements have disabled styling I've been using the aria-disabled attrubtue for this purpose (two birds one stone)
so i would like to be able to test as expect(myCustomSvgButton).toBeDisabled()
Describe alternatives you've considered:
obiviously this is doable just now using .toHaveAttribute but i feel like this reduces the clarity of what's being checked and focuses on implementation details making the test brittle.
on a side note, maybe encouraging the use of aria-* attrs for communication of more complex state in the DOM is not a bad thing?
Reacted by Ernesto GarcΓa, Alex Krolick, Daniel G., Andrew Chung, sanchopancho13, Jordan Thomson, Natalia Goska, Angelos Koufakis, bozdoz, Maciej Kubiak and 10 more
I wasn't aware of this issue so I want to summarize some concerns that I raised when the implementation was proposed:
The aria-disabledattribute (proposed) and the disabledproperty (current) assert different things with a very small overlap. I do not know everything disabled implies but of the top of my hat the following are implicitly expected when disabled:
not focusable
not clickable
certain events not firing (e.g. mouseEnter)
implicit aria-disabled="true"
So while I do see the usefulness of an toBeAriaDisabled matcher (either having aria-disabled="true" or disabled) I do not think it is a good idea to check for aria-disabled in toBeDisabled.
Reacted by Ernesto GarcΓa, adyharold, Laura Chan and Leo Weigand
Reacted by bozdoz, Walker Boyle, cjenkscybercom, Ε½ilvinas, Phebe S, Dmitri Pavlutin, Robert Komaromi and David Habl
@eps1lon thanks for the input, i suppose i was thinking (maybe incorrectly) that this would be inline with the testing-library philosophy, that tests shouldn't necessary be testing implementation details (which exact attributes has a value) but should be testing what a user observes as the behaviour.
for the latter case, to a screenreader client disabled & aria-dissabled would indicate the same thing ?
however for my own self interest, would there be a better way to indicate disabled SVG buttons/Clickable elements that would be testable using toBeDissabled ?
Reacted by bozdoz, Ε½ilvinas, Phebe S, Keegan Good, Paul Bill, David Habl and Paulius Varna
for the latter case, to a screenreader client disabled & aria-dissabled would indicate the same thing ?
Partially. But you're not matching toBeAriaDisabled but toBeDisabled. The latter one implies a variety of other UX properties that are not implied by aria-disabled.
So to summarize:
toBeAriaDisabled matches aria-disabled="true" or disabled
toBeDisabled matches the disabled property
If you want that toBeDisabled also matches aria-disabled="true" you have to match all the other UX behaviors implied by disabled.
Also want to address
that tests shouldn't necessary be testing implementation details
checking that a disabled element is not focusable is not an implementation detail. This is very much observable both by users with and without assistive technology.
Reacted by Ernesto GarcΓa, Ruben Swieringa and Leo Weigand
Reacted by bozdoz, Sasha Chudesnov, cjenkscybercom and Ε½ilvinas
So to summarize, @eps1lon, what you're proposing is that we leave toBeDisabled as is, and we instead add a new .toBeAriaDisabled custom matcher. Is that it?
If .toBeAriaDisabled is added then should we add .toBeAriaHidden or .toBeAriaExpanded or .toBeAria*?
If you want that toBeDisabled also matches aria-disabled="true" you have to match all the other UX behaviors implied by disabled.
It doesn't look like .toBeInvalid is matching all other UX behaviours implied by invalid. It looks like .toBeInvalid checks for aria-invalid="true" and toBeChecked checks for aria-checked="true" so why would .toBeDisabled work differently?
I think it makes more sense to add the aria-disabled check to .toBeDisabled so that's it's intuitive to the user and is consistent with the other matchers.
If .toBeAriaDisabled is added then should we add .toBeAriaHidden or .toBeAriaExpanded or .toBeAria*?
Yep although I would make sure there aren't other libraries out there that already reflect aria properties.
I think it makes more sense to add the aria-disabled check to .toBeDisabled so that's it's intuitive to the user and is consistent with the other matchers.
If toBeInvalid has similar false assumptions then we should fix this instead of applying this bug everywhere else.
It does feel like toBeAriaDisabled is implying the underlying implementation, I'm not sure how you would convey that the extra recommended UX behaviours have been met by just by using a different matcher. Unfortunately the aria spec is a bit light on the details of the 'UX behaviours' associated with aria-disabled so I'm not sure what toBeAriaDisabled will give you that toBeDisabled won't.
Also wouldn't the user of jest-dom now have to make a decision on which matcher to use based on their implementation? I guess another solution would be that toBeDisabled also asserts all the behaviours associated with an element being disabled, regardless of aria-disabled="true" or disabled? Or jest-dom makes no assumption on the behaviour of a disabled element and only asserts that the correct attributes have been applied?
Indicates that the element is perceivable but disabled, so it is not editable or otherwise operable. See related aria-hidden and aria-readonly.
For example, irrelevant options in a radio group may be disabled. Disabled elements might not receive focus from the tab order. For some disabled elements, applications might choose not to support navigation to descendants. In addition to setting the aria-disabled attribute, authors SHOULD change the appearance (grayed out, etc.) to indicate that the item has been disabled.
The state of being disabled applies to the current element and all focusable descendant elements of the element on which the aria-disabled attribute is applied.
Reacted by Ernesto GarcΓa, Carlos and Steven Cochrane
Also wouldn't the user of jest-dom now have to make a decision on which matcher to use based on their implementation?
Yes. I can't stress this enough: Using aria-* attributes is not an implementation detail. They are very much observable by the user of your website. There is a big difference between <button disabled /> and <button aria-disabled="true" /> so your test should reflect that. When we talk about implementation details we usually mean the details of your renderer e.g. react or vue or svelte. For some APIs of the DOM this is also true but we currently don't mimic a browser with the testing-library (and doing so would be out of scope).
aria-* attributes (as the spec even words it) are only indicators for assistive technology. These map these attributes and the original DOM attributes/properties to their accessibility API which then puts this out as speech or receivable commands to the user. With aria-* attributes you communicate with assistive technology. With the other attributes you communicate with assistive technology and users not using assistive technology. Even if you are writing software only usable by assistive technology then both matchers then these matchers wouldn't do the same. <button aria-disabled /> is still focusable (which is perfectly valid) while <button disabled /> is not. This is pretty significant for users of assistive technology as well.
I understand that it is tempting to short-circuit writing tests. If you have considered all the implications of considering aria-disabled and disabled equal then you can always use custom matchers in your code base.
Reacted by haworku, gabrielmaschke, Thiago Brezinski, Alejandro Franco, Keegan Good, Leo Weigand, Tyler Mumford and Adam Stankiewicz
Yep that makes perfect sense, thanks for the detailed explanation! π
If toBeInvalid has similar false assumptions then we should fix this instead of applying this bug everywhere else.
I know I added aria-checked into toBeChecked, so that will also need to be split out into 2 matchers. I'm happy to dig into this and put in some PRs for that and other matchers, if that's alright?
I know I added aria-checked into toBeChecked, so that will also need to be split out into 2 matchers. I'm happy to dig into this and put in some PRs for that and other matchers, if that's alright?
Sure. It's mostly digging through specifications though I'd imagine. I think the biggest difference is that something like checked has an impact when submitting a form to the server. aria-checked doesn't
So as an attempt to come to a bit of a conclusion (maybe only for myself)
There is a big difference between <button disabled /> and <button aria-disabled="true" /> so your test should reflect that. ... we currently don't mimic a browser with the testing-library
As a user (and fan) of this library & the testing-library ecosystem as a whole, i did think that "mimic a browser" was the intent. I was under the impression (possibly incorrectly) that the philosophy behind the testing-library ecosystem was to encourage tests that mimic what a user would see and do. So following that logic i thought rolling these two together would be a sensible thing.
This conversation has been really useful in showing that this thinking wasn't quite accurate, so instead would it be acceptable to add a new toBeAriaDissabled which would do the same matching as toBeDissabled but also match elements with aria-disabled="true" ?
So following that logic i thought rolling these two together would be a sensible thing.
I seem to have failed communicating that doing that would be the exact opposite. If we would do that someone would need to put time and effort into compiling what .disabled implies in the browser. Just matching the aria-disabled attribute would not be sufficient. I'm not opposed to doing that but I just wanted to make it clear that this isn't something you're done with in 30 minutes or handwave why you added some expectations.
This conversation has been really useful in showing that this thinking wasn't quite accurate, so instead would it be acceptable to add a new toBeAriaDissabled which would do the same matching as toBeDissabled but also match elements with aria-disabled="true" ?
This sounds perfectly fine. This way you can't accidentally introduce (potentially ) breaking changes by switching from disabled to aria-disabled. I think the important part here is documenting that this only asserts what assistive technology announces when reading that element and explain disabled vs aria-dsiabled and focus, events etc.
@eps1lon sorry, I've been reading back right to my initial query, i might have confused matters by not being very clear originally. (if not just ignore this message π ) My proposal was never to remove any of the existing behaviour / matching semantics of the current toBeDisabled, only to add to them so that all the following examples would match:
It is not the original topic, but very similar issue. What is the recommendation for .toBeVisible() but for aria-hidden? I have a carousel with copied fake slides (for infinite scrolling) and I use aria-hidden in them.
See that the slidesDOM array are not the .slide elements, but their span children. The tests rely on their parents being visible/not visible, so I cannot use the .toHaveAttribute('aria-hidden', "true") check.
Even though they are rendered in the dom nodes, they are not visible for most users. They are only visible if they scroll very far in a single swipe, after which they remove the hidden attribute.
I can't use the hidden attribute in them. The parents should be visible in the case of a long swipe, but I can't test that they are hidden for screen readers users.
@andre-matulionis-ifood we may need a custom matcher for that because the semantics of aria-hidden are fundamentally different than that of hidden. Can you convert that comment of yours into a new issue?
I don't think we're going to do this (adding support for the presence of aria-disabled to toBeDisabled). The semantics are different, as discussed above. We're probably not going to add a toBeAriaDisabled either. The workaround is .toHaveAttribute('aria-disabled', 'true').
Good luck!
Reacted by Connor Adams, Lewis, Distributed-Manufacturing, John Tabone, Richard Young, datguyducky, Dmitriy Gavrilov, Dimitri Steinel, marco-beduschi, Mireia Bru and 38 more
Reacted by Jordan Thomson, Dario Lipartiti, Kay Schwieger, bozdoz, Priscila Oliveira, Walker Boyle, Eduardo MacΓͺdo, Ε½ilvinas, Marcio Caraballo, Jason and 3 more
The workaround is .toHaveAttribute('aria-disabled', 'true').
Isn't this testing for the explicit attribute? Whereas the proposal above should return true whether it has has aria-disabled or disabled (because the latter implies aria-disabled).
Surely this is exactly the use case for the convenience methods such as .toBeDisabled()? If not, then all the above arguments should have already been applied and .toBeDisabled() would never have been added. If you explicitly want something with the disabled attribute, the check is .toHaveAttribute('disabled'), and this is literally the first example in the docs for that method: https://github.com/testing-library/jest-dom#tohaveattribute
the proposal above should return true whether it has has aria-disabled or disabled (because the latter implies aria-disabled).
The problem with aria-disabled is that it does not really tell you that the element is disabled. For instance, having a button like this:
<button aria-disabled="true">
Click me
</button>
That button is still clickable and focusable. You can reach it when tabbing around the page, and click it with the mouse and with the keyboard. So having .toBeDisabled() pass when it has aria-disabled="true" and nothing else is misleading.
then all the above arguments should have already been applied and .toBeDisabled() would never have been added. If you explicitly want something with the disabled attribute, the check is .toHaveAttribute('disabled'),
This is not so simple. The toBeDisabled() custom matcher still does more than that. An element is also disabled if it's inside a parent form or fieldset that is disabled. So even if an element does not have the disabled attribute, the custom matcher may still pass the test.
For instance:
<fieldset disabled="true">
<button>
Click me
</button>
</fieldset>
Activity
[-]add aria-disb[/-][+]add aria-disabled in the `tobedisabled` matcher[/+]eps1lon commentedon Oct 31, 2019
I wasn't aware of this issue so I want to summarize some concerns that I raised when the implementation was proposed:
The
aria-disabledattribute (proposed) and thedisabledproperty (current) assert different things with a very small overlap. I do not know everythingdisabledimplies but of the top of my hat the following are implicitly expected whendisabled:aria-disabled="true"So while I do see the usefulness of an
toBeAriaDisabledmatcher (either havingaria-disabled="true"ordisabled) I do not think it is a good idea to check foraria-disabledintoBeDisabled.chrismatheson commentedon Nov 1, 2019
@eps1lon thanks for the input, i suppose i was thinking (maybe incorrectly) that this would be inline with the testing-library philosophy, that tests shouldn't necessary be testing implementation details (which exact attributes has a value) but should be testing what a user observes as the behaviour.
for the latter case, to a screenreader client
disabled&aria-dissabledwould indicate the same thing ?however for my own self interest, would there be a better way to indicate disabled SVG buttons/Clickable elements that would be testable using
toBeDissabled?eps1lon commentedon Nov 1, 2019
Partially. But you're not matching
toBeAriaDisabledbuttoBeDisabled. The latter one implies a variety of other UX properties that are not implied byaria-disabled.So to summarize:
toBeAriaDisabledmatchesaria-disabled="true"ordisabledtoBeDisabledmatches thedisabledpropertyIf you want that
toBeDisabledalso matchesaria-disabled="true"you have to match all the other UX behaviors implied bydisabled.Also want to address
checking that a disabled element is not focusable is not an implementation detail. This is very much observable both by users with and without assistive technology.
gnapse commentedon Nov 1, 2019
So to summarize, @eps1lon, what you're proposing is that we leave
toBeDisabledas is, and we instead add a new.toBeAriaDisabledcustom matcher. Is that it?eps1lon commentedon Nov 1, 2019
@gnapse π That seems like it would adress the original issue and be safe for UX testing.
connormeredith commentedon Nov 1, 2019
If
.toBeAriaDisabledis added then should we add.toBeAriaHiddenor.toBeAriaExpandedor.toBeAria*?It doesn't look like
.toBeInvalidis matching all other UX behaviours implied by invalid. It looks like.toBeInvalidchecks foraria-invalid="true"andtoBeCheckedchecks foraria-checked="true"so why would.toBeDisabledwork differently?I think it makes more sense to add the
aria-disabledcheck to.toBeDisabledso that's it's intuitive to the user and is consistent with the other matchers.eps1lon commentedon Nov 1, 2019
Yep although I would make sure there aren't other libraries out there that already reflect aria properties.
If
toBeInvalidhas similar false assumptions then we should fix this instead of applying this bug everywhere else.connormeredith commentedon Nov 1, 2019
It does feel like
toBeAriaDisabledis implying the underlying implementation, I'm not sure how you would convey that the extra recommended UX behaviours have been met by just by using a different matcher. Unfortunately the aria spec is a bit light on the details of the 'UX behaviours' associated witharia-disabledso I'm not sure whattoBeAriaDisabledwill give you thattoBeDisabledwon't.Also wouldn't the user of
jest-domnow have to make a decision on which matcher to use based on their implementation? I guess another solution would be thattoBeDisabledalso asserts all the behaviours associated with an element being disabled, regardless ofaria-disabled="true"ordisabled? Orjest-dommakes no assumption on the behaviour of a disabled element and only asserts that the correct attributes have been applied?https://www.w3.org/TR/wai-aria-1.1/#aria-disabled
eps1lon commentedon Nov 1, 2019
Yes. I can't stress this enough: Using aria-* attributes is not an implementation detail. They are very much observable by the user of your website. There is a big difference between
<button disabled />and<button aria-disabled="true" />so your test should reflect that. When we talk about implementation details we usually mean the details of your renderer e.g. react or vue or svelte. For some APIs of the DOM this is also true but we currently don't mimic a browser with the testing-library (and doing so would be out of scope).aria-*attributes (as the spec even words it) are only indicators for assistive technology. These map these attributes and the original DOM attributes/properties to their accessibility API which then puts this out as speech or receivable commands to the user. Witharia-*attributes you communicate with assistive technology. With the other attributes you communicate with assistive technology and users not using assistive technology. Even if you are writing software only usable by assistive technology then both matchers then these matchers wouldn't do the same.<button aria-disabled />is still focusable (which is perfectly valid) while<button disabled />is not. This is pretty significant for users of assistive technology as well.I understand that it is tempting to short-circuit writing tests. If you have considered all the implications of considering aria-disabled and disabled equal then you can always use custom matchers in your code base.
connormeredith commentedon Nov 1, 2019
Yep that makes perfect sense, thanks for the detailed explanation! π
I know I added
aria-checkedintotoBeChecked, so that will also need to be split out into 2 matchers. I'm happy to dig into this and put in some PRs for that and other matchers, if that's alright?eps1lon commentedon Nov 1, 2019
Sure. It's mostly digging through specifications though I'd imagine. I think the biggest difference is that something like
checkedhas an impact when submitting a form to the server.aria-checkeddoesn't4 remaining items
chrismatheson commentedon Nov 5, 2019
So as an attempt to come to a bit of a conclusion (maybe only for myself)
As a user (and fan) of this library & the testing-library ecosystem as a whole, i did think that "mimic a browser" was the intent. I was under the impression (possibly incorrectly) that the philosophy behind the testing-library ecosystem was to encourage tests that mimic what a user would see and do. So following that logic i thought rolling these two together would be a sensible thing.
This conversation has been really useful in showing that this thinking wasn't quite accurate, so instead would it be acceptable to add a new
toBeAriaDissabledwhich would do the same matching astoBeDissabledbut also match elements witharia-disabled="true"?eps1lon commentedon Nov 5, 2019
I seem to have failed communicating that doing that would be the exact opposite. If we would do that someone would need to put time and effort into compiling what
.disabledimplies in the browser. Just matching thearia-disabledattribute would not be sufficient. I'm not opposed to doing that but I just wanted to make it clear that this isn't something you're done with in 30 minutes or handwave why you added some expectations.This sounds perfectly fine. This way you can't accidentally introduce (potentially ) breaking changes by switching from
disabledtoaria-disabled. I think the important part here is documenting that this only asserts what assistive technology announces when reading that element and explaindisabledvsaria-dsiabledand focus, events etc.chrismatheson commentedon Nov 5, 2019
@eps1lon sorry, I've been reading back right to my initial query, i might have confused matters by not being very clear originally. (if not just ignore this message π ) My proposal was never to remove any of the existing behaviour / matching semantics of the current
toBeDisabled, only to add to them so that all the following examples would match:<input disabled/><input aria-disabled="true"/><input disabled aria-disabled="true"/><div disabled="true"/><div aria-disabled="true"/><div disabled aria-disabled="true"/><form disabled/><form aria-disabled="true"/><form disabled aria-disabled="true"/>obviously there would be the potential for ambiguous
<form disabled aria-disabled="false"/><input disabled="false" aria-disabled="true"/><input disabled aria-disabled="false"/>which could be detected and throw an error or some description explaining the ambiguity.
this is exactly what id propose goes into a new
toBeAriaDissabled[-]add aria-disabled in the `tobedisabled` matcher[/-][+]add aria-disabled in the `toBeDisabled` matcher[/+]andre-matulionis-ifood commentedon Nov 8, 2019
It is not the original topic, but very similar issue. What is the recommendation for
.toBeVisible()but for aria-hidden? I have a carousel with copied fake slides (for infinite scrolling) and I usearia-hiddenin them.See that the
slidesDOMarray are not the.slideelements, but theirspanchildren. The tests rely on their parents being visible/not visible, so I cannot use the.toHaveAttribute('aria-hidden', "true")check.Even though they are rendered in the dom nodes, they are not visible for most users. They are only visible if they scroll very far in a single swipe, after which they remove the hidden attribute.
I can't use the
hiddenattribute in them. The parents should be visible in the case of a long swipe, but I can't test that they are hidden for screen readers users.eps1lon commentedon Nov 8, 2019
@andre-matulionis-ifood
byRoleonly include accessible elements by default. That might helpgnapse commentedon Nov 9, 2019
@andre-matulionis-ifood we may need a custom matcher for that because the semantics of
aria-hiddenare fundamentally different than that ofhidden. Can you convert that comment of yours into a new issue?andre-matulionis-ifood commentedon Nov 9, 2019
@gnapse done. Issue #162 Thanks
gnapse commentedon Jan 22, 2020
Hi folks,
Thanks for all your input.
I don't think we're going to do this (adding support for the presence of
aria-disabledtotoBeDisabled). The semantics are different, as discussed above. We're probably not going to add atoBeAriaDisabledeither. The workaround is.toHaveAttribute('aria-disabled', 'true').Good luck!
Dreamsorcerer commentedon Jan 21, 2024
Isn't this testing for the explicit attribute? Whereas the proposal above should return true whether it has has aria-disabled or disabled (because the latter implies aria-disabled).
Surely this is exactly the use case for the convenience methods such as .toBeDisabled()? If not, then all the above arguments should have already been applied and .toBeDisabled() would never have been added. If you explicitly want something with the disabled attribute, the check is
.toHaveAttribute('disabled'), and this is literally the first example in the docs for that method:https://github.com/testing-library/jest-dom#tohaveattribute
gnapse commentedon Jan 22, 2024
The problem with
aria-disabledis that it does not really tell you that the element is disabled. For instance, having a button like this:That button is still clickable and focusable. You can reach it when tabbing around the page, and click it with the mouse and with the keyboard. So having
.toBeDisabled()pass when it hasaria-disabled="true"and nothing else is misleading.This is not so simple. The
toBeDisabled()custom matcher still does more than that. An element is also disabled if it's inside a parent form or fieldset that is disabled. So even if an element does not have thedisabledattribute, the custom matcher may still pass the test.For instance:
The test below passes:
The same cannot be said of
aria-disabled, which is not only not really disabling anything, but it is also not inherited down the DOM tree in this way.move pacing settings to menu
feat(rules): add validity matcher rules
move pacing settings to menu