|
| 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 {HarnessPredicate, ContentContainerComponentHarness} from '@angular/cdk/testing'; |
| 10 | +import {CardHarnessFilters} from './card-harness-filters'; |
| 11 | + |
| 12 | +/** Selectors for different sections of the mat-card that can container user content. */ |
| 13 | +export const enum MatCardSection { |
| 14 | + HEADER = '.mat-mdc-card-header', |
| 15 | + CONTENT = '.mat-mdc-card-content', |
| 16 | + ACTIONS = '.mat-mdc-card-actions', |
| 17 | + FOOTER = '.mat-mdc-card-footer' |
| 18 | +} |
| 19 | + |
| 20 | +/** Harness for interacting with an MDC-based mat-card in tests. */ |
| 21 | +export class MatCardHarness extends ContentContainerComponentHarness<MatCardSection> { |
| 22 | + /** The selector for the host element of a `MatCard` instance. */ |
| 23 | + static hostSelector = '.mat-mdc-card'; |
| 24 | + |
| 25 | + /** |
| 26 | + * Gets a `HarnessPredicate` that can be used to search for a `MatCardHarness` that meets |
| 27 | + * certain criteria. |
| 28 | + * @param options Options for filtering which card instances are considered a match. |
| 29 | + * @return a `HarnessPredicate` configured with the given options. |
| 30 | + */ |
| 31 | + static with(options: CardHarnessFilters = {}): HarnessPredicate<MatCardHarness> { |
| 32 | + return new HarnessPredicate(MatCardHarness, options) |
| 33 | + .addOption('text', options.text, |
| 34 | + (harness, text) => HarnessPredicate.stringMatches(harness.getText(), text)) |
| 35 | + .addOption('title', options.title, |
| 36 | + (harness, title) => HarnessPredicate.stringMatches(harness.getTitleText(), title)) |
| 37 | + .addOption('subtitle', options.subtitle, |
| 38 | + (harness, subtitle) => |
| 39 | + HarnessPredicate.stringMatches(harness.getSubtitleText(), subtitle)); |
| 40 | + } |
| 41 | + |
| 42 | + private _title = this.locatorForOptional('.mat-mdc-card-title'); |
| 43 | + private _subtitle = this.locatorForOptional('.mat-mdc-card-subtitle'); |
| 44 | + |
| 45 | + /** Gets all of the card's content as text. */ |
| 46 | + async getText(): Promise<string> { |
| 47 | + return (await this.host()).text(); |
| 48 | + } |
| 49 | + |
| 50 | + /** Gets the cards's title text. */ |
| 51 | + async getTitleText(): Promise<string> { |
| 52 | + return (await this._title())?.text() ?? ''; |
| 53 | + } |
| 54 | + |
| 55 | + /** Gets the cards's subtitle text. */ |
| 56 | + async getSubtitleText(): Promise<string> { |
| 57 | + return (await this._subtitle())?.text() ?? ''; |
| 58 | + } |
| 59 | +} |
0 commit comments