feat: add toHaveVirtualFocus matcher (#207)#730
Draft
HyeryongChoi wants to merge 1 commit into
Draft
Conversation
Adds a toHaveVirtualFocus() matcher that checks whether an element has virtual focus via aria-activedescendant, i.e. it is referenced by the aria-activedescendant attribute of the element that currently has DOM focus. Common in composite widgets (listbox, combobox, grid) where DOM focus stays on a container while aria-activedescendant tracks the active item. Scoped to toHaveVirtualFocus only; the related toBeActiveDescendant matcher discussed in the issue is left out so the two APIs can be reviewed independently.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What:
Adds a new
toHaveVirtualFocus()matcher that checks whether an elementhas "virtual focus" via
aria-activedescendant— i.e. it is referenced bythe
aria-activedescendantattribute of the element that currently hasDOM focus.
Why:
Closes part of #207. Composite widgets (listbox, combobox, grid, etc.)
commonly keep DOM focus on a container while
aria-activedescendanttracks which item is "active". There's currently no ergonomic way to
assert this in tests without reaching into
ownerDocument.activeElementand the attribute manually.
This PR scopes to
toHaveVirtualFocusonly. The issue also discusses apossible
toBeActiveDescendantmatcher as a second, related API — I'veleft that out of this PR so the two can be reviewed/discussed
independently. Happy to add it here too if that's preferred.
How:
Follows the same shape as the existing
toHaveFocusmatcher: readselement.ownerDocument.activeElement, resolves the element referenced byits
aria-activedescendantattribute (if any), and passes when thatresolved element is the one under test.
One gotcha worth flagging for review: in jsdom, non-natively-focusable
elements (e.g.
<ul>,<div>) silently ignore.focus()unless theyhave a
tabindex. The test fixtures includetabindex="0"on thelistbox container for this reason — real-world composite widgets need
this too, so it seemed worth keeping the tests realistic rather than
working around it.
Checklist: