Skip to content

feat: add toHaveVirtualFocus matcher (#207)#730

Draft
HyeryongChoi wants to merge 1 commit into
testing-library:mainfrom
HyeryongChoi:add-to-have-virtual-focus
Draft

feat: add toHaveVirtualFocus matcher (#207)#730
HyeryongChoi wants to merge 1 commit into
testing-library:mainfrom
HyeryongChoi:add-to-have-virtual-focus

Conversation

@HyeryongChoi

Copy link
Copy Markdown

What:

Adds a new 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.

Why:

Closes part of #207. Composite widgets (listbox, combobox, grid, etc.)
commonly keep DOM focus on a container while aria-activedescendant
tracks which item is "active". There's currently no ergonomic way to
assert this in tests without reaching into ownerDocument.activeElement
and the attribute manually.

This PR scopes to toHaveVirtualFocus only. The issue also discusses a
possible toBeActiveDescendant matcher as a second, related API — I've
left 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 toHaveFocus matcher: reads
element.ownerDocument.activeElement, resolves the element referenced by
its aria-activedescendant attribute (if any), and passes when that
resolved 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 they
have a tabindex. The test fixtures include tabindex="0" on the
listbox 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:

  • Documentation
  • Tests
  • Updated Type Definitions
  • Ready to be merged

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant