Queries for tables
See original GitHub issueDescribe the feature youβd like:
The current queries donβt really allow anything matching how a user would interact with a table.
There is limited support using the ByRole
selectors, to choose all rows, or all cells, or nesting between these, but doesnβt always match the principle of βuse your component as a user wouldβ.
e.g. In a table where each row has a βDeleteβ icon, choosing the delete icon corresponding to the particular row contents. A user would identify which row theyβre looking at by the value in one of the cells, then scanning across.
Suggested implementation:
Iβve implemented some custom queries in https://github.com/lexanth/testing-library-table-queries#readme, but some of it is a bit hacky.
Describe alternatives youβve considered:
getAllByRole('row')
gets rows, but then filtering by text content is slightly awkward (either index based or filtering based on content).
Teachability, Documentation, Adoption, Migration Strategy:
This would be additional queries, so backwards-compatible.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:28
- Comments:16 (2 by maintainers)
Iβve been digging into this for a project Iβve been working on with the intention of building queries based on table headers, which is something Iβve found challenging in the past. I tried to be pretty comprehensive to account for semantic and
aria-*
tables alike with variousrowSpan
s andcolSpan
s.Iβm sure that Iβve made some incorrect assumptions but figured Iβd share what I came up with and propose a path forward if this is of interest.
Code sample
Hereβs the gist of my solution π
So far Iβve tested this for the following table:
API
My proposal is to add a query,
byTableHeaderText
, and a corresponding matcher,toHaveTableHeaderText
to enable the following:A few more examples:
If this is of interest Iβd be happy to put up a PR!
Are there any examples or documentation of the current best practices for working with tables?