Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ expect(getRowByFirstCellText('John Smith')).toBeVisible()
fireEvent.click(within(getRowByFirstCellText('John Smith')).getByText('Delete'))
```

Users will generally find rows by scanning the content in the first column, then reading across the row. This finds that row (rather than just the first cell), which can then be used to identify other items within that row.
Users will generally find rows by scanning the content in the first column, then reading across the row. This finds that row (rather than just the first cell), which can then be used to identify other items within that row. You can also use regular expression matching instead of looking for an exact text:

```js
expect(getRowByFirstCellText(/John Smith/)).toBeVisible()
```

### Column cells by header text

Expand All @@ -125,7 +129,7 @@ ageCells.forEach((cell, index) => {
})
```

Returns an array of cells based on the text in the column header. Note that there is no DOM 'column' element, so it is an array of cells. If multiple columns have the same header text, the first is used. Optionally, this also supports an index (starting from zero) to support having multiple header rows:
Returns an array of cells based on the text (or a regex) in the column header. Note that there is no DOM 'column' element, so it is an array of cells. If multiple columns have the same header text, the first is used. Optionally, this also supports an index (starting from zero) to support having multiple header rows:

```js
const { getAllColumnCellsByHeaderText } = render(<MyTable />)
Expand All @@ -141,7 +145,7 @@ expect(getCellByRowAndColumnHeaders('John Smith', 'Age')).toHaveTextContent(
)
```

If a user is trying to find a specific value for a specific entity, they might scan from the row and column headers. This finds cells based on those headers. Like column cells by header text, it only uses the first column with the specified header text (but will handle multiple rows), and supports a header index.
If a user is trying to find a specific value for a specific entity, they might scan from the row and column headers. This finds cells based on those headers. Like column cells by header text (or regex), it only uses the first column with the specified header text (or regex) (but will handle multiple rows), and supports a header index.

## Examples

Expand All @@ -150,7 +154,6 @@ See [example tests](./example/src/SimpleTable.test.js)
## Future changes

- Address the first column limitation
- Allow custom text normalisation/matching
- Allow Nth cell in a row, rather than just first

## Development
Expand Down
34 changes: 20 additions & 14 deletions src/cellByRowAndColumnHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ import { queryAllRowsByFirstCellText } from './rowByFirstCellText'
import { getColumnIndexByHeaderText } from './utils/columnIndexByHeaderText'
import { getCellInRowByIndex } from './utils/cellInRowByIndex'
import { nthHeaderError } from './utils/nthHeaderError'
import { stringOrRegexError } from './utils/stringOrRegexError'

function queryAllCellsByRowAndColumnHeaders(
container: HTMLElement,
rowHeaderText: string,
columnheaderText: string,
rowHeaderTextQuery: string | RegExp,
columnheaderTextQuery: string | RegExp,
headerRowIndex = 0
) {
const rows = queryAllRowsByFirstCellText(container, rowHeaderText)
const rows = queryAllRowsByFirstCellText(container, rowHeaderTextQuery)

const columnIndex = getColumnIndexByHeaderText(
container,
columnheaderText,
columnheaderTextQuery,
headerRowIndex
)

Expand All @@ -25,22 +26,27 @@ function queryAllCellsByRowAndColumnHeaders(

const getMultipleError = (
_c: Element | null,
rowHeaderText: string,
columnheaderText: string,
rowHeaderText: string | RegExp,
columnheaderText: string | RegExp,
headerRowIndex = 0
) =>
`Found multiple cells with ${rowHeaderText} in the first column and ${columnheaderText} in the ${nthHeaderError(
headerRowIndex
)}`
`Found multiple cells ${stringOrRegexError(
rowHeaderText
)} in the first column and ${stringOrRegexError(
columnheaderText
)} in the ${nthHeaderError(headerRowIndex)}`

const getMissingError = (
_c: Element | null,
rowHeaderText: string,
columnheaderText: string,
rowHeaderText: string | RegExp,
columnheaderText: string | RegExp,
headerRowIndex = 0
) =>
`Found no rows with ${rowHeaderText} in the first column and ${columnheaderText} in the ${nthHeaderError(
headerRowIndex
)}`
`Found no rows ${stringOrRegexError(
rowHeaderText
)} in the first column and ${stringOrRegexError(
columnheaderText
)} in the ${nthHeaderError(headerRowIndex)}`

const [
queryCellByRowAndColumnHeaders,
Expand Down
16 changes: 10 additions & 6 deletions src/columnCellsByHeaderText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import { queryAllRows } from './rows'
import { getColumnIndexByHeaderText } from './utils/columnIndexByHeaderText'
import { getCellInRowByIndex } from './utils/cellInRowByIndex'
import { nthHeaderError } from './utils/nthHeaderError'
import { stringOrRegexError } from './utils/stringOrRegexError'

function queryAllColumnCellsByHeaderText(
container: HTMLElement,
textContent: string,
textQuery: string | RegExp,
headerRowIndex = 0
) {
const cellIndex = getColumnIndexByHeaderText(
container,
textContent,
textQuery,
headerRowIndex
)

Expand All @@ -24,18 +25,21 @@ function queryAllColumnCellsByHeaderText(

const getMultipleError = (
_c: Element | null,
textContent: string,
textQuery: string | RegExp,
headerRowIndex = 0
) =>
`Found multiple cells with ${textContent} in the ${nthHeaderError(
`Found mutiple cells ${stringOrRegexError(textQuery)} in the ${nthHeaderError(
headerRowIndex
)}`

const getMissingError = (
_c: Element | null,
textContent: string,
textQuery: string | RegExp,
headerRowIndex = 0
) =>
`Found no rows with ${textContent} in the ${nthHeaderError(headerRowIndex)}`
`Found no rows ${stringOrRegexError(textQuery)} in the ${nthHeaderError(
headerRowIndex
)}`

const [
queryColumnCellByHeaderText,
Expand Down
Loading