testing-library / testing-library/react-testing-library
Support complex aria-labelledby queries
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 19.7k
- Forks
- 1.2k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 1
Description
Describe the feature you'd like:
I'd like getByLabelText to resolve the associated elements of aria-labelledby. As I'm writing this it occurs to me that this might be too costly and perhaps better suited to something like axe.
Describe alternatives you've considered:
I've resorted to a combination of getByRole + getByText and checking IDs.
Teachability, Documentation, Adoption, Migration Strategy:
The summarised/pseudo code looks something like:
let contentId = 'content-id';
let spinnerId = 'spinner-id';
return (
<button
aria-disabled={props.pending ? true : undefined}
aria-labelledby={props.pending ? `${contentId} ${spinnerId}` : undefined}
>
<span id={contentId}>{props.children}</span>
{props.pending && (
<span id={spinnerId} aria-label="pending" aria-hidden>
...
</span>
)}
</button>
);
The desired test would look something like:
it('supports "pending" prop', () => {
let { getByLabelText } = render(<Button pending>Submit</Button>);
expect(getByLabelText(`Submit ${PENDING_LABEL}`)).toHaveAttribute('aria-disabled', 'true');
});
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the getByLabelText entry point and how it currently resolves label associations. Use the proposed pending-button example as a regression test, including multiple space-separated aria-labelledby IDs. Done means the query resolves the combined accessible label and the test verifies the button's aria-disabled attribute.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, react
- Domain
- frontend, testing-qa
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100