Skip to content

refactor(table): add the ability to get row text organized by column in harness #18024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2019
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
33 changes: 33 additions & 0 deletions src/material/table/testing/row-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -33,6 +38,11 @@ export class MatRowHarness extends ComponentHarness {
async getCellTextByIndex(filter: CellHarnessFilters = {}): Promise<string[]> {
return getCellTextByIndex(this, filter);
}

/** Gets the text inside the row organized by columns. */
async getCellTextByColumnName(): Promise<MatRowHarnessColumnsText> {
return getCellTextByColumnName(this);
}
}

/** Harness for interacting with a standard Angular Material table header row. */
Expand All @@ -59,6 +69,11 @@ export class MatHeaderRowHarness extends ComponentHarness {
async getCellTextByIndex(filter: CellHarnessFilters = {}): Promise<string[]> {
return getCellTextByIndex(this, filter);
}

/** Gets the text inside the header row organized by columns. */
async getCellTextByColumnName(): Promise<MatRowHarnessColumnsText> {
return getCellTextByColumnName(this);
}
}


Expand Down Expand Up @@ -86,11 +101,29 @@ export class MatFooterRowHarness extends ComponentHarness {
async getCellTextByIndex(filter: CellHarnessFilters = {}): Promise<string[]> {
return getCellTextByIndex(this, filter);
}

/** Gets the text inside the footer row organized by columns. */
async getCellTextByColumnName(): Promise<MatRowHarnessColumnsText> {
return getCellTextByColumnName(this);
}
}


async function getCellTextByIndex(harness: {
getCells: (filter?: CellHarnessFilters) => Promise<MatCellHarness[]>
}, filter: CellHarnessFilters): Promise<string[]> {
const cells = await harness.getCells(filter);
return Promise.all(cells.map(cell => cell.getText()));
}

async function getCellTextByColumnName(harness: {
getCells: () => Promise<MatCellHarness[]>
}): Promise<MatRowHarnessColumnsText> {
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;
}
21 changes: 21 additions & 0 deletions src/material/table/testing/shared.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
34 changes: 17 additions & 17 deletions src/material/table/testing/table-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand All @@ -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]);
}
});
});
Expand Down
7 changes: 7 additions & 0 deletions tools/public_api_guard/material/table/testing.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export declare class MatFooterCellHarness extends MatCellHarness {
}

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

export declare class MatHeaderRowHarness extends ComponentHarness {
getCellTextByColumnName(): Promise<MatRowHarnessColumnsText>;
getCellTextByIndex(filter?: CellHarnessFilters): Promise<string[]>;
getCells(filter?: CellHarnessFilters): Promise<MatHeaderCellHarness[]>;
static hostSelector: string;
static with(options?: RowHarnessFilters): HarnessPredicate<MatHeaderRowHarness>;
}

export declare class MatRowHarness extends ComponentHarness {
getCellTextByColumnName(): Promise<MatRowHarnessColumnsText>;
getCellTextByIndex(filter?: CellHarnessFilters): Promise<string[]>;
getCells(filter?: CellHarnessFilters): Promise<MatCellHarness[]>;
static hostSelector: string;
static with(options?: RowHarnessFilters): HarnessPredicate<MatRowHarness>;
}

export interface MatRowHarnessColumnsText {
[columnName: string]: string;
}

export declare class MatTableHarness extends ComponentHarness {
getCellTextByColumnName(): Promise<MatTableHarnessColumnsText>;
getCellTextByIndex(): Promise<string[][]>;
Expand Down