Skip to content

Commit e45e452

Browse files
authored
feat(table/testing): allow filtering by column name (#19224)
Adds an extra filter to the column harness to allow cells to be picked out by column name. Fixes #19172.
1 parent dfe92fc commit e45e452

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

src/material/table/testing/cell-harness.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,7 @@ function getCellPredicate<T extends MatCellHarness>(
8888
options: CellHarnessFilters): HarnessPredicate<T> {
8989
return new HarnessPredicate(type, options)
9090
.addOption('text', options.text,
91-
(harness, text) => HarnessPredicate.stringMatches(harness.getText(), text));
91+
(harness, text) => HarnessPredicate.stringMatches(harness.getText(), text))
92+
.addOption('columnName', options.columnName,
93+
(harness, name) => HarnessPredicate.stringMatches(harness.getColumnName(), name));
9294
}

src/material/table/testing/shared.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ export function runHarnessTests(
7979
expect(cellTexts).toEqual(['1.0079']);
8080
});
8181

82+
it('should be able to filter cells by column name', async () => {
83+
const table = await loader.getHarness(tableHarness);
84+
const firstRow = (await table.getRows())[0];
85+
const cells = await firstRow.getCells({columnName: 'symbol'});
86+
const cellTexts = await Promise.all(cells.map(cell => cell.getText()));
87+
expect(cellTexts).toEqual(['H']);
88+
});
89+
8290
it('should be able to filter cells by regex', async () => {
8391
const table = await loader.getHarness(tableHarness);
8492
const firstRow = (await table.getRows())[0];

src/material/table/testing/table-harness-filters.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import {BaseHarnessFilters} from '@angular/cdk/testing';
1111
export interface CellHarnessFilters extends BaseHarnessFilters {
1212
/** Only find instances whose text matches the given value. */
1313
text?: string | RegExp;
14+
15+
/** Only find instances whose column name matches the given value. */
16+
columnName?: string | RegExp;
1417
}
1518

1619
/** A set of criteria that can be used to filter a list of row harness instances. */

tools/public_api_guard/material/table/testing.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export interface CellHarnessFilters extends BaseHarnessFilters {
2+
columnName?: string | RegExp;
23
text?: string | RegExp;
34
}
45

0 commit comments

Comments
 (0)