Skip to content

Commit a936b34

Browse files
crisbetommalerba
authored andcommitted
refactor(table): add the ability to get row text organized by… (#18024)
Follow-up from the feedback in #17799. Adds a `getCellTextByColumnName` to the `MatRowHarness` which aligns with what we have on the table harness.
1 parent b989701 commit a936b34

File tree

4 files changed

+78
-17
lines changed

4 files changed

+78
-17
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
1010
import {RowHarnessFilters, CellHarnessFilters} from './table-harness-filters';
1111
import {MatCellHarness, MatHeaderCellHarness, MatFooterCellHarness} from './cell-harness';
1212

13+
/** Text extracted from a table row organized by columns. */
14+
export interface MatRowHarnessColumnsText {
15+
[columnName: string]: string;
16+
}
17+
1318
/** Harness for interacting with a standard Angular Material table row. */
1419
export class MatRowHarness extends ComponentHarness {
1520
/** The selector for the host element of a `MatRowHarness` instance. */
@@ -33,6 +38,11 @@ export class MatRowHarness extends ComponentHarness {
3338
async getCellTextByIndex(filter: CellHarnessFilters = {}): Promise<string[]> {
3439
return getCellTextByIndex(this, filter);
3540
}
41+
42+
/** Gets the text inside the row organized by columns. */
43+
async getCellTextByColumnName(): Promise<MatRowHarnessColumnsText> {
44+
return getCellTextByColumnName(this);
45+
}
3646
}
3747

3848
/** Harness for interacting with a standard Angular Material table header row. */
@@ -59,6 +69,11 @@ export class MatHeaderRowHarness extends ComponentHarness {
5969
async getCellTextByIndex(filter: CellHarnessFilters = {}): Promise<string[]> {
6070
return getCellTextByIndex(this, filter);
6171
}
72+
73+
/** Gets the text inside the header row organized by columns. */
74+
async getCellTextByColumnName(): Promise<MatRowHarnessColumnsText> {
75+
return getCellTextByColumnName(this);
76+
}
6277
}
6378

6479

@@ -86,11 +101,29 @@ export class MatFooterRowHarness extends ComponentHarness {
86101
async getCellTextByIndex(filter: CellHarnessFilters = {}): Promise<string[]> {
87102
return getCellTextByIndex(this, filter);
88103
}
104+
105+
/** Gets the text inside the footer row organized by columns. */
106+
async getCellTextByColumnName(): Promise<MatRowHarnessColumnsText> {
107+
return getCellTextByColumnName(this);
108+
}
89109
}
90110

111+
91112
async function getCellTextByIndex(harness: {
92113
getCells: (filter?: CellHarnessFilters) => Promise<MatCellHarness[]>
93114
}, filter: CellHarnessFilters): Promise<string[]> {
94115
const cells = await harness.getCells(filter);
95116
return Promise.all(cells.map(cell => cell.getText()));
96117
}
118+
119+
async function getCellTextByColumnName(harness: {
120+
getCells: () => Promise<MatCellHarness[]>
121+
}): Promise<MatRowHarnessColumnsText> {
122+
const output: MatRowHarnessColumnsText = {};
123+
const cells = await harness.getCells();
124+
const cellsData = await Promise.all(cells.map(cell => {
125+
return Promise.all([cell.getColumnName(), cell.getText()]);
126+
}));
127+
cellsData.forEach(([columnName, text]) => output[columnName] = text);
128+
return output;
129+
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,27 @@ export function runHarnessTests(
138138
['10', 'Neon', '20.1797', 'Ne']
139139
]);
140140
});
141+
142+
it('should be able to get the cell text in a row organized by index', async () => {
143+
const table = await loader.getHarness(tableHarness);
144+
const rows = await table.getRows();
145+
146+
expect(rows.length).toBeGreaterThan(0);
147+
expect(await rows[0].getCellTextByIndex()).toEqual(['1', 'Hydrogen', '1.0079', 'H']);
148+
});
149+
150+
it('should be able to get the cell text in a row organized by columns', async () => {
151+
const table = await loader.getHarness(tableHarness);
152+
const rows = await table.getRows();
153+
154+
expect(rows.length).toBeGreaterThan(0);
155+
expect(await rows[0].getCellTextByColumnName()).toEqual({
156+
position: '1',
157+
name: 'Hydrogen',
158+
weight: '1.0079',
159+
symbol: 'H'
160+
});
161+
});
141162
}
142163

