Skip to content

Commit cea7fc4

Browse files
committed
feat(material/list): add test harnesses for list components.
1 parent cdab906 commit cea7fc4

16 files changed

+1297
-9
lines changed

src/cdk/testing/component-harness.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -406,16 +406,23 @@ export class HarnessPredicate<T extends ComponentHarness> {
406406
}
407407

408408
/**
409-
* Checks if a string matches the given pattern.
410-
* @param s The string to check, or a Promise for the string to check.
411-
* @param pattern The pattern the string is expected to match. If `pattern` is a string, `s` is
412-
* expected to match exactly. If `pattern` is a regex, a partial match is allowed.
413-
* @return A Promise that resolves to whether the string matches the pattern.
409+
* Checks if the specified nullable string value matches the given pattern.
410+
* @param value The nullable string value to check, or a Promise resolving to the
411+
* nullable string value.
412+
* @param pattern The pattern the value is expected to match. If `pattern` is a string,
413+
* `value` is expected to match exactly. If `pattern` is a regex, a partial match is
414+
* allowed. If `pattern` is `null`, the value is expected to be `null`.
415+
* @return A Promise that resolves to whether the value matches the pattern.
414416
*/
415-
static async stringMatches(s: string | Promise<string>, pattern: string | RegExp):
416-
Promise<boolean> {
417-
s = await s;
418-
return typeof pattern === 'string' ? s === pattern : pattern.test(s);
417+
static async stringMatches(value: string | null | Promise<string | null>,
418+
pattern: string | RegExp | null): Promise<boolean> {
419+
value = await value;
420+
if (pattern === null) {
421+
return value === null;
422+
} else if (value === null) {
423+
return false;
424+
}
425+
return typeof pattern === 'string' ? value === pattern : pattern.test(value);
419426
}
420427

421428
/**

src/material/config.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ entryPoints = [
1515
"dialog",
1616
"dialog/testing",
1717
"divider",
18+
"divider/testing",
1819
"expansion",
1920
"form-field",
2021
"grid-list",
2122
"icon",
2223
"input",
2324
"list",
25+
"list/testing",
2426
"menu",
2527
"menu/testing",
2628
"paginator",
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("//tools:defaults.bzl", "ng_test_library", "ng_web_test_suite", "ts_library")
4+
5+
ts_library(
6+
name = "testing",
7+
srcs = glob(
8+
["**/*.ts"],
9+
exclude = ["**/*.spec.ts"],
10+
),
11+
module_name = "@angular/material/divider/testing",
12+
deps = [
13+
"//src/cdk/testing",
14+
],
15+
)
16+
17+
filegroup(
18+
name = "source-files",
19+
srcs = glob(["**/*.ts"]),
20+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
export interface DividerHarnessFilters extends BaseHarnessFilters {
12+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 {DividerHarnessFilters} from './divider-harness-filters';
11+
12+
/**
13+
* Harness for interacting with a `mat-divider`.
14+
* @dynamic
15+
*/
16+
export class MatDividerHarness extends ComponentHarness {
17+
static hostSelector = 'mat-divider';
18+
19+
static with(options: DividerHarnessFilters = {}) {
20+
return new HarnessPredicate(MatDividerHarness, options);
21+
}
22+
23+
async getOrientation(): Promise<'horizontal' | 'vertical'> {
24+
return (await this.host()).getAttribute('aria-orientation') as
25+
Promise<'horizontal' | 'vertical'>;
26+
}
27+
28+
async isInset(): Promise<boolean> {
29+
return (await this.host()).hasClass('mat-divider-inset');
30+
}
31+
}

src/material/divider/testing/index.ts

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: 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 './divider-harness';
10+
export * from './divider-harness-filters';

src/material/list/testing/BUILD.bazel

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("//tools:defaults.bzl", "ng_test_library", "ng_web_test_suite", "ts_library")
4+
5+
ts_library(
6+
name = "testing",
7+
srcs = glob(
8+
["**/*.ts"],
9+
exclude = ["**/*.spec.ts"],
10+
),
11+
module_name = "@angular/material/list/testing",
12+
deps = [
13+
"//src/cdk/coercion",
14+
"//src/cdk/testing",
15+
"//src/material/divider/testing",
16+
],
17+
)
18+
19+
filegroup(
20+
name = "source-files",
21+
srcs = glob(["**/*.ts"]),
22+
)
23+
24+
ng_test_library(
25+
name = "harness_tests_lib",
26+
srcs = ["shared.spec.ts"],
27+
deps = [
28+
":testing",
29+
"//src/cdk/testing",
30+
"//src/cdk/testing/testbed",
31+
"//src/material/divider/testing",
32+
"//src/material/list",
33+
"@npm//@angular/platform-browser",
34+
],
35+
)
36+
37+
ng_test_library(
38+
name = "unit_tests_lib",
39+
srcs = glob(
40+
["**/*.spec.ts"],
41+
exclude = ["shared.spec.ts"],
42+
),
43+
deps = [
44+
":harness_tests_lib",
45+
":testing",
46+
"//src/material/divider/testing",
47+
"//src/material/list",
48+
],
49+
)
50+
51+
ng_web_test_suite(
52+
name = "unit_tests",
53+
deps = [":unit_tests_lib"],
54+
)

src/material/list/testing/index.ts

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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
export interface ListHarnessFilters extends BaseHarnessFilters {}
12+
13+
export interface ActionListHarnessFilters extends BaseHarnessFilters {}
14+
15+
export interface NavListHarnessFilters extends BaseHarnessFilters {}
16+
17+
export interface SelectionListHarnessFilters extends BaseHarnessFilters {}
18+
19+
export interface BaseListItemHarnessFilters extends BaseHarnessFilters {
20+
text?: string | RegExp;
21+
}
22+
23+
export interface ListItemHarnessFilters extends BaseListItemHarnessFilters {}
24+
25+
export interface ActionListItemHarnessFilters extends BaseListItemHarnessFilters {}
26+
27+
export interface NavListItemHarnessFilters extends BaseListItemHarnessFilters {
28+
href?: string | RegExp | null;
29+
}
30+
31+
export interface ListOptionHarnessFilters extends BaseListItemHarnessFilters {
32+
selected?: boolean;
33+
}
34+
35+
export interface SubheaderHarnessFilters extends BaseHarnessFilters {
36+
text?: string | RegExp;
37+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {MatDividerHarness} from '@angular/material/divider/testing';
2+
import {MatListModule} from '@angular/material/list';
3+
import {
4+
MatActionListHarness,
5+
MatListHarness,
6+
MatNavListHarness,
7+
MatSelectionListHarness
8+
} from './list-harness';
9+
import {MatListItemHarnessBase, MatSubheaderHarness} from './list-item-harness';
10+
import {runHarnessTests} from './shared.spec';
11+
12+
describe('Non-MDC-based list harnesses', () => {
13+
runHarnessTests(
14+
MatListModule, MatListHarness, MatActionListHarness, MatNavListHarness,
15+
MatSelectionListHarness, MatListItemHarnessBase, MatSubheaderHarness, MatDividerHarness);
16+
});

0 commit comments

Comments
 (0)