Skip to content

Commit 3269dae

Browse files
authored
refactor(material-experimental/mdc-paginator): de-duplicate test harness logic (#21519)
Changes the MDC-based `MatPaginatorHarness` to extend the non-MDC version since the logic is identical, apart from a few selectors.
1 parent 241e85d commit 3269dae

File tree

7 files changed

+81
-117
lines changed

7 files changed

+81
-117
lines changed

scripts/check-mdc-exports-config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ export const config = {
4646
// Private base class that is only exported for MDC.
4747
'_MatPaginatorBase'
4848
],
49+
'mdc-paginator/testing': [
50+
// Private base class that is only exported for MDC.
51+
'_MatPaginatorHarnessBase'
52+
],
4953
'mdc-radio': [
5054
// Private base classes that are only exported for MDC.
5155
'_MatRadioGroupBase',

src/material-experimental/mdc-paginator/testing/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ ts_library(
1010
),
1111
module_name = "@angular/material-experimental/mdc-paginator/testing",
1212
deps = [
13-
"//src/cdk/coercion",
1413
"//src/cdk/testing",
1514
"//src/material-experimental/mdc-select/testing",
1615
"//src/material/paginator/testing",

src/material-experimental/mdc-paginator/testing/paginator-harness-filters.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/material-experimental/mdc-paginator/testing/paginator-harness.ts

Lines changed: 13 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,27 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
9+
import {HarnessPredicate} from '@angular/cdk/testing';
1010
import {MatSelectHarness} from '@angular/material-experimental/mdc-select/testing';
11-
import {coerceNumberProperty} from '@angular/cdk/coercion';
12-
import {PaginatorHarnessFilters} from './paginator-harness-filters';
11+
import {
12+
PaginatorHarnessFilters,
13+
_MatPaginatorHarnessBase,
14+
} from '@angular/material/paginator/testing';
1315

1416

1517
/** Harness for interacting with an MDC-based mat-paginator in tests. */
16-
export class MatPaginatorHarness extends ComponentHarness {
18+
export class MatPaginatorHarness extends _MatPaginatorHarnessBase {
1719
/** Selector used to find paginator instances. */
1820
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({
21+
protected _nextButton = this.locatorFor('.mat-mdc-paginator-navigation-next');
22+
protected _previousButton = this.locatorFor('.mat-mdc-paginator-navigation-previous');
23+
protected _firstPageButton = this.locatorForOptional('.mat-mdc-paginator-navigation-first');
24+
protected _lastPageButton = this.locatorForOptional('.mat-mdc-paginator-navigation-last');
25+
protected _select = this.locatorForOptional(MatSelectHarness.with({
2426
ancestor: '.mat-mdc-paginator-page-size'
2527
}));
26-
private _pageSizeFallback = this.locatorFor('.mat-mdc-paginator-page-size-value');
27-
private _rangeLabel = this.locatorFor('.mat-mdc-paginator-range-label');
28+
protected _pageSizeFallback = this.locatorFor('.mat-mdc-paginator-page-size-value');
29+
protected _rangeLabel = this.locatorFor('.mat-mdc-paginator-range-label');
2830

2931
/**
3032
* Gets a `HarnessPredicate` that can be used to search for a `MatPaginatorHarness` that meets
@@ -35,69 +37,4 @@ export class MatPaginatorHarness extends ComponentHarness {
3537
static with(options: PaginatorHarnessFilters = {}): HarnessPredicate<MatPaginatorHarness> {
3638
return new HarnessPredicate(MatPaginatorHarness, options);
3739
}
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-
}
10340
}

src/material-experimental/mdc-paginator/testing/public-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
*/
88

99
export * from './paginator-harness';
10-
export * from './paginator-harness-filters';
10+
export {PaginatorHarnessFilters} from '@angular/material/paginator/testing';

src/material/paginator/testing/paginator-harness.ts

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,27 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
9+
import {
10+
AsyncFactoryFn,
11+
ComponentHarness,
12+
HarnessPredicate,
13+
TestElement,
14+
} from '@angular/cdk/testing';
1015
import {MatSelectHarness} from '@angular/material/select/testing';
1116
import {coerceNumberProperty} from '@angular/cdk/coercion';
1217
import {PaginatorHarnessFilters} from './paginator-harness-filters';
1318

14-
15-
/** Harness for interacting with a standard mat-paginator in tests. */
16-
export class MatPaginatorHarness extends ComponentHarness {
17-
/** Selector used to find paginator instances. */
18-
static hostSelector = '.mat-paginator';
19-
private _nextButton = this.locatorFor('.mat-paginator-navigation-next');
20-
private _previousButton = this.locatorFor('.mat-paginator-navigation-previous');
21-
private _firstPageButton = this.locatorForOptional('.mat-paginator-navigation-first');
22-
private _lastPageButton = this.locatorForOptional('.mat-paginator-navigation-last');
23-
private _select = this.locatorForOptional(MatSelectHarness.with({
24-
ancestor: '.mat-paginator-page-size'
25-
}));
26-
private _pageSizeFallback = this.locatorFor('.mat-paginator-page-size-value');
27-
private _rangeLabel = this.locatorFor('.mat-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-
}
19+
export abstract class _MatPaginatorHarnessBase extends ComponentHarness {
20+
protected abstract _nextButton: AsyncFactoryFn<TestElement>;
21+
protected abstract _previousButton: AsyncFactoryFn<TestElement>;
22+
protected abstract _firstPageButton: AsyncFactoryFn<TestElement | null>;
23+
protected abstract _lastPageButton: AsyncFactoryFn<TestElement | null>;
24+
protected abstract _select: AsyncFactoryFn<ComponentHarness & {
25+
getValueText(): Promise<string>;
26+
clickOptions(...filters: unknown[]): Promise<void>;
27+
} | null>;
28+
protected abstract _pageSizeFallback: AsyncFactoryFn<TestElement>;
29+
protected abstract _rangeLabel: AsyncFactoryFn<TestElement>;
3830

3931
/** Goes to the next page in the paginator. */
4032
async goToNextPage(): Promise<void> {
@@ -101,3 +93,28 @@ export class MatPaginatorHarness extends ComponentHarness {
10193
return (await this._rangeLabel()).text();
10294
}
10395
}
96+
97+
/** Harness for interacting with a standard mat-paginator in tests. */
98+
export class MatPaginatorHarness extends _MatPaginatorHarnessBase {
99+
/** Selector used to find paginator instances. */
100+
static hostSelector = '.mat-paginator';
101+
protected _nextButton = this.locatorFor('.mat-paginator-navigation-next');
102+
protected _previousButton = this.locatorFor('.mat-paginator-navigation-previous');
103+
protected _firstPageButton = this.locatorForOptional('.mat-paginator-navigation-first');
104+
protected _lastPageButton = this.locatorForOptional('.mat-paginator-navigation-last');
105+
protected _select = this.locatorForOptional(MatSelectHarness.with({
106+
ancestor: '.mat-paginator-page-size'
107+
}));
108+
protected _pageSizeFallback = this.locatorFor('.mat-paginator-page-size-value');
109+
protected _rangeLabel = this.locatorFor('.mat-paginator-range-label');
110+
111+
/**
112+
* Gets a `HarnessPredicate` that can be used to search for a `MatPaginatorHarness` that meets
113+
* certain criteria.
114+
* @param options Options for filtering which paginator instances are considered a match.
115+
* @return a `HarnessPredicate` configured with the given options.
116+
*/
117+
static with(options: PaginatorHarnessFilters = {}): HarnessPredicate<MatPaginatorHarness> {
118+
return new HarnessPredicate(MatPaginatorHarness, options);
119+
}
120+
}

tools/public_api_guard/material/paginator/testing.d.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
1-
export declare class MatPaginatorHarness extends ComponentHarness {
1+
export declare abstract class _MatPaginatorHarnessBase extends ComponentHarness {
2+
protected abstract _firstPageButton: AsyncFactoryFn<TestElement | null>;
3+
protected abstract _lastPageButton: AsyncFactoryFn<TestElement | null>;
4+
protected abstract _nextButton: AsyncFactoryFn<TestElement>;
5+
protected abstract _pageSizeFallback: AsyncFactoryFn<TestElement>;
6+
protected abstract _previousButton: AsyncFactoryFn<TestElement>;
7+
protected abstract _rangeLabel: AsyncFactoryFn<TestElement>;
8+
protected abstract _select: AsyncFactoryFn<(ComponentHarness & {
9+
getValueText(): Promise<string>;
10+
clickOptions(...filters: unknown[]): Promise<void>;
11+
}) | null>;
212
getPageSize(): Promise<number>;
313
getRangeLabel(): Promise<string>;
414
goToFirstPage(): Promise<void>;
515
goToLastPage(): Promise<void>;
616
goToNextPage(): Promise<void>;
717
goToPreviousPage(): Promise<void>;
818
setPageSize(size: number): Promise<void>;
19+
}
20+
21+
export declare class MatPaginatorHarness extends _MatPaginatorHarnessBase {
22+
protected _firstPageButton: AsyncFactoryFn<TestElement | null>;
23+
protected _lastPageButton: AsyncFactoryFn<TestElement | null>;
24+
protected _nextButton: AsyncFactoryFn<TestElement>;
25+
protected _pageSizeFallback: AsyncFactoryFn<TestElement>;
26+
protected _previousButton: AsyncFactoryFn<TestElement>;
27+
protected _rangeLabel: AsyncFactoryFn<TestElement>;
28+
protected _select: AsyncFactoryFn<MatSelectHarness | null>;
929
static hostSelector: string;
1030
static with(options?: PaginatorHarnessFilters): HarnessPredicate<MatPaginatorHarness>;
1131
}

0 commit comments

Comments
 (0)