import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
+import { Title } from '@angular/platform-browser';
import { Router, Routes } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
let component: BreadcrumbsComponent;
let fixture: ComponentFixture<BreadcrumbsComponent>;
let router: Router;
+ let titleService: Title;
@Component({ selector: 'cd-fake', template: '' })
class FakeComponent {}
]
}
]
+ },
+ {
+ path: 'error',
+ component: FakeComponent,
+ data: { breadcrumbs: '' }
}
];
beforeEach(() => {
fixture = TestBed.createComponent(BreadcrumbsComponent);
router = TestBed.inject(Router);
+ titleService = TestBed.inject(Title);
component = fixture.componentInstance;
fixture.detectChanges();
expect(component.crumbs).toEqual([]);
component.ngOnDestroy();
expect(component.subscription.closed).toBeTruthy();
});
+
+ it('should display no breadcrumbs in page title when navigating to dashboard', fakeAsync(() => {
+ fixture.ngZone.run(() => {
+ router.navigateByUrl('');
+ });
+ tick();
+ expect(titleService.getTitle()).toEqual('Ceph');
+ }));
+
+ it('should display no breadcrumbs in page title when a page is not found', fakeAsync(() => {
+ fixture.ngZone.run(() => {
+ router.navigateByUrl('/error');
+ });
+ tick();
+ expect(titleService.getTitle()).toEqual('Ceph');
+ }));
+
+ it('should display 2 breadcrumbs in page title when navigating to hosts', fakeAsync(() => {
+ fixture.ngZone.run(() => {
+ router.navigateByUrl('/hosts');
+ });
+ tick();
+ expect(titleService.getTitle()).toEqual('Ceph: Cluster > Hosts');
+ }));
+
+ it('should display 3 breadcrumbs in page title when navigating to RBD Add', fakeAsync(() => {
+ fixture.ngZone.run(() => {
+ router.navigateByUrl('/block/rbd/add');
+ });
+ tick();
+ expect(titleService.getTitle()).toEqual('Ceph: Block > Images > Add');
+ }));
});
*/
import { Component, Injector, OnDestroy } from '@angular/core';
+import { Title } from '@angular/platform-browser';
import { ActivatedRouteSnapshot, NavigationEnd, NavigationStart, Router } from '@angular/router';
import { concat, from, Observable, of, Subscription } from 'rxjs';
subscription: Subscription;
private defaultResolver = new BreadcrumbsResolver();
- constructor(private router: Router, private injector: Injector) {
+ constructor(private router: Router, private injector: Injector, private titleService: Title) {
this.subscription = this.router.events
.pipe(filter((x) => x instanceof NavigationStart))
.subscribe(() => {
.subscribe((x) => {
this.finished = true;
this.crumbs = x;
+ const title = this.getTitleFromCrumbs(this.crumbs);
+ this.titleService.setTitle(title);
});
});
}
return of(value as T);
}
+
+ private getTitleFromCrumbs(crumbs: IBreadcrumb[]): string {
+ const currentLocation = crumbs
+ .map((crumb: IBreadcrumb) => {
+ return crumb.text || '';
+ })
+ .join(' > ');
+ if (currentLocation.length > 0) {
+ return `Ceph: ${currentLocation}`;
+ } else {
+ return 'Ceph';
+ }
+ }
}