Skip to content

Commit 94bd52f

Browse files
committed
only add visible nodes to structured text
1 parent 7b136cb commit 94bd52f

File tree

3 files changed

+62
-15
lines changed

3 files changed

+62
-15
lines changed

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

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,60 @@ export function runHarnessTests(
100100
it ('should correctly get tree structure', async () => {
101101
const trees = await loader.getAllHarnesses(treeHarness);
102102
const flatTree = trees[0];
103-
expect(await flatTree.getStructureText()).toEqual(
103+
104+
expect(await flatTree.getStructuredText()).toEqual(
105+
`Flat Group 1
106+
Flat Group 2`);
107+
108+
const firstGroup = (await flatTree.getNodes({text: /Flat Group 1/}))[0];
109+
await firstGroup.expand();
110+
111+
expect(await flatTree.getStructuredText()).toEqual(
104112
`Flat Group 1
113+
\tFlat Leaf 1.1
114+
\tFlat Leaf 1.2
115+
\tFlat Leaf 1.3
105116
Flat Group 2`);
117+
118+
const secondGroup = (await flatTree.getNodes({text: /Flat Group 2/}))[0];
119+
await secondGroup.expand();
120+
121+
expect(await flatTree.getStructuredText()).toEqual(
122+
`Flat Group 1
123+
\tFlat Leaf 1.1
124+
\tFlat Leaf 1.2
125+
\tFlat Leaf 1.3
126+
Flat Group 2
127+
\tFlat Group 2.1`);
106128
});
107129

108130
it('should correctly get tree structure', async () => {
109131
const trees = await loader.getAllHarnesses(treeHarness);
110132
const nestedTree = trees[1];
111-
expect(await nestedTree.getStructureText()).toEqual(
133+
expect(await nestedTree.getStructuredText()).toEqual(
134+
`Nested Group 1
135+
Nested Group 2`);
136+
137+
const firstGroup = (await nestedTree.getNodes({text: /Nested Group 1/}))[0];
138+
await firstGroup.expand();
139+
140+
expect(await nestedTree.getStructuredText()).toEqual(
141+
`Nested Group 1
142+
\tNested Leaf 1.1
143+
\tNested Leaf 1.2
144+
\tNested Leaf 1.3
145+
Nested Group 2`);
146+
147+
const secondGroup = (await nestedTree.getNodes({text: /Nested Group 2/}))[0];
148+
await secondGroup.expand();
149+
150+
expect(await nestedTree.getStructuredText()).toEqual(
112151
`Nested Group 1
113152
\tNested Leaf 1.1
114153
\tNested Leaf 1.2
115154
\tNested Leaf 1.3
116155
Nested Group 2
117-
\tNested Group 2.1
118-
\t\tNested Leaf 2.1.1
119-
\t\tNested Leaf 2.1.2`);
156+
\tNested Group 2.1`);
120157
});
121158
}
122159

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ export class MatTreeHarness extends ComponentHarness {
3030
}
3131

3232
/**
33-
* String representation of the tree structure.
33+
* String representation of the visible tree structure.
34+
* If a node is under an unexpanded node it will not be shown here.
3435
* Eg.
35-
* Tree:
36+
* Tree (all nodes expanded):
3637
* `
3738
* <mat-tree>
3839
* <mat-tree-node>Node 1<mat-tree-node>
@@ -57,17 +58,26 @@ export class MatTreeHarness extends ComponentHarness {
5758
* Node 2.1.1
5859
* Node 2.2
5960
*/
60-
async getStructureText(): Promise<string> {
61+
async getStructuredText(): Promise<string> {
6162
let treeString = '';
6263
const nodes = await this.getNodes();
63-
const levelsAndText = await Promise.all(nodes.map(node => {
64-
return Promise.all([node.getLevel(), node.getText()]);
64+
const nodeInformation = await Promise.all(nodes.map(node => {
65+
return Promise.all([node.getLevel(), node.getText(), node.isExpanded()]);
6566
}));
67+
// initialize as true because the root level nodes are always visible
68+
let previousExpanded = true;
69+
// initialize as 0 because the root essentially has level 0 even though the tree itself has
70+
// no level
71+
let previousLevel = 0;
6672
for (let i = 0; i < nodes.length; i++) {
67-
const [level, text] = levelsAndText[i];
68-
treeString += i === 0 ? '' : '\n';
69-
treeString += '\t'.repeat(level - 1);
70-
treeString += text;
73+
const [level, text, expanded] = nodeInformation[i];
74+
if (previousExpanded || level <= previousLevel) {
75+
treeString += i === 0 ? '' : '\n';
76+
treeString += '\t'.repeat(level - 1);
77+
treeString += text;
78+
previousLevel = level;
79+
previousExpanded = expanded;
80+
}
7181
}
7282
return treeString;
7383
}

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

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

0 commit comments

Comments
 (0)