Skip to content

Commit d6bc318

Browse files
committed
feat(materiale-experimental/mdc-paginator): add test harness
Sets up a test harness for the MDC-based paginator.
1 parent e99ca0a commit d6bc318

File tree

7 files changed

+186
-0
lines changed

7 files changed

+186
-0
lines changed

src/material-experimental/config.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ entryPoints = [
2222
"mdc-menu",
2323
"mdc-menu/testing",
2424
"mdc-paginator",
25+
"mdc-paginator/testing",
2526
"mdc-progress-bar",
2627
"mdc-progress-bar/testing",
2728
"mdc-progress-spinner",
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
load("//tools:defaults.bzl", "ng_test_library", "ng_web_test_suite", "ts_library")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
ts_library(
6+
name = "testing",
7+
srcs = glob(
8+
["**/*.ts"],
9+
exclude = ["**/*.spec.ts"],
10+
),
11+
module_name = "@angular/material-experimental/mdc-paginator/testing",
12+
deps = [
13+
"//src/cdk/coercion",
14+
"//src/cdk/testing",
15+
"//src/material-experimental/mdc-select/testing",
16+
"//src/material/paginator/testing",
17+
],
18+
)
19+
20+
ng_test_library(
21+
name = "unit_tests_lib",
22+
srcs = glob(["**/*.spec.ts"]),
23+
deps = [
24+
":testing",
25+
"//src/material-experimental/mdc-paginator",
26+
"//src/material/paginator/testing:harness_tests_lib",
27+
],
28+
)
29+
30+
ng_web_test_suite(
31+
name = "unit_tests",
32+
static_files = [
33+
"@npm//:node_modules/@material/textfield/dist/mdc.textfield.js",
34+
"@npm//:node_modules/@material/line-ripple/dist/mdc.lineRipple.js",
35+
"@npm//:node_modules/@material/notched-outline/dist/mdc.notchedOutline.js",
36+
"@npm//:node_modules/@material/ripple/dist/mdc.ripple.js",
37+
"@npm//:node_modules/@material/dom/dist/mdc.dom.js",
38+
],
39+
deps = [
40+
":unit_tests_lib",
41+
"//src/material-experimental:mdc_require_config.js",
42+
],
43+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
export * from './public-api';
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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 {BaseHarnessFilters} from '@angular/cdk/testing';
10+
11+
/** A set of criteria that can be used to filter a list of `MatPaginatorHarness` instances. */
12+
export interface PaginatorHarnessFilters extends BaseHarnessFilters {
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {MatPaginatorModule} from '@angular/material-experimental/mdc-paginator';
2+
import {runHarnessTests} from '@angular/material/paginator/testing/shared.spec';
3+
import {MatPaginatorHarness} from './paginator-harness';
4+
5+
describe('MDC-based MatPaginatorHarness', () => {
6+
runHarnessTests(MatPaginatorModule, MatPaginatorHarness as any);
7+
});
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
export * from './paginator-harness';
10+
export * from './paginator-harness-filters';

0 commit comments

Comments
 (0)