Skip to content

refactor(material-experimental/mdc-paginator): de-duplicate test harness logic #21519

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
Jan 25, 2021
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
4 changes: 4 additions & 0 deletions scripts/check-mdc-exports-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export const config = {
// Private base class that is only exported for MDC.
'_MatPaginatorBase'
],
'mdc-paginator/testing': [
// Private base class that is only exported for MDC.
'_MatPaginatorHarnessBase'
],
'mdc-radio': [
// Private base classes that are only exported for MDC.
'_MatRadioGroupBase',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ ts_library(
),
module_name = "@angular/material-experimental/mdc-paginator/testing",
deps = [
"//src/cdk/coercion",
"//src/cdk/testing",
"//src/material-experimental/mdc-select/testing",
"//src/material/paginator/testing",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@
* found in the LICENSE file at https://angular.io/license
*/

import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
import {HarnessPredicate} from '@angular/cdk/testing';
import {MatSelectHarness} from '@angular/material-experimental/mdc-select/testing';
import {coerceNumberProperty} from '@angular/cdk/coercion';
import {PaginatorHarnessFilters} from './paginator-harness-filters';
import {
PaginatorHarnessFilters,
_MatPaginatorHarnessBase,
} from '@angular/material/paginator/testing';


/** Harness for interacting with an MDC-based mat-paginator in tests. */
export class MatPaginatorHarness extends ComponentHarness {
export class MatPaginatorHarness extends _MatPaginatorHarnessBase {
/** Selector used to find paginator instances. */
static hostSelector = '.mat-mdc-paginator';
private _nextButton = this.locatorFor('.mat-mdc-paginator-navigation-next');
private _previousButton = this.locatorFor('.mat-mdc-paginator-navigation-previous');
private _firstPageButton = this.locatorForOptional('.mat-mdc-paginator-navigation-first');
private _lastPageButton = this.locatorForOptional('.mat-mdc-paginator-navigation-last');
private _select = this.locatorForOptional(MatSelectHarness.with({
protected _nextButton = this.locatorFor('.mat-mdc-paginator-navigation-next');
protected _previousButton = this.locatorFor('.mat-mdc-paginator-navigation-previous');
protected _firstPageButton = this.locatorForOptional('.mat-mdc-paginator-navigation-first');
protected _lastPageButton = this.locatorForOptional('.mat-mdc-paginator-navigation-last');
protected _select = this.locatorForOptional(MatSelectHarness.with({
ancestor: '.mat-mdc-paginator-page-size'
}));
private _pageSizeFallback = this.locatorFor('.mat-mdc-paginator-page-size-value');
private _rangeLabel = this.locatorFor('.mat-mdc-paginator-range-label');
protected _pageSizeFallback = this.locatorFor('.mat-mdc-paginator-page-size-value');
protected _rangeLabel = this.locatorFor('.mat-mdc-paginator-range-label');

/**
* Gets a `HarnessPredicate` that can be used to search for a `MatPaginatorHarness` that meets
Expand All @@ -35,69 +37,4 @@ export class MatPaginatorHarness extends ComponentHarness {
static with(options: PaginatorHarnessFilters = {}): HarnessPredicate<MatPaginatorHarness> {
return new HarnessPredicate(MatPaginatorHarness, options);
}

/** Goes to the next page in the paginator. */
async goToNextPage(): Promise<void> {
return (await this._nextButton()).click();
}

/** Goes to the previous page in the paginator. */
async goToPreviousPage(): Promise<void> {
return (await this._previousButton()).click();
}

/** Goes to the first page in the paginator. */
async goToFirstPage(): Promise<void> {
const button = await this._firstPageButton();

// The first page button isn't enabled by default so we need to check for it.
if (!button) {
throw Error('Could not find first page button inside paginator. ' +
'Make sure that `showFirstLastButtons` is enabled.');
}

return button.click();
}

/** Goes to the last page in the paginator. */
async goToLastPage(): Promise<void> {
const button = await this._lastPageButton();

// The last page button isn't enabled by default so we need to check for it.
if (!button) {
throw Error('Could not find last page button inside paginator. ' +
'Make sure that `showFirstLastButtons` is enabled.');
}

return button.click();
}

/**
* Sets the page size of the paginator.
* @param size Page size that should be select.
*/
async setPageSize(size: number): Promise<void> {
const select = await this._select();

// The select is only available if the `pageSizeOptions` are
// set to an array with more than one item.
if (!select) {
throw Error('Cannot find page size selector in paginator. ' +
'Make sure that the `pageSizeOptions` have been configured.');
}

return select.clickOptions({text: `${size}`});
}

/** Gets the page size of the paginator. */
async getPageSize(): Promise<number> {
const select = await this._select();
const value = select ? select.getValueText() : (await this._pageSizeFallback()).text();
return coerceNumberProperty(await value);
}

/** Gets the text of the range labe of the paginator. */
async getRangeLabel(): Promise<string> {
return (await this._rangeLabel()).text();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
*/

export * from './paginator-harness';
export * from './paginator-harness-filters';
export {PaginatorHarnessFilters} from '@angular/material/paginator/testing';
67 changes: 42 additions & 25 deletions src/material/paginator/testing/paginator-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,27 @@
* found in the LICENSE file at https://angular.io/license
*/

import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
import {
AsyncFactoryFn,
ComponentHarness,
HarnessPredicate,
TestElement,
} from '@angular/cdk/testing';
import {MatSelectHarness} from '@angular/material/select/testing';
import {coerceNumberProperty} from '@angular/cdk/coercion';
import {PaginatorHarnessFilters} from './paginator-harness-filters';


/** Harness for interacting with a standard mat-paginator in tests. */
export class MatPaginatorHarness extends ComponentHarness {
/** Selector used to find paginator instances. */
static hostSelector = '.mat-paginator';
private _nextButton = this.locatorFor('.mat-paginator-navigation-next');
private _previousButton = this.locatorFor('.mat-paginator-navigation-previous');
private _firstPageButton = this.locatorForOptional('.mat-paginator-navigation-first');
private _lastPageButton = this.locatorForOptional('.mat-paginator-navigation-last');
private _select = this.locatorForOptional(MatSelectHarness.with({
ancestor: '.mat-paginator-page-size'
}));
private _pageSizeFallback = this.locatorFor('.mat-paginator-page-size-value');
private _rangeLabel = this.locatorFor('.mat-paginator-range-label');

/**
* Gets a `HarnessPredicate` that can be used to search for a `MatPaginatorHarness` that meets
* certain criteria.
* @param options Options for filtering which paginator instances are considered a match.
* @return a `HarnessPredicate` configured with the given options.
*/
static with(options: PaginatorHarnessFilters = {}): HarnessPredicate<MatPaginatorHarness> {
return new HarnessPredicate(MatPaginatorHarness, options);
}
export abstract class _MatPaginatorHarnessBase extends ComponentHarness {
protected abstract _nextButton: AsyncFactoryFn<TestElement>;
protected abstract _previousButton: AsyncFactoryFn<TestElement>;
protected abstract _firstPageButton: AsyncFactoryFn<TestElement | null>;
protected abstract _lastPageButton: AsyncFactoryFn<TestElement | null>;
protected abstract _select: AsyncFactoryFn<ComponentHarness & {
getValueText(): Promise<string>;
clickOptions(...filters: unknown[]): Promise<void>;
} | null>;
protected abstract _pageSizeFallback: AsyncFactoryFn<TestElement>;
protected abstract _rangeLabel: AsyncFactoryFn<TestElement>;

/** Goes to the next page in the paginator. */
async goToNextPage(): Promise<void> {
Expand Down Expand Up @@ -101,3 +93,28 @@ export class MatPaginatorHarness extends ComponentHarness {
return (await this._rangeLabel()).text();
}
}

/** Harness for interacting with a standard mat-paginator in tests. */
export class MatPaginatorHarness extends _MatPaginatorHarnessBase {
/** Selector used to find paginator instances. */
static hostSelector = '.mat-paginator';
protected _nextButton = this.locatorFor('.mat-paginator-navigation-next');
protected _previousButton = this.locatorFor('.mat-paginator-navigation-previous');
protected _firstPageButton = this.locatorForOptional('.mat-paginator-navigation-first');
protected _lastPageButton = this.locatorForOptional('.mat-paginator-navigation-last');
protected _select = this.locatorForOptional(MatSelectHarness.with({
ancestor: '.mat-paginator-page-size'
}));
protected _pageSizeFallback = this.locatorFor('.mat-paginator-page-size-value');
protected _rangeLabel = this.locatorFor('.mat-paginator-range-label');

/**
* Gets a `HarnessPredicate` that can be used to search for a `MatPaginatorHarness` that meets
* certain criteria.
* @param options Options for filtering which paginator instances are considered a match.
* @return a `HarnessPredicate` configured with the given options.
*/
static with(options: PaginatorHarnessFilters = {}): HarnessPredicate<MatPaginatorHarness> {
return new HarnessPredicate(MatPaginatorHarness, options);
}
}
22 changes: 21 additions & 1 deletion tools/public_api_guard/material/paginator/testing.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
export declare class MatPaginatorHarness extends ComponentHarness {
export declare abstract class _MatPaginatorHarnessBase extends ComponentHarness {
protected abstract _firstPageButton: AsyncFactoryFn<TestElement | null>;
protected abstract _lastPageButton: AsyncFactoryFn<TestElement | null>;
protected abstract _nextButton: AsyncFactoryFn<TestElement>;
protected abstract _pageSizeFallback: AsyncFactoryFn<TestElement>;
protected abstract _previousButton: AsyncFactoryFn<TestElement>;
protected abstract _rangeLabel: AsyncFactoryFn<TestElement>;
protected abstract _select: AsyncFactoryFn<(ComponentHarness & {
getValueText(): Promise<string>;
clickOptions(...filters: unknown[]): Promise<void>;
}) | null>;
getPageSize(): Promise<number>;
getRangeLabel(): Promise<string>;
goToFirstPage(): Promise<void>;
goToLastPage(): Promise<void>;
goToNextPage(): Promise<void>;
goToPreviousPage(): Promise<void>;
setPageSize(size: number): Promise<void>;
}

export declare class MatPaginatorHarness extends _MatPaginatorHarnessBase {
protected _firstPageButton: AsyncFactoryFn<TestElement | null>;
protected _lastPageButton: AsyncFactoryFn<TestElement | null>;
protected _nextButton: AsyncFactoryFn<TestElement>;
protected _pageSizeFallback: AsyncFactoryFn<TestElement>;
protected _previousButton: AsyncFactoryFn<TestElement>;
protected _rangeLabel: AsyncFactoryFn<TestElement>;
protected _select: AsyncFactoryFn<MatSelectHarness | null>;
static hostSelector: string;
static with(options?: PaginatorHarnessFilters): HarnessPredicate<MatPaginatorHarness>;
}
Expand Down