question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Queries for tables

See original GitHub issue

Describe 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:open
  • Created 3 years ago
  • Reactions:28
  • Comments:16 (2 by maintainers)

github_iconTop GitHub Comments

58reactions
enagy27commented, Dec 26, 2021

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 various rowSpans and colSpans.

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:

Table of Items Sold August 2016 with nested row headers and column headers which is tested in the provided Github Gist
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (index) β”‚         0         β”‚      1      β”‚     2      β”‚     3     β”‚     4     β”‚       5       β”‚       6       β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚    0    β”‚        ''         β”‚     ''      β”‚ 'Clothes'  β”‚ 'Clothes' β”‚ 'Clothes' β”‚ 'Accessories' β”‚ 'Accessories' β”‚
β”‚    1    β”‚        ''         β”‚     ''      β”‚ 'Trousers' β”‚ 'Skirts'  β”‚ 'Dresses' β”‚  'Bracelets'  β”‚    'Rings'    β”‚
β”‚    2    β”‚     'Belgium'     β”‚  'Antwerp'  β”‚    '56'    β”‚   '22'    β”‚   '43'    β”‚     '72'      β”‚     '23'      β”‚
β”‚    3    β”‚     'Belgium'     β”‚   'Gent'    β”‚    '46'    β”‚   '18'    β”‚   '50'    β”‚     '61'      β”‚     '15'      β”‚
β”‚    4    β”‚     'Belgium'     β”‚ 'Brussels'  β”‚    '51'    β”‚   '27'    β”‚   '38'    β”‚     '69'      β”‚     '28'      β”‚
β”‚    5    β”‚ 'The Netherlands' β”‚ 'Amsterdam' β”‚    '89'    β”‚   '34'    β”‚   '69'    β”‚     '85'      β”‚     '38'      β”‚
β”‚    6    β”‚ 'The Netherlands' β”‚  'Utrecht'  β”‚    '80'    β”‚   '12'    β”‚   '43'    β”‚     '36'      β”‚     '19'      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

API

My proposal is to add a query, byTableHeaderText, and a corresponding matcher, toHaveTableHeaderText to enable the following:

const gentRow = within(table).getByRole('row', /Gent/);

const gentTrousers = within(gentRow).getByTableHeaderText('Trousers');
expect(gentTrousers).toHaveTableHeaderText('Gent');

A few more examples:

// Note: the second argument with scope is optional
const belgiumCells = screen.getAllByTableHeaderText('Belgium', { scope: 'rowgroup' });
const gentCells = screen.getAllByTableHeaderText('Gent', { scope: 'row' });

const gentHeader = within(table).getByRole('rowheader', { name: 'Gent' });
expect(gentHeader).toHaveTableHeaderText('Belgium', { scope: 'rowgroup' });

const clothesCells = screen.getAllByTableHeaderText('Clothes', { scope: 'colgroup' });
const trousersCells = screen.getAllByTableHeaderText('Trousers', { scope: 'col' });

const trousersHeader = within(table).getByRole('columnheader', { name: 'Trousers' });
expect(trousersHeader).toHaveTableHeaderText('Clothes', { scope: 'colgroup' });

// Filter to get the intersection
const gentTrousersCells = _.intersection(gentCells, trousersCells);

// Alternatively without lodash this intersection would be:
// gentCells.filter(cell => trousersCells.includes(cell));

If this is of interest I’d be happy to put up a PR!

24reactions
bradleylandiscommented, Jul 2, 2021

Are there any examples or documentation of the current best practices for working with tables?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create a query based on multiple tables - Microsoft Support
Build a select query by using tables with a many-to-many relationship Β· On the Create tab, in the Queries group, click Query Design....
Read more >
Basic queries: SQL and RA
In queries where all the data is found in one table, the FROM clause is where we specify the name of the table...
Read more >
Make Table Queries - CustomGuide
1. Click the Create tab on the ribbon. 2. Click the Query Design button. The Query Design window and Show Table dialog box...
Read more >
Query Tables l Zoho Analytics Help
Query Table is a feature that enables you to prepare data for easy reporting and analysis. You can combine data from one or...
Read more >
SQL Query on Multiple Tables - Exercises, Practice, Solution
[An editor is available at the bottom of the page to write and execute the scripts.] 1. From the following tables, write a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found