Skip to content

Commit 8ed1c45

Browse files
committed
feat(aria/tree): add test harnesses
Sets up test harnesses for the Aria tree module.
1 parent 754b682 commit 8ed1c45

File tree

9 files changed

+544
-0
lines changed

9 files changed

+544
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## API Report File for "@angular/aria_tree_testing"
2+
3+
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
4+
5+
```ts
6+
7+
import { BaseHarnessFilters } from '@angular/cdk/testing';
8+
import { ComponentHarness } from '@angular/cdk/testing';
9+
import { ContentContainerComponentHarness } from '@angular/cdk/testing';
10+
import { HarnessPredicate } from '@angular/cdk/testing';
11+
12+
// @public (undocumented)
13+
export interface TextTree {
14+
// (undocumented)
15+
children?: TextTree[];
16+
// (undocumented)
17+
text?: string;
18+
}
19+
20+
// @public
21+
export class TreeHarness extends ComponentHarness {
22+
getItems(filter?: TreeItemHarnessFilters): Promise<TreeItemHarness[]>;
23+
getTreeStructure(): Promise<TextTree>;
24+
// (undocumented)
25+
static hostSelector: string;
26+
static with(options?: TreeHarnessFilters): HarnessPredicate<TreeHarness>;
27+
}
28+
29+
// @public
30+
export interface TreeHarnessFilters extends BaseHarnessFilters {
31+
}
32+
33+
// @public
34+
export class TreeItemHarness extends ContentContainerComponentHarness<string> {
35+
click(): Promise<void>;
36+
getLevel(): Promise<number>;
37+
getText(): Promise<string>;
38+
// (undocumented)
39+
static hostSelector: string;
40+
isDisabled(): Promise<boolean>;
41+
isExpanded(): Promise<boolean>;
42+
isSelected(): Promise<boolean>;
43+
static with(options?: TreeItemHarnessFilters): HarnessPredicate<TreeItemHarness>;
44+
}
45+
46+
// @public
47+
export interface TreeItemHarnessFilters extends BaseHarnessFilters {
48+
disabled?: boolean;
49+
expanded?: boolean;
50+
level?: number;
51+
selected?: boolean;
52+
text?: string | RegExp;
53+
}
54+
55+
// (No @packageDocumentation comment for this package)
56+
57+
```

src/aria/config.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ ARIA_ENTRYPOINTS = [
88
"tabs",
99
"toolbar",
1010
"tree",
11+
"tree/testing",
1112
"private",
1213
]
1314

src/aria/tree/testing/BUILD.bazel

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
load("//tools:defaults.bzl", "ng_project", "ng_web_test_suite", "ts_project")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
ts_project(
6+
name = "testing",
7+
srcs = glob(
8+
["**/*.ts"],
9+
exclude = ["**/*.spec.ts"],
10+
),
11+
deps = [
12+
"//src/cdk/testing",
13+
],
14+
)
15+
16+
filegroup(
17+
name = "source-files",
18+
srcs = glob(["**/*.ts"]),
19+
)
20+
21+
ng_project(
22+
name = "unit_tests_lib",
23+
testonly = True,
24+
srcs = glob(["**/*.spec.ts"]),
25+
deps = [
26+
":testing",
27+
"//:node_modules/@angular/common",
28+
"//:node_modules/@angular/core",
29+
"//src/aria/tree",
30+
"//src/cdk/testing",
31+
"//src/cdk/testing/testbed",
32+
],
33+
)
34+
35+
ng_web_test_suite(
36+
name = "unit_tests",
37+
deps = [":unit_tests_lib"],
38+
)

src/aria/tree/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.dev/license
7+
*/
8+
9+
export * from './public-api';
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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.dev/license
7+
*/
8+
9+
import {ContentContainerComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
10+
import {TreeItemHarnessFilters} from './tree-harness-filters';
11+
12+
/** Harness for interacting with an Aria tree item. */
13+
export class TreeItemHarness extends ContentContainerComponentHarness<string> {
14+
static hostSelector = '[ngTreeItem]';
15+
16+
/**
17+
* Gets a `HarnessPredicate` that can be used to search for a tree item with specific attributes.
18+
* @param options Options for narrowing the search
19+
* @return a `HarnessPredicate` configured with the given options.
20+
*/
21+
static with(options: TreeItemHarnessFilters = {}): HarnessPredicate<TreeItemHarness> {
22+
return new HarnessPredicate(TreeItemHarness, options)
23+
.addOption('text', options.text, (harness, text) =>
24+
HarnessPredicate.stringMatches(harness.getText(), text),
25+
)
26+
.addOption(
27+
'disabled',
28+
options.disabled,
29+
async (harness, disabled) => (await harness.isDisabled()) === disabled,
30+
)
31+
.addOption(
32+
'expanded',
33+
options.expanded,
34+
async (harness, expanded) => (await harness.isExpanded()) === expanded,
35+
)
36+
.addOption(
37+
'selected',
38+
options.selected,
39+
async (harness, selected) => (await harness.isSelected()) === selected,
40+
)
41+
.addOption(
42+
'level',
43+
options.level,
44+
async (harness, level) => (await harness.getLevel()) === level,
45+
);
46+
}
47+
48+
/** Whether the tree item is expanded. */
49+
async isExpanded(): Promise<boolean> {
50+
return (await this._getHostAttribute('aria-expanded')) === 'true';
51+
}
52+
53+
/** Whether the tree item is disabled. */
54+
async isDisabled(): Promise<boolean> {
55+
return (await this._getHostAttribute('aria-disabled')) === 'true';
56+
}
57+
58+
/** Whether the tree item is selected. */
59+
async isSelected(): Promise<boolean> {
60+
return (await this._getHostAttribute('aria-selected')) === 'true';
61+
}
62+
63+
/** Gets the level of the tree item. Note that this gets the aria-level and is 1 indexed. */
64+
async getLevel(): Promise<number> {
65+
const level = (await this._getHostAttribute('aria-level')) ?? '1';
66+
return parseInt(level);
67+
}
68+
69+
/** Gets the tree item's text. */
70+
async getText(): Promise<string> {
71+
return (await this.host()).text({exclude: '[ngTreeItem], [ngTreeItemGroup]'});
72+
}
73+
74+
/** Clicks the tree item. */
75+
async click(): Promise<void> {
76+
return (await this.host()).click();
77+
}
78+
79+
private async _getHostAttribute(attributeName: string): Promise<string | null> {
80+
return (await this.host()).getAttribute(attributeName);
81+
}
82+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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.dev/license
7+
*/
8+
9+
export * from './item-harness';
10+
export * from './tree-harness';
11+
export * from './tree-harness-filters';
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.dev/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 tree harness instances */
12+
export interface TreeHarnessFilters extends BaseHarnessFilters {}
13+
14+
/** A set of criteria that can be used to filter a list of tree item harness instances. */
15+
export interface TreeItemHarnessFilters extends BaseHarnessFilters {
16+
/** Only find instances whose text matches the given value. */
17+
text?: string | RegExp;
18+
19+
/** Only find instances whose disabled state matches the given value. */
20+
disabled?: boolean;
21+
22+
/** Only find instances whose expansion state matches the given value. */
23+
expanded?: boolean;
24+
25+
/** Only find instances whose selection state matches the given value. */
26+
selected?: boolean;
27+
28+
/** Only find instances whose level matches the given value. */
29+
level?: number;
30+
}

0 commit comments

Comments
 (0)