diff --git a/src/material/table/testing/row-harness.ts b/src/material/table/testing/row-harness.ts index 88c03f6d1f7f..c0b03d4b4472 100644 --- a/src/material/table/testing/row-harness.ts +++ b/src/material/table/testing/row-harness.ts @@ -10,6 +10,11 @@ import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing'; import {RowHarnessFilters, CellHarnessFilters} from './table-harness-filters'; import {MatCellHarness, MatHeaderCellHarness, MatFooterCellHarness} from './cell-harness'; +/** Text extracted from a table row organized by columns. */ +export interface MatRowHarnessColumnsText { + [columnName: string]: string; +} + /** Harness for interacting with a standard Angular Material table row. */ export class MatRowHarness extends ComponentHarness { /** The selector for the host element of a `MatRowHarness` instance. */ @@ -33,6 +38,11 @@ export class MatRowHarness extends ComponentHarness { async getCellTextByIndex(filter: CellHarnessFilters = {}): Promise { return getCellTextByIndex(this, filter); } + + /** Gets the text inside the row organized by columns. */ + async getCellTextByColumnName(): Promise { + return getCellTextByColumnName(this); + } } /** Harness for interacting with a standard Angular Material table header row. */ @@ -59,6 +69,11 @@ export class MatHeaderRowHarness extends ComponentHarness { async getCellTextByIndex(filter: CellHarnessFilters = {}): Promise { return getCellTextByIndex(this, filter); } + + /** Gets the text inside the header row organized by columns. */ + async getCellTextByColumnName(): Promise { + return getCellTextByColumnName(this); + } } @@ -86,11 +101,29 @@ export class MatFooterRowHarness extends ComponentHarness { async getCellTextByIndex(filter: CellHarnessFilters = {}): Promise { return getCellTextByIndex(this, filter); } + + /** Gets the text inside the footer row organized by columns. */ + async getCellTextByColumnName(): Promise { + return getCellTextByColumnName(this); + } } + async function getCellTextByIndex(harness: { getCells: (filter?: CellHarnessFilters) => Promise }, filter: CellHarnessFilters): Promise { const cells = await harness.getCells(filter); return Promise.all(cells.map(cell => cell.getText())); } + +async function getCellTextByColumnName(harness: { + getCells: () => Promise +}): Promise { + const output: MatRowHarnessColumnsText = {}; + const cells = await harness.getCells(); + const cellsData = await Promise.all(cells.map(cell => { + return Promise.all([cell.getColumnName(), cell.getText()]); + })); + cellsData.forEach(([columnName, text]) => output[columnName] = text); + return output; +} diff --git a/src/material/table/testing/shared.spec.ts b/src/material/table/testing/shared.spec.ts index 8ac81fa76787..954d1e163127 100644 --- a/src/material/table/testing/shared.spec.ts +++ b/src/material/table/testing/shared.spec.ts @@ -138,6 +138,27 @@ export function runHarnessTests( ['10', 'Neon', '20.1797', 'Ne'] ]); }); + + it('should be able to get the cell text in a row organized by index', async () => { + const table = await loader.getHarness(tableHarness); + const rows = await table.getRows(); + + expect(rows.length).toBeGreaterThan(0); + expect(await rows[0].getCellTextByIndex()).toEqual(['1', 'Hydrogen', '1.0079', 'H']); + }); + + it('should be able to get the cell text in a row organized by columns', async () => { + const table = await loader.getHarness(tableHarness); + const rows = await table.getRows(); + + expect(rows.length).toBeGreaterThan(0); + expect(await rows[0].getCellTextByColumnName()).toEqual({ + position: '1', + name: 'Hydrogen', + weight: '1.0079', + symbol: 'H' + }); + }); } @Component({ diff --git a/src/material/table/testing/table-harness.ts b/src/material/table/testing/table-harness.ts index dcd5ff64f16c..6f9f8835b89d 100644 --- a/src/material/table/testing/table-harness.ts +++ b/src/material/table/testing/table-harness.ts @@ -8,7 +8,12 @@ import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing'; import {TableHarnessFilters, RowHarnessFilters} from './table-harness-filters'; -import {MatRowHarness, MatHeaderRowHarness, MatFooterRowHarness} from './row-harness'; +import { + MatRowHarness, + MatHeaderRowHarness, + MatFooterRowHarness, + MatRowHarnessColumnsText, +} from './row-harness'; /** Text extracted from a table organized by columns. */ export interface MatTableHarnessColumnsText { @@ -64,13 +69,15 @@ export class MatTableHarness extends ComponentHarness { const text: MatTableHarnessColumnsText = {}; const [headerData, footerData, rowsData] = await Promise.all([ - Promise.all(headerRows.map(row => getRowData(row))), - Promise.all(footerRows.map(row => getRowData(row))), - Promise.all(dataRows.map(row => getRowData(row))), + Promise.all(headerRows.map(row => row.getCellTextByColumnName())), + Promise.all(footerRows.map(row => row.getCellTextByColumnName())), + Promise.all(dataRows.map(row => row.getCellTextByColumnName())), ]); - rowsData.forEach(cells => { - cells.forEach(([columnName, cellText]) => { + rowsData.forEach(data => { + Object.keys(data).forEach(columnName => { + const cellText = data[columnName]; + if (!text[columnName]) { text[columnName] = { headerText: getCellTextsByColumn(headerData, columnName), @@ -87,21 +94,14 @@ export class MatTableHarness extends ComponentHarness { } } -/** Utility to extract the column names and text from all of the cells in a row. */ -async function getRowData(row: MatRowHarness | MatHeaderRowHarness | MatFooterRowHarness) { - const cells = await row.getCells(); - return Promise.all(cells.map(cell => Promise.all([cell.getColumnName(), cell.getText()]))); -} - - /** Extracts the text of cells only under a particular column. */ -function getCellTextsByColumn(rowsData: [string, string][][], column: string): string[] { +function getCellTextsByColumn(rowsData: MatRowHarnessColumnsText[], column: string): string[] { const columnTexts: string[] = []; - rowsData.forEach(cells => { - cells.forEach(([columnName, cellText]) => { + rowsData.forEach(data => { + Object.keys(data).forEach(columnName => { if (columnName === column) { - columnTexts.push(cellText); + columnTexts.push(data[columnName]); } }); }); diff --git a/tools/public_api_guard/material/table/testing.d.ts b/tools/public_api_guard/material/table/testing.d.ts index de7736e82333..eb35b624df18 100644 --- a/tools/public_api_guard/material/table/testing.d.ts +++ b/tools/public_api_guard/material/table/testing.d.ts @@ -15,6 +15,7 @@ export declare class MatFooterCellHarness extends MatCellHarness { } export declare class MatFooterRowHarness extends ComponentHarness { + getCellTextByColumnName(): Promise; getCellTextByIndex(filter?: CellHarnessFilters): Promise; getCells(filter?: CellHarnessFilters): Promise; static hostSelector: string; @@ -27,6 +28,7 @@ export declare class MatHeaderCellHarness extends MatCellHarness { } export declare class MatHeaderRowHarness extends ComponentHarness { + getCellTextByColumnName(): Promise; getCellTextByIndex(filter?: CellHarnessFilters): Promise; getCells(filter?: CellHarnessFilters): Promise; static hostSelector: string; @@ -34,12 +36,17 @@ export declare class MatHeaderRowHarness extends ComponentHarness { } export declare class MatRowHarness extends ComponentHarness { + getCellTextByColumnName(): Promise; getCellTextByIndex(filter?: CellHarnessFilters): Promise; getCells(filter?: CellHarnessFilters): Promise; static hostSelector: string; static with(options?: RowHarnessFilters): HarnessPredicate; } +export interface MatRowHarnessColumnsText { + [columnName: string]: string; +} + export declare class MatTableHarness extends ComponentHarness { getCellTextByColumnName(): Promise; getCellTextByIndex(): Promise;