143164
@Component({

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88

99
import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
1010
import {TableHarnessFilters, RowHarnessFilters} from './table-harness-filters';
11-
import {MatRowHarness, MatHeaderRowHarness, MatFooterRowHarness} from './row-harness';
11+
import {
12+
MatRowHarness,
13+
MatHeaderRowHarness,
14+
MatFooterRowHarness,
15+
MatRowHarnessColumnsText,
16+
} from './row-harness';
1217

1318
/** Text extracted from a table organized by columns. */
1419
export interface MatTableHarnessColumnsText {
@@ -64,13 +69,15 @@ export class MatTableHarness extends ComponentHarness {
6469

6570
const text: MatTableHarnessColumnsText = {};
6671
const [headerData, footerData, rowsData] = await Promise.all([
67-
Promise.all(headerRows.map(row => getRowData(row))),
68-
Promise.all(footerRows.map(row => getRowData(row))),
69-
Promise.all(dataRows.map(row => getRowData(row))),
72+
Promise.all(headerRows.map(row => row.getCellTextByColumnName())),
73+
Promise.all(footerRows.map(row => row.getCellTextByColumnName())),
74+
Promise.all(dataRows.map(row => row.getCellTextByColumnName())),
7075
]);
7176

72-
rowsData.forEach(cells => {
73-
cells.forEach(([columnName, cellText]) => {
77+
rowsData.forEach(data => {
78+
Object.keys(data).forEach(columnName => {
79+
const cellText = data[columnName];
80+
7481
if (!text[columnName]) {
7582
text[columnName] = {
7683
headerText: getCellTextsByColumn(headerData, columnName),
@@ -87,21 +94,14 @@ export class MatTableHarness extends ComponentHarness {
8794
}
8895
}
8996

90-
/** Utility to extract the column names and text from all of the cells in a row. */
91-
async function getRowData(row: MatRowHarness | MatHeaderRowHarness | MatFooterRowHarness) {
92-
const cells = await row.getCells();
93-
return Promise.all(cells.map(cell => Promise.all([cell.getColumnName(), cell.getText()])));
94-
}
95-
96-
9797
/** Extracts the text of cells only under a particular column. */
98-
function getCellTextsByColumn(rowsData: [string, string][][], column: string): string[] {
98+
function getCellTextsByColumn(rowsData: MatRowHarnessColumnsText[], column: string): string[] {
9999
const columnTexts: string[] = [];
100100

101-
rowsData.forEach(cells => {
102-
cells.forEach(([columnName, cellText]) => {
101+
rowsData.forEach(data => {
102+
Object.keys(data).forEach(columnName => {
103103
if (columnName === column) {
104-
columnTexts.push(cellText);
104+
columnTexts.push(data[columnName]);
105105
}
106106
});
107107
});

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export declare class MatFooterCellHarness extends MatCellHarness {
1515
}
1616

1717
export declare class MatFooterRowHarness extends ComponentHarness {
18+
getCellTextByColumnName(): Promise<MatRowHarnessColumnsText>;
1819
getCellTextByIndex(filter?: CellHarnessFilters): Promise<string[]>;
1920
getCells(filter?: CellHarnessFilters): Promise<MatFooterCellHarness[]>;
2021
static hostSelector: string;
@@ -27,19 +28,25 @@ export declare class MatHeaderCellHarness extends MatCellHarness {
2728
}
2829

2930
export declare class MatHeaderRowHarness extends ComponentHarness {
31+
getCellTextByColumnName(): Promise<MatRowHarnessColumnsText>;
3032
getCellTextByIndex(filter?: CellHarnessFilters): Promise<string[]>;
3133
getCells(filter?: CellHarnessFilters): Promise<MatHeaderCellHarness[]>;
3234
static hostSelector: string;
3335
static with(options?: RowHarnessFilters): HarnessPredicate<MatHeaderRowHarness>;
3436
}
3537

3638
export declare class MatRowHarness extends ComponentHarness {
39+
getCellTextByColumnName(): Promise<MatRowHarnessColumnsText>;
3740
getCellTextByIndex(filter?: CellHarnessFilters): Promise<string[]>;
3841
getCells(filter?: CellHarnessFilters): Promise<MatCellHarness[]>;
3942
static hostSelector: string;
4043
static with(options?: RowHarnessFilters): HarnessPredicate<MatRowHarness>;
4144
}
4245

46+
export interface MatRowHarnessColumnsText {
47+
[columnName: string]: string;
48+
}
49+
4350
export declare class MatTableHarness extends ComponentHarness {
4451
getCellTextByColumnName(): Promise<MatTableHarnessColumnsText>;
4552
getCellTextByIndex(): Promise<string[][]>;

0 commit comments

Comments
 (0)