|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing'; |
| 10 | +import {RowHarnessFilters, CellHarnessFilters} from './table-harness-filters'; |
| 11 | +import {MatCellHarness, MatHeaderCellHarness, MatFooterCellHarness} from './cell-harness'; |
| 12 | + |
| 13 | +/** Data extracted from the cells in a table row. */ |
| 14 | +export type MatRowHarnessData = { |
| 15 | + columnName: string; |
| 16 | + text: string; |
| 17 | +}[]; |
| 18 | + |
| 19 | +/** Harness for interacting with a standard Angular Material table row. */ |
| 20 | +export class MatRowHarness extends ComponentHarness { |
| 21 | + static hostSelector = '.mat-row'; |
| 22 | + |
| 23 | + /** |
| 24 | + * Gets a `HarnessPredicate` that can be used to search for a row with specific attributes. |
| 25 | + */ |
| 26 | + static with(options: RowHarnessFilters = {}): HarnessPredicate<MatRowHarness> { |
| 27 | + return new HarnessPredicate(MatRowHarness, options); |
| 28 | + } |
| 29 | + |
| 30 | + /** Gets all cells of the table row. */ |
| 31 | + async getCells(filter: CellHarnessFilters = {}): Promise<MatCellHarness[]> { |
| 32 | + return this.locatorForAll(MatCellHarness.with(filter))(); |
| 33 | + } |
| 34 | + |
| 35 | + /** Gets the data of the cells in the footer row. */ |
| 36 | + async getData(filter: CellHarnessFilters = {}): Promise<MatRowHarnessData> { |
| 37 | + return getCellData(await this.getCells(filter)); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/** Harness for interacting with a standard Angular Material table header row. */ |
| 42 | +export class MatHeaderRowHarness extends ComponentHarness { |
| 43 | + static hostSelector = '.mat-header-row'; |
| 44 | + |
| 45 | + /** |
| 46 | + * Gets a `HarnessPredicate` that can be used to search for a header row with specific attributes. |
| 47 | + */ |
| 48 | + static with(options: RowHarnessFilters = {}): HarnessPredicate<MatHeaderRowHarness> { |
| 49 | + return new HarnessPredicate(MatHeaderRowHarness, options); |
| 50 | + } |
| 51 | + |
| 52 | + /** Gets all cells of the table header row. */ |
| 53 | + async getCells(filter: CellHarnessFilters = {}): Promise<MatHeaderCellHarness[]> { |
| 54 | + return this.locatorForAll(MatHeaderCellHarness.with(filter))(); |
| 55 | + } |
| 56 | + |
| 57 | + /** Gets the data of the cells in the footer row. */ |
| 58 | + async getData(filter: CellHarnessFilters = {}): Promise<MatRowHarnessData> { |
| 59 | + return getCellData(await this.getCells(filter)); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +/** Harness for interacting with a standard Angular Material table footer row. */ |
| 65 | +export class MatFooterRowHarness extends ComponentHarness { |
| 66 | + static hostSelector = '.mat-footer-row'; |
| 67 | + |
| 68 | + /** |
| 69 | + * Gets a `HarnessPredicate` that can be used to search for a footer row with specific attributes. |
| 70 | + */ |
| 71 | + static with(options: RowHarnessFilters = {}): HarnessPredicate<MatFooterRowHarness> { |
| 72 | + return new HarnessPredicate(MatFooterRowHarness, options); |
| 73 | + } |
| 74 | + |
| 75 | + /** Gets all cells of the table footer row. */ |
| 76 | + async getCells(filter: CellHarnessFilters = {}): Promise<MatFooterCellHarness[]> { |
| 77 | + return this.locatorForAll(MatFooterCellHarness.with(filter))(); |
| 78 | + } |
| 79 | + |
| 80 | + /** Gets the data of the cells in the footer row. */ |
| 81 | + async getData(filter: CellHarnessFilters = {}): Promise<MatRowHarnessData> { |
| 82 | + return getCellData(await this.getCells(filter)); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/** Extracts the data from the cells in a row. */ |
| 87 | +async function getCellData(cells: MatCellHarness[]): Promise<MatRowHarnessData> { |
| 88 | + return Promise.all(cells.map(async cell => ({ |
| 89 | + text: await cell.getText(), |
| 90 | + columnName: await cell.getColumnName() |
| 91 | + }))); |
| 92 | +} |
0 commit comments