Skip to content

Commit ecc6d38

Browse files
committed
feat(tree/testing): add getTreeStructure for tree harness
1 parent f0f9f90 commit ecc6d38

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,28 @@ export function runHarnessTests(
9696
// no-op if already collapsed
9797
expect(await firstGroup.isExpanded()).toBe(false);
9898
});
99+
100+
it ('should correctly get tree structure', async () => {
101+
const trees = await loader.getAllHarnesses(treeHarness);
102+
const flatTree = trees[0];
103+
expect(await flatTree.getTreeStructure()).toEqual(
104+
`Flat Group 1
105+
Flat Group 2`);
106+
});
107+
108+
it('should correctly get tree structure', async () => {
109+
const trees = await loader.getAllHarnesses(treeHarness);
110+
const nestedTree = trees[1];
111+
expect(await nestedTree.getTreeStructure()).toEqual(
112+
`Nested Group 1
113+
\tNested Leaf 1.1
114+
\tNested Leaf 1.2
115+
\tNested Leaf 1.3
116+
Nested Group 2
117+
\tNested Group 2.1
118+
\t\tNested Leaf 2.1.1
119+
\t\tNested Leaf 2.1.2`);
120+
});
99121
}
100122

101123
interface FoodNode {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,17 @@ export class MatTreeHarness extends ComponentHarness {
2828
async getNodes(filter: TreeNodeHarnessFilters = {}): Promise<MatTreeNodeHarness[]> {
2929
return this.locatorForAll(MatTreeNodeHarness.with(filter))();
3030
}
31+
32+
async getTreeStructure(): Promise<string> {
33+
let treeString = '';
34+
const nodes = await this.getNodes();
35+
for (let i = 0; i < nodes.length; i++) {
36+
treeString += i === 0 ? '' : '\n';
37+
const node = nodes[i];
38+
const level = await node.getLevel();
39+
treeString += '\t'.repeat(level - 1);
40+
treeString += await node.getText();
41+
}
42+
return treeString;
43+
}
3144
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export declare class MatTreeHarness extends ComponentHarness {
22
getNodes(filter?: TreeNodeHarnessFilters): Promise<MatTreeNodeHarness[]>;
3+
getTreeStructure(): Promise<string>;
34
static hostSelector: string;
45
static with(options?: TreeHarnessFilters): HarnessPredicate<MatTreeHarness>;
56
}

0 commit comments

Comments
 (0)