Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Fixed a bug preventing navigation via omnibar due to loading outside Angular #332

Merged
merged 2 commits into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
53 changes: 53 additions & 0 deletions src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import { HelpInitializationService } from '@blackbaud/skyux-lib-help';

import {
BBOmnibar,
BBOmnibarConfig,
BBOmnibarLegacy,
BBOmnibarNavigationItem,
BBOmnibarSearchArgs
} from '@blackbaud/auth-client';

Expand Down Expand Up @@ -290,6 +292,57 @@ describe('AppComponent', () => {
})
);

it(
'should run omnibar navigation within the Angular zone',
async(() => {
skyAppConfig.skyux.host.url = 'base.com/';
skyAppConfig.runtime.app.base = 'custom-base/';

let beforeNavCallback: (item: BBOmnibarNavigationItem) => boolean | void;

spyOn(BBOmnibar, 'load').and.callFake((config: BBOmnibarConfig) => {
beforeNavCallback = config.nav.beforeNavCallback;
});

skyAppConfig.skyux.omnibar = {
experimental: true
};

setup(skyAppConfig).then(() => {
const zone = fixture.debugElement.injector.get(NgZone);
const router = fixture.debugElement.injector.get(Router);

const navigateByUrlSpy = spyOn(router, 'navigateByUrl');

let zoneRunCallback: Function;

const runSpy = spyOn(zone, 'run').and.callFake(
(cb: Function) => {
if (cb && cb.toString().indexOf('navigateByUrl') >= 0) {
zoneRunCallback = cb;
} else {
cb();
}
}
);

fixture.detectChanges();

beforeNavCallback({
title: '',
url: 'base.com/custom-base/new-place'
});

expect(runSpy).toHaveBeenCalled();
expect(navigateByUrlSpy).not.toHaveBeenCalled();

zoneRunCallback();

expect(navigateByUrlSpy).toHaveBeenCalled();
});
})
);

it('should set the onSearch property if a search provider is provided', async(() => {
let spyOmnibar = spyOn(BBOmnibar, 'load');

Expand Down
9 changes: 8 additions & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,18 @@ export class AppComponent implements OnInit {
}

nav.beforeNavCallback = (item: BBOmnibarNavigationItem) => {
console.log(baseUrl);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug remnants?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

const url = item.url.toLowerCase();

if (url.indexOf(baseUrl) === 0) {
const routePath = item.url.substring(baseUrl.length, url.length);
this.router.navigateByUrl(routePath);

// Since the omnibar is loaded outside Angular, navigating needs to be explicitly
// run inside the Angular zone in order for navigation to work properly.
this.zone.run(() => {
this.router.navigateByUrl(routePath);
});

return false;
}

Expand Down