Skip to content

Commit

Permalink
test(tree): Add more tests for cdk-tree (#8967)
Browse files Browse the repository at this point in the history
  • Loading branch information
tinayuangao authored and jelbourn committed Feb 12, 2018
1 parent dc3256f commit ecdf8b6
Show file tree
Hide file tree
Showing 6 changed files with 650 additions and 111 deletions.
23 changes: 13 additions & 10 deletions src/cdk/tree/nested-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
QueryList,
} from '@angular/core';
import {takeUntil} from 'rxjs/operators/takeUntil';

import {CdkTree} from './tree';
import {CdkTreeNodeOutlet} from './outlet';
import {CdkTreeNode} from './node';
Expand Down Expand Up @@ -43,9 +44,9 @@ import {CdkTreeNode} from './node';
'class': 'cdk-tree-node cdk-nested-tree-node',
'tabindex': '0',
},
providers: [{provide: CdkTreeNode, useExisting: CdkNestedTreeNode}]
})
export class CdkNestedTreeNode<T> extends CdkTreeNode<T> implements AfterContentInit, OnDestroy {

/** The children data dataNodes of current node. They will be placed in `CdkTreeNodeOutlet`. */
protected _children: T[];

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

ngAfterContentInit() {
this._tree.treeControl.getChildren(this.data).pipe(takeUntil(this._destroyed))
.subscribe(result => {
// In case when nodeOutlet is not in the DOM when children changes, save it in the node
// and add to nodeOutlet when it's available.
this._children = result as T[];
this._addChildrenNodes();
});
.subscribe(result => {
if (result && result.length) {
// In case when nodeOutlet is not in the DOM when children changes, save it in the node
// and add to nodeOutlet when it's available.
this._children = result as T[];
this._addChildrenNodes();
}
});
this.nodeOutlet.changes.pipe(takeUntil(this._destroyed))
.subscribe((_) => this._addChildrenNodes());
.subscribe((_) => this._addChildrenNodes());
}

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

/** Clear the children dataNodes. */
protected _clear(): void {
if (this.nodeOutlet.first) {
if (this.nodeOutlet && this.nodeOutlet.first) {
this.nodeOutlet.first.viewContainer.clear();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cdk/tree/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class CdkTreeNode<T> implements FocusableOption, OnDestroy {
}
this._tree.treeControl.getChildren(this._data).pipe(takeUntil(this._destroyed))
.subscribe(children => {
this.role = children ? 'group' : 'treeitem';
this.role = children && children.length ? 'group' : 'treeitem';
});
}
}
Expand Down
Loading

0 comments on commit ecdf8b6

Please sign in to comment.