refactor(app): refactor app slightly to maintain backwards compatibility

This commit is contained in:
Dan Bucholtz
2017-06-20 13:26:02 -05:00
parent 0480f73f8e
commit 58e1d79518
2 changed files with 66 additions and 2 deletions

View File

@@ -208,17 +208,37 @@ 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()`
*/
getActiveNav(navId: string): NavControllerBase {
getActiveNav(navId?: string): NavControllerBase {
const portal = this._appRoot._getPortal(Constants.PORTAL_MODAL);
if (portal.length() > 0) {
return <NavControllerBase> findTopNav(portal);
}
if (!this._rootNavs || !this._rootNavs.size || !this._rootNavs.has(navId)) {
if (!this._rootNavs || !this._rootNavs.size) {
return null;
}
if (this._rootNavs.size === 1) {
return <NavControllerBase> findTopNav(this._rootNavs.values().next().value);
}
return <NavControllerBase> findTopNav(this.getRootNavById(navId));
}
getRootNav(): any {
console.warn('(getRootNav) is deprecated and will be removed in the next major release. Use getRootNavById instead.');
const rootNavs = this.getRootNavs();
if (rootNavs.length === 0) {
return null;
} else if (rootNavs.length > 1) {
console.warn('(getRootNav) there are multiple root navs, use getRootNavs instead');
}
return rootNavs[0];
}
getRootNavs(): any[] {
const navs: NavigationContainer[] = [];
this._rootNavs.forEach(nav => navs.push(nav));
return navs;
}
/**
* @return {NavController} Returns the root NavController
*/
@@ -233,6 +253,7 @@ export class App {
this._rootNavs.set(nav.id, nav);
}
getActiveNavContainers(): NavigationContainer[] {
// for each root nav container, get it's active nav
const list: NavigationContainer[] = [];

View File

@@ -431,6 +431,49 @@ describe('App', () => {
expect(activeNavOne).toBe(childNavOne);
expect(activeNavTwo).toBe(childNavTwo);
});
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', () => {
it('should return an array of navs', () => {
const rootNavOne = mockNavController();
app.registerRootNav(rootNavOne);
const rootNavTwo = mockNavController();
app.registerRootNav(rootNavTwo);
const results = app.getRootNavs();
expect(results.length).toEqual(2);
});
});
describe('getRootNav', () => {
it('should return the single root nav', () => {
const rootNavOne = mockNavController();
app.registerRootNav(rootNavOne);
const result = app.getRootNav();
expect(result).toEqual(rootNavOne);
});
it('should return the first nav in the list for backwards compatibility', () => {
const rootNavOne = mockNavController();
app.registerRootNav(rootNavOne);
const rootNavTwo = mockNavController();
app.registerRootNav(rootNavTwo);
const result = app.getRootNav();
expect(result).toEqual(rootNavOne);
});
});
describe('setEnabled', () => {