Skip to content

Commit 8dbeefd

Browse files
committed
test(tree): Add more tests for cdk-tree (#8967)
1 parent 6bd149a commit 8dbeefd

File tree

6 files changed

+650
-111
lines changed

6 files changed

+650
-111
lines changed

src/cdk/tree/nested-node.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
QueryList,
1515
} from '@angular/core';
1616
import {takeUntil} from 'rxjs/operators/takeUntil';
17+
1718
import {CdkTree} from './tree';
1819
import {CdkTreeNodeOutlet} from './outlet';
1920
import {CdkTreeNode} from './node';
@@ -43,9 +44,9 @@ import {CdkTreeNode} from './node';
4344
'class': 'cdk-tree-node cdk-nested-tree-node',
4445
'tabindex': '0',
4546
},
47+
providers: [{provide: CdkTreeNode, useExisting: CdkNestedTreeNode}]
4648
})
4749
export class CdkNestedTreeNode<T> extends CdkTreeNode<T> implements AfterContentInit, OnDestroy {
48-
4950
/** The children data dataNodes of current node. They will be placed in `CdkTreeNodeOutlet`. */
5051
protected _children: T[];
5152

@@ -59,14 +60,16 @@ export class CdkNestedTreeNode<T> extends CdkTreeNode<T> implements AfterContent
5960

6061
ngAfterContentInit() {
6162
this._tree.treeControl.getChildren(this.data).pipe(takeUntil(this._destroyed))
62-
.subscribe(result => {
63-
// In case when nodeOutlet is not in the DOM when children changes, save it in the node
64-
// and add to nodeOutlet when it's available.
65-
this._children = result as T[];
66-
this._addChildrenNodes();
67-
});
63+
.subscribe(result => {
64+
if (result && result.length) {
65+
// In case when nodeOutlet is not in the DOM when children changes, save it in the node
66+
// and add to nodeOutlet when it's available.
67+
this._children = result as T[];
68+
this._addChildrenNodes();
69+
}
70+
});
6871
this.nodeOutlet.changes.pipe(takeUntil(this._destroyed))
69-
.subscribe((_) => this._addChildrenNodes());
72+
.subscribe((_) => this._addChildrenNodes());
7073
}
7174

7275
ngOnDestroy() {
@@ -78,7 +81,7 @@ export class CdkNestedTreeNode<T> extends CdkTreeNode<T> implements AfterContent
7881
/** Add children dataNodes to the NodeOutlet */
7982
protected _addChildrenNodes(): void {
8083
this._clear();
81-
if (this.nodeOutlet.length && this._children) {
84+
if (this.nodeOutlet.length && this._children && this._children.length) {
8285
this._children.forEach((child, index) => {
8386
this._tree.insertNode(child, index, this.nodeOutlet.first.viewContainer);
8487
});
@@ -87,7 +90,7 @@ export class CdkNestedTreeNode<T> extends CdkTreeNode<T> implements AfterContent
8790

8891
/** Clear the children dataNodes. */
8992
protected _clear(): void {
90-
if (this.nodeOutlet.first) {
93+
if (this.nodeOutlet && this.nodeOutlet.first) {
9194
this.nodeOutlet.first.viewContainer.clear();
9295
}
9396
}

src/cdk/tree/node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export class CdkTreeNode<T> implements FocusableOption, OnDestroy {
119119
}
120120
this._tree.treeControl.getChildren(this._data).pipe(takeUntil(this._destroyed))
121121
.subscribe(children => {
122-
this.role = children ? 'group' : 'treeitem';
122+
this.role = children && children.length ? 'group' : 'treeitem';
123123
});
124124
}
125125
}

0 commit comments

Comments
 (0)