From 2d49e10da49f63e795166d0b7e987df5f2460c4a Mon Sep 17 00:00:00 2001 From: Dan Bucholtz Date: Fri, 14 Jul 2017 10:40:04 -0500 Subject: [PATCH] fix(app): restore getActiveNav api restore getActiveNav api --- src/components/app/app.ts | 27 +++- src/components/app/test/app.spec.ts | 184 +++++++++++++++++++++++++++- 2 files changed, 205 insertions(+), 6 deletions(-) diff --git a/src/components/app/app.ts b/src/components/app/app.ts index b004c95bd2a..733e8cf1e86 100644 --- a/src/components/app/app.ts +++ b/src/components/app/app.ts @@ -206,9 +206,21 @@ export class App { } /** - * @return {NavController} Returns the active NavController. Using this method is preferred when we need access to the top-level navigation controller while on the outside views and handlers like `registerBackButtonAction()` + * @return {NavController} Returns the first Active Nav Controller from the list. This method is deprecated */ - getActiveNavs(navId?: string): NavControllerBase[] { + getActiveNav(): NavControllerBase { + console.warn('(getActiveNav) is deprecated and will be removed in the next major release. Use getActiveNavs instead.'); + const navs = this.getActiveNavs(); + if (navs && navs.length) { + return navs[0]; + } + return null; + } + + /** + * @return {NavController[]} Returns the active NavControllers. Using this method is preferred when we need access to the top-level navigation controller while on the outside views and handlers like `registerBackButtonAction()` + */ + getActiveNavs(rootNavId?: string): NavControllerBase[] { const portal = this._appRoot._getPortal(Constants.PORTAL_MODAL); if (portal.length() > 0) { return findTopNavs(portal); @@ -219,7 +231,16 @@ export class App { if (this._rootNavs.size === 1) { return findTopNavs(this._rootNavs.values().next().value); } - return findTopNavs(this.getRootNavById(navId)); + if (rootNavId) { + return findTopNavs(this._rootNavs.get(rootNavId)); + } + // fallback to just using all root names + let activeNavs: NavigationContainer[] = []; + this._rootNavs.forEach(nav => { + const topNavs = findTopNavs(nav); + activeNavs = activeNavs.concat(topNavs); + }); + return activeNavs; } getRootNav(): any { diff --git a/src/components/app/test/app.spec.ts b/src/components/app/test/app.spec.ts index d8c0b281cff..02d5116d524 100644 --- a/src/components/app/test/app.spec.ts +++ b/src/components/app/test/app.spec.ts @@ -312,7 +312,7 @@ describe('App', () => { }); }); - describe('getActiveNav', () => { + describe('getActiveNavs', () => { it('should get active NavController when using tabs with nested nav', () => { const nav = mockNavController(); @@ -340,6 +340,11 @@ describe('App', () => { expect(activeNavs.length).toEqual(2); expect(activeNavs[0]).toEqual(nav2); expect(activeNavs[1]).toEqual(nav3); + + const activeNavsTwo = app.getActiveNavs(); + expect(activeNavsTwo.length).toEqual(2); + expect(activeNavsTwo[0]).toEqual(nav2); + expect(activeNavsTwo[1]).toEqual(nav3); }); it('should get active NavController when using tabs, nested in a root nav', () => { @@ -355,10 +360,12 @@ describe('App', () => { tab2.setSelected(true); expect(app.getActiveNavs(nav.id)[0]).toBe(tab2); + expect(app.getActiveNavs()[0]).toBe(tab2); tab2.setSelected(false); tab3.setSelected(true); expect(app.getActiveNavs(nav.id)[0]).toBe(tab3); + expect(app.getActiveNavs()[0]).toBe(tab3); }); it('should get active tab NavController when using tabs, and tabs is the root', () => { @@ -371,10 +378,12 @@ describe('App', () => { tab2.setSelected(true); expect(app.getActiveNavs(tabs.id)[0]).toBe(tab2); + expect(app.getActiveNavs()[0]).toBe(tab2); tab2.setSelected(false); tab3.setSelected(true); expect(app.getActiveNavs(tabs.id)[0]).toBe(tab3); + expect(app.getActiveNavs()[0]).toBe(tab3); }); it('should get active NavController when nested 3 deep', () => { @@ -387,6 +396,8 @@ describe('App', () => { nav2.registerChildNav(nav3); expect(app.getActiveNavs(nav1.id)[0]).toBe(nav3); + expect(app.getActiveNavs()[0]).toBe(nav3); + expect(app.getActiveNavs().length).toBe(1); }); it('should get active NavController when nested 2 deep', () => { @@ -399,12 +410,15 @@ describe('App', () => { const activeNav = app.getActiveNavs(nav1.id)[0]; expect(activeNav).toBe(nav2); + + expect(app.getActiveNavs()[0]).toBe(nav2); }); it('should get active NavController when only one nav controller', () => { const nav = mockNavController(); app.registerRootNav(nav); expect(app.getActiveNavs(nav.id)[0]).toBe(nav); + expect(app.getActiveNavs()[0]).toBe(nav); }); it('should set/get the root nav controller', () => { @@ -414,9 +428,9 @@ describe('App', () => { }); it('should not get an active NavController if there is not root set', () => { - const activeNav = app.getActiveNavs(''); + const activeNavs = app.getActiveNavs(); const rootNav = app.getRootNavById(''); - expect(activeNav.length).toEqual(0); + expect(activeNavs.length).toEqual(0); expect(rootNav).toBeFalsy(); }); @@ -438,6 +452,9 @@ describe('App', () => { expect(activeNavOne).toBe(childNavOne); expect(activeNavTwo).toBe(childNavTwo); + + expect(app.getActiveNavs()[0]).toBe(childNavOne); + expect(app.getActiveNavs()[1]).toBe(childNavTwo); }); it('should get the active nav when no id is provided assuming there is one nav', () => { @@ -451,6 +468,167 @@ describe('App', () => { expect(result).toEqual(childNavOne); }); + + it('should return the all the active navs when there is not an id passed', () => { + const rootNavOne = mockNavController(); + app.registerRootNav(rootNavOne); + + const rootNavTwo = mockNavController(); + app.registerRootNav(rootNavTwo); + + const childNavOne = mockNavController(); + rootNavOne.registerChildNav(childNavOne); + + const childChildNavOne = mockNavController(); + childNavOne.registerChildNav(childChildNavOne); + + const childNavTwo = mockNavController(); + rootNavTwo.registerChildNav(childNavTwo); + + const childChildNavTwo = mockNavController(); + childNavTwo.registerChildNav(childChildNavTwo); + + const results = app.getActiveNavs(); + expect(results.length).toEqual(2); + expect(results[0]).toEqual(childChildNavOne); + expect(results[1]).toEqual(childChildNavTwo); + + const withIdResultsOne = app.getActiveNavs(rootNavOne.id); + expect(withIdResultsOne.length).toEqual(1); + expect(withIdResultsOne[0]).toEqual(childChildNavOne); + + const withIdResultsTwo = app.getActiveNavs(rootNavTwo.id); + expect(withIdResultsTwo.length).toEqual(1); + expect(withIdResultsTwo[0]).toEqual(childChildNavTwo); + }); + }); + + describe('getActiveNav', () => { + it('should get active NavController when using tabs with nested nav', () => { + const nav = mockNavController(); + app.registerRootNav(nav); + + const tabs = mockTabs(); + const tab1 = mockTab(tabs); + const tab2 = mockTab(tabs); + nav.registerChildNav(tabs); + + tab2.setSelected(true); + const nav2 = mockNavController(); + nav2.name = 'nav2'; + const nav3 = mockNavController(); + nav3.name = 'nav3'; + const nav4 = mockNavController(); + nav4.name = 'nav4'; + + tab1.registerChildNav(nav4); + // tab 2 registers two child navs!! + tab2.registerChildNav(nav2); + tab2.registerChildNav(nav3); + + const activeNav = app.getActiveNav(); + expect(activeNav).toEqual(nav2); + }); + + it('should get active NavController when using tabs, nested in a root nav', () => { + const nav = mockNavController(); + app.registerRootNav(nav); + + const tabs = mockTabs(); + mockTab(tabs); + const tab2 = mockTab(tabs); + const tab3 = mockTab(tabs); + nav.registerChildNav(tabs); + + tab2.setSelected(true); + + expect(app.getActiveNav()).toBe(tab2); + + tab2.setSelected(false); + tab3.setSelected(true); + expect(app.getActiveNav()).toBe(tab3); + }); + + it('should get active tab NavController when using tabs, and tabs is the root', () => { + const tabs = mockTabs(); + mockTab(tabs); + const tab2 = mockTab(tabs); + const tab3 = mockTab(tabs); + app.registerRootNav(tabs); + + tab2.setSelected(true); + + expect(app.getActiveNav()).toBe(tab2); + + tab2.setSelected(false); + tab3.setSelected(true); + expect(app.getActiveNav()).toBe(tab3); + }); + + it('should get active NavController when nested 3 deep', () => { + const nav1 = mockNavController(); + const nav2 = mockNavController(); + const nav3 = mockNavController(); + app.registerRootNav(nav1); + + nav1.registerChildNav(nav2); + nav2.registerChildNav(nav3); + + expect(app.getActiveNav()).toBe(nav3); + }); + + it('should get active NavController when nested 2 deep', () => { + const nav1 = mockNavController(); + const nav2 = mockNavController(); + app.registerRootNav(nav1); + + nav1.registerChildNav(nav2); + + const activeNav = app.getActiveNav(); + + expect(activeNav).toBe(nav2); + }); + + it('should get active NavController when only one nav controller', () => { + const nav = mockNavController(); + app.registerRootNav(nav); + expect(app.getActiveNav()).toBe(nav); + }); + + it('should not get an active NavController if there is not root set', () => { + const activeNav = app.getActiveNav(); + expect(activeNav).toBeFalsy(); + }); + + it('should just work when there are multiple active navs', () => { + const rootNavOne = mockNavController(); + const rootNavTwo = mockNavController(); + + app.registerRootNav(rootNavOne); + app.registerRootNav(rootNavTwo); + + const childNavOne = mockNavController(); + const childNavTwo = mockNavController(); + + rootNavOne.registerChildNav(childNavOne); + rootNavTwo.registerChildNav(childNavTwo); + + const activeNavOne = app.getActiveNav(); + + expect(activeNavOne).toBe(childNavOne); + }); + + it('should get the active nav when no id is provided assuming there is one nav', () => { + const rootNavOne = mockNavController(); + app.registerRootNav(rootNavOne); + + const childNavOne = mockNavController(); + rootNavOne.registerChildNav(childNavOne); + + const result = app.getActiveNav(); + + expect(result).toEqual(childNavOne); + }); }); describe('getRootNavs', () => {