|
| 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 {MatSelectHarness} from '@angular/material-experimental/mdc-select/testing'; |
| 11 | +import {coerceNumberProperty} from '@angular/cdk/coercion'; |
| 12 | +import {PaginatorHarnessFilters} from './paginator-harness-filters'; |
| 13 | + |
| 14 | + |
| 15 | +/** Harness for interacting with an MDC-based mat-paginator in tests. */ |
| 16 | +export class MatPaginatorHarness extends ComponentHarness { |
| 17 | + /** Selector used to find paginator instances. */ |
| 18 | + static hostSelector = '.mat-mdc-paginator'; |
| 19 | + private _nextButton = this.locatorFor('.mat-mdc-paginator-navigation-next'); |
| 20 | + private _previousButton = this.locatorFor('.mat-mdc-paginator-navigation-previous'); |
| 21 | + private _firstPageButton = this.locatorForOptional('.mat-mdc-paginator-navigation-first'); |
| 22 | + private _lastPageButton = this.locatorForOptional('.mat-mdc-paginator-navigation-last'); |
| 23 | + private _select = this.locatorForOptional(MatSelectHarness.with({ |
| 24 | + ancestor: '.mat-mdc-paginator-page-size' |
| 25 | + })); |
| 26 | + private _pageSizeFallback = this.locatorFor('.mat-mdc-paginator-page-size-value'); |
| 27 | + private _rangeLabel = this.locatorFor('.mat-mdc-paginator-range-label'); |
| 28 | + |
| 29 | + /** |
| 30 | + * Gets a `HarnessPredicate` that can be used to search for a `MatPaginatorHarness` that meets |
| 31 | + * certain criteria. |
| 32 | + * @param options Options for filtering which paginator instances are considered a match. |
| 33 | + * @return a `HarnessPredicate` configured with the given options. |
| 34 | + */ |
| 35 | + static with(options: PaginatorHarnessFilters = {}): HarnessPredicate<MatPaginatorHarness> { |
| 36 | + return new HarnessPredicate(MatPaginatorHarness, options); |
| 37 | + } |
| 38 | + |
| 39 | + /** Goes to the next page in the paginator. */ |
| 40 | + async goToNextPage(): Promise<void> { |
| 41 | + return (await this._nextButton()).click(); |
| 42 | + } |
| 43 | + |
| 44 | + /** Goes to the previous page in the paginator. */ |
| 45 | + async goToPreviousPage(): Promise<void> { |
| 46 | + return (await this._previousButton()).click(); |
| 47 | + } |
| 48 | + |
| 49 | + /** Goes to the first page in the paginator. */ |
| 50 | + async goToFirstPage(): Promise<void> { |
| 51 | + const button = await this._firstPageButton(); |
| 52 | + |
| 53 | + // The first page button isn't enabled by default so we need to check for it. |
| 54 | + if (!button) { |
| 55 | + throw Error('Could not find first page button inside paginator. ' + |
| 56 | + 'Make sure that `showFirstLastButtons` is enabled.'); |
| 57 | + } |
| 58 | + |
| 59 | + return button.click(); |
| 60 | + } |
| 61 | + |
| 62 | + /** Goes to the last page in the paginator. */ |
| 63 | + async goToLastPage(): Promise<void> { |
| 64 | + const button = await this._lastPageButton(); |
| 65 | + |
| 66 | + // The last page button isn't enabled by default so we need to check for it. |
| 67 | + if (!button) { |
| 68 | + throw Error('Could not find last page button inside paginator. ' + |
| 69 | + 'Make sure that `showFirstLastButtons` is enabled.'); |
| 70 | + } |
| 71 | + |
| 72 | + return button.click(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Sets the page size of the paginator. |
| 77 | + * @param size Page size that should be select. |
| 78 | + */ |
| 79 | + async setPageSize(size: number): Promise<void> { |
| 80 | + const select = await this._select(); |
| 81 | + |
| 82 | + // The select is only available if the `pageSizeOptions` are |
| 83 | + // set to an array with more than one item. |
| 84 | + if (!select) { |
| 85 | + throw Error('Cannot find page size selector in paginator. ' + |
| 86 | + 'Make sure that the `pageSizeOptions` have been configured.'); |
| 87 | + } |
| 88 | + |
| 89 | + return select.clickOptions({text: `${size}`}); |
| 90 | + } |
| 91 | + |
| 92 | + /** Gets the page size of the paginator. */ |
| 93 | + async getPageSize(): Promise<number> { |
| 94 | + const select = await this._select(); |
| 95 | + const value = select ? select.getValueText() : (await this._pageSizeFallback()).text(); |
| 96 | + return coerceNumberProperty(await value); |
| 97 | + } |
| 98 | + |
| 99 | + /** Gets the text of the range labe of the paginator. */ |
| 100 | + async getRangeLabel(): Promise<string> { |
| 101 | + return (await this._rangeLabel()).text(); |
| 102 | + } |
| 103 | +} |
0 commit comments