1 import { CommonModule } from '@angular/common';
2 import { Component } from '@angular/core';
3 import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
4 import { Router, Routes } from '@angular/router';
5 import { RouterTestingModule } from '@angular/router/testing';
7 import { PerformanceCounterBreadcrumbsResolver } from '~/app/app-routing.module';
8 import { configureTestBed } from '~/testing/unit-test-helper';
9 import { BreadcrumbsComponent } from './breadcrumbs.component';
11 describe('BreadcrumbsComponent', () => {
12 let component: BreadcrumbsComponent;
13 let fixture: ComponentFixture<BreadcrumbsComponent>;
16 @Component({ selector: 'cd-fake', template: '' })
17 class FakeComponent {}
19 const routes: Routes = [
22 component: FakeComponent,
23 data: { breadcrumbs: 'Cluster/Hosts' }
26 path: 'perf_counters',
27 component: FakeComponent,
29 breadcrumbs: PerformanceCounterBreadcrumbsResolver
34 data: { breadcrumbs: true, text: 'Block', path: null },
38 data: { breadcrumbs: 'Images' },
40 { path: '', component: FakeComponent },
41 { path: 'add', component: FakeComponent, data: { breadcrumbs: 'Add' } }
49 declarations: [BreadcrumbsComponent, FakeComponent],
50 imports: [CommonModule, RouterTestingModule.withRoutes(routes)],
51 providers: [PerformanceCounterBreadcrumbsResolver]
55 fixture = TestBed.createComponent(BreadcrumbsComponent);
56 router = TestBed.inject(Router);
57 component = fixture.componentInstance;
58 fixture.detectChanges();
59 expect(component.crumbs).toEqual([]);
62 it('should create', () => {
63 expect(component).toBeTruthy();
64 expect(component.subscription).toBeDefined();
67 it('should run postProcess and split the breadcrumbs when navigating to hosts', fakeAsync(() => {
68 fixture.ngZone.run(() => {
69 router.navigateByUrl('/hosts');
72 expect(component.crumbs).toEqual([
73 { path: null, text: 'Cluster' },
74 { path: '/hosts', text: 'Hosts' }
78 it('should display empty breadcrumb when navigating to perf_counters from unknown path', fakeAsync(() => {
79 fixture.ngZone.run(() => {
80 router.navigateByUrl('/perf_counters');
83 expect(component.crumbs).toEqual([
84 { path: null, text: 'Cluster' },
85 { path: null, text: '' },
86 { path: '', text: 'Performance Counters' }
90 it('should display Monitor breadcrumb when navigating to perf_counters from Monitors', fakeAsync(() => {
91 fixture.ngZone.run(() => {
92 router.navigate(['/perf_counters'], { queryParams: { fromLink: '/monitor' } });
95 expect(component.crumbs).toEqual([
96 { path: null, text: 'Cluster' },
97 { path: '/monitor', text: 'Monitors' },
98 { path: '', text: 'Performance Counters' }
102 it('should display Hosts breadcrumb when navigating to perf_counters from Hosts', fakeAsync(() => {
103 fixture.ngZone.run(() => {
104 router.navigate(['/perf_counters'], { queryParams: { fromLink: '/hosts' } });
107 expect(component.crumbs).toEqual([
108 { path: null, text: 'Cluster' },
109 { path: '/hosts', text: 'Hosts' },
110 { path: '', text: 'Performance Counters' }
114 it('should show all 3 breadcrumbs when navigating to RBD Add', fakeAsync(() => {
115 fixture.ngZone.run(() => {
116 router.navigateByUrl('/block/rbd/add');
119 expect(component.crumbs).toEqual([
120 { path: null, text: 'Block' },
121 { path: '/block/rbd', text: 'Images' },
122 { path: '/block/rbd/add', text: 'Add' }
126 it('should unsubscribe on ngOnDestroy', () => {
127 expect(component.subscription.closed).toBeFalsy();
128 component.ngOnDestroy();
129 expect(component.subscription.closed).toBeTruthy();