|
| 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 | +/** Harness for interacting with a standard Angular Material table row. */ |
| 14 | +export class MatRowHarness extends ComponentHarness { |
| 15 | + /** The selector for the host element of a `MatRowHarness` instance. */ |
| 16 | + static hostSelector = '.mat-row'; |
| 17 | + |
| 18 | + /** |
| 19 | + * Gets a `HarnessPredicate` that can be used to search for a table row with specific attributes. |
| 20 | + * @param options Options for narrowing the search |
| 21 | + * @return a `HarnessPredicate` configured with the given options. |
| 22 | + */ |
| 23 | + static with(options: RowHarnessFilters = {}): HarnessPredicate<MatRowHarness> { |
| 24 | + return new HarnessPredicate(MatRowHarness, options); |
| 25 | + } |
| 26 | + |
| 27 | + /** Gets a list of `MatCellHarness` for all cells in the row. */ |
| 28 | + async getCells(filter: CellHarnessFilters = {}): Promise<MatCellHarness[]> { |
| 29 | + return this.locatorForAll(MatCellHarness.with(filter))(); |
| 30 | + } |
| 31 | + |
| 32 | + /** Gets the text of the cells in the row. */ |
| 33 | + async getCellTextByIndex(filter: CellHarnessFilters = {}): Promise<string[]> { |
| 34 | + return getCellTextByIndex(this, filter); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/** Harness for interacting with a standard Angular Material table header row. */ |
| 39 | +export class MatHeaderRowHarness extends ComponentHarness { |
| 40 | + /** The selector for the host element of a `MatHeaderRowHarness` instance. */ |
| 41 | + static hostSelector = '.mat-header-row'; |
| 42 | + |
| 43 | + /** |
| 44 | + * Gets a `HarnessPredicate` that can be used to search for |
| 45 | + * a table header row with specific attributes. |
| 46 | + * @param options Options for narrowing the search |
| 47 | + * @return a `HarnessPredicate` configured with the given options. |
| 48 | + */ |
| 49 | + static with(options: RowHarnessFilters = {}): HarnessPredicate<MatHeaderRowHarness> { |
| 50 | + return new HarnessPredicate(MatHeaderRowHarness, options); |
| 51 | + } |
| 52 | + |
| 53 | + /** Gets a list of `MatHeaderCellHarness` for all cells in the row. */ |
| 54 | + async getCells(filter: CellHarnessFilters = {}): Promise<MatHeaderCellHarness[]> { |
| 55 | + return this.locatorForAll(MatHeaderCellHarness.with(filter))(); |
| 56 | + } |
| 57 | + |
| 58 | + /** Gets the text of the cells in the header row. */ |
| 59 | + async getCellTextByIndex(filter: CellHarnessFilters = {}): Promise<string[]> { |
| 60 | + return getCellTextByIndex(this, filter); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +/** Harness for interacting with a standard Angular Material table footer row. */ |
| 66 | +export class MatFooterRowHarness extends ComponentHarness { |
| 67 | + /** The selector for the host element of a `MatFooterRowHarness` instance. */ |
| 68 | + static hostSelector = '.mat-footer-row'; |
| 69 | + |
| 70 | + /** |
| 71 | + * Gets a `HarnessPredicate` that can be used to search for |
| 72 | + * a table footer row cell with specific attributes. |
| 73 | + * @param options Options for narrowing the search |
| 74 | + * @return a `HarnessPredicate` configured with the given options. |
| 75 | + */ |
| 76 | + static with(options: RowHarnessFilters = {}): HarnessPredicate<MatFooterRowHarness> { |
| 77 | + return new HarnessPredicate(MatFooterRowHarness, options); |
| 78 | + } |
| 79 | + |
| 80 | + /** Gets a list of `MatFooterCellHarness` for all cells in the row. */ |
| 81 | + async getCells(filter: CellHarnessFilters = {}): Promise<MatFooterCellHarness[]> { |
| 82 | + return this.locatorForAll(MatFooterCellHarness.with(filter))(); |
| 83 | + } |
| 84 | + |
| 85 | + /** Gets the text of the cells in the footer row. */ |
| 86 | + async getCellTextByIndex(filter: CellHarnessFilters = {}): Promise<string[]> { |
| 87 | + return getCellTextByIndex(this, filter); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +async function getCellTextByIndex(harness: { |
| 92 | + getCells: (filter?: CellHarnessFilters) => Promise<MatCellHarness[]> |
| 93 | +}, filter: CellHarnessFilters): Promise<string[]> { |
| 94 | + const cells = await harness.getCells(filter); |
| 95 | + return Promise.all(cells.map(cell => cell.getText())); |
| 96 | +} |
0 commit comments