Skip to content

fix(tree): handle null children in nested tree #14547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/cdk/tree/control/nested-tree-control.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {fakeAsync, flush} from '@angular/core/testing';
import {of as observableOf} from 'rxjs';
import {NestedTreeControl} from './nested-tree-control';

Expand Down Expand Up @@ -92,6 +93,20 @@ describe('CdkNestedTreeControl', () => {
.toBe(totalNumber, `Expect ${totalNumber} expanded nodes`);
});

// Note that this needs to be `fakeAsync` in order to
// catch the error inside an observable correctly.
it('should handle null children', fakeAsync(() => {
const nodes = generateData(3, 2);

nodes[1].children = null!;
treeControl.dataNodes = nodes;

expect(() => {
treeControl.expandAll();
flush();
}).not.toThrow();
}));

describe('with children array', () => {
let getStaticChildren = (node: TestData) => node.children;

Expand Down Expand Up @@ -201,12 +216,12 @@ export class TestData {

function generateData(dataLength: number, childLength: number, grandChildLength: number = 0)
: TestData[] {
let data = <any>[];
let data: TestData[] = [];
let nextIndex = 0;
for (let i = 0; i < dataLength; i++) {
let children = <any>[];
let children: TestData[] = [];
for (let j = 0; j < childLength; j++) {
let grandChildren = <any>[];
let grandChildren: TestData[] = [];
for (let k = 0; k < grandChildLength; k++) {
grandChildren.push(new TestData(`a_${nextIndex}`, `b_${nextIndex}`, `c_${nextIndex++}`, 3));
}
Expand Down
4 changes: 2 additions & 2 deletions src/cdk/tree/control/nested-tree-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Observable} from 'rxjs';
import {take} from 'rxjs/operators';
import {take, filter} from 'rxjs/operators';
import {BaseTreeControl} from './base-tree-control';

/** Nested tree control. Able to expand/collapse a subtree recursively for NestedNode type. */
Expand Down Expand Up @@ -46,7 +46,7 @@ export class NestedTreeControl<T> extends BaseTreeControl<T> {
if (Array.isArray(childrenNodes)) {
childrenNodes.forEach((child: T) => this._getDescendants(descendants, child));
} else if (childrenNodes instanceof Observable) {
childrenNodes.pipe(take(1)).subscribe(children => {
childrenNodes.pipe(take(1), filter(Boolean)).subscribe(children => {
children.forEach((child: T) => this._getDescendants(descendants, child));
});
}
Expand Down