Skip to content

Commit d8102de

Browse files
committed
different geText for nested node
separte out toggle method, change role to isLeaf
1 parent d55a5bd commit d8102de

File tree

5 files changed

+34
-13
lines changed

5 files changed

+34
-13
lines changed

src/material/tree/testing/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ ts_library(
1010
),
1111
module_name = "@angular/material/tree/testing",
1212
deps = [
13-
"//src/cdk/testing",
1413
"//src/cdk/coercion",
14+
"//src/cdk/testing",
1515
],
1616
)
1717

@@ -27,8 +27,8 @@ ng_test_library(
2727
":testing",
2828
"//src/cdk/testing",
2929
"//src/cdk/testing/testbed",
30-
"//src/material/tree",
3130
"//src/cdk/tree",
31+
"//src/material/tree",
3232
],
3333
)
3434

src/material/tree/testing/node-harness.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,44 @@ export class MatTreeNodeHarness extends ComponentHarness {
4545
return coerceNumberProperty(await (await this.host()).getAttribute('aria-level'));
4646
}
4747

48-
/** Gets the role of the tree node. 'group' or 'treeitem' */
49-
async getRole(): Promise<string|null> {
50-
return (await this.host()).getAttribute('role');
48+
/** Whether the node is a leaf node. */
49+
async isLeaf(): Promise<boolean> {
50+
const role = await (await this.host()).getAttribute('role');
51+
if (role === 'group') {
52+
return false;
53+
} else if (role === 'treeitem') {
54+
return true
55+
} else {
56+
throw new Error('Invalid node role');
57+
}
5158
}
5259

5360
/** Gets the tree node's text. */
5461
async getText(): Promise<string> {
5562
return (await this.host()).text();
5663
}
5764

58-
/** Expands/collapses the node by clicking on the toggle. Only works when node is not disabled. */
59-
async toggleExpansion(): Promise<void> {
65+
/** Toggles node between expanded/collapsed. Only works when node is not disabled. */
66+
async toggle(): Promise<void> {
6067
const toggle = await this._toggle();
6168
if (toggle) {
6269
return toggle.click();
6370
}
6471
}
72+
73+
/** Expands the node if it is collapsed. Only works when node is not disabled. */
74+
async expand(): Promise<void> {
75+
if (!(await this.isExpanded())) {
76+
await this.toggle();
77+
}
78+
}
79+
80+
/** Collapses the node if it is expanded. Only works when node is not disabled. */
81+
async collapse(): Promise<void> {
82+
if (await this.isExpanded()) {
83+
await this.toggle();
84+
}
85+
}
6586
}
6687

6788
/** Harness for interacting with a standard Angular Material nested tree node. */
@@ -97,6 +118,6 @@ function getNodePredicate<T extends MatTreeNodeHarness>(
97118
'level', options.level,
98119
async (harness, level) => (await harness.getLevel()) === level)
99120
.addOption(
100-
'role', options.role,
101-
async (harness, role) => (await harness.getRole()) === role);
121+
'leaf', options.leaf,
122+
async (harness, leaf) => (await harness.isLeaf()) === leaf);
102123
}

src/material/tree/testing/shared.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function runHarnessTests(
5757
expect(flatTreeNodes.length).toBe(1);
5858
const secondGroup = flatTreeNodes[0];
5959

60-
expect(await secondGroup.getRole()).toBe('group');
60+
expect(await secondGroup.isLeaf()).toBe(false);
6161
expect(await secondGroup.getText()).toBe('Toggle Flat Group 2');
6262
expect(await secondGroup.getLevel()).toBe(0);
6363
expect(await secondGroup.isDisabled()).toBe(false);
@@ -72,7 +72,7 @@ export function runHarnessTests(
7272

7373
expect(await firstGroup.isExpanded()).toBe(false);
7474

75-
await firstGroup.toggleExpansion();
75+
await firstGroup.expand();
7676

7777
expect(await firstGroup.isExpanded()).toBe(true);
7878
});

src/material/tree/testing/tree-harness-filters.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ export interface TreeNodeHarnessFilters extends BaseHarnessFilters {
1717
/** Only find instances whose text matches the given value. */
1818
text?: string | RegExp;
1919

20-
/** Only find instances whose state matches the given value. */
20+
/** Only find instances whose disabled state matches the given value. */
2121
disabled?: boolean;
2222

2323
/** Only find instances whose expansion state matches the given value. */
2424
expanded?: boolean;
2525

2626
/** Only find instances whose role matches the given value. */
27-
role?: 'treeitem'|'group';
27+
leaf?: boolean;
2828

2929
/** Only find instances whose level matches the given value. */
3030
level?: number;

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

Whitespace-only changes.

0 commit comments

Comments
 (0)