diff --git a/modules/@angular/router/src/utils/tree.ts b/modules/@angular/router/src/utils/tree.ts index ad49901e7a..e325d3c7ae 100644 --- a/modules/@angular/router/src/utils/tree.ts +++ b/modules/@angular/router/src/utils/tree.ts @@ -71,7 +71,7 @@ function findPath(expected: T, c: TreeNode, collected: TreeNode[]): Tre for (let cc of c.children) { const cloned = collected.slice(0); const r = findPath(expected, cc, cloned); - if (r) return r; + if (r.length > 0) return r; } return []; diff --git a/modules/@angular/router/test/utils/tree.spec.ts b/modules/@angular/router/test/utils/tree.spec.ts index 311ddccdc5..5534175733 100644 --- a/modules/@angular/router/test/utils/tree.spec.ts +++ b/modules/@angular/router/test/utils/tree.spec.ts @@ -20,6 +20,13 @@ describe('tree', () => { expect(t.parent(2)).toEqual(1); }); + it('should return the parent of a node (second child)', () => { + const t = new Tree( + new TreeNode(1, [new TreeNode(2, []), new TreeNode(3, [])])); + expect(t.parent(1)).toEqual(null); + expect(t.parent(3)).toEqual(1); + }); + it('should return the children of a node', () => { const t = new Tree(new TreeNode(1, [new TreeNode(2, [])])); expect(t.children(1)).toEqual([2]);