summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/breadcrumbs/breadcrumbs.component.spec.ts
blob: b6551f780e9bf42ad1b798e0d6c3b3e078d3706a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { Router, Routes } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';

import { PerformanceCounterBreadcrumbsResolver } from '~/app/app-routing.module';
import { configureTestBed } from '~/testing/unit-test-helper';
import { BreadcrumbsComponent } from './breadcrumbs.component';

describe('BreadcrumbsComponent', () => {
  let component: BreadcrumbsComponent;
  let fixture: ComponentFixture<BreadcrumbsComponent>;
  let router: Router;

  @Component({ selector: 'cd-fake', template: '' })
  class FakeComponent {}

  const routes: Routes = [
    {
      path: 'hosts',
      component: FakeComponent,
      data: { breadcrumbs: 'Cluster/Hosts' }
    },
    {
      path: 'perf_counters',
      component: FakeComponent,
      data: {
        breadcrumbs: PerformanceCounterBreadcrumbsResolver
      }
    },
    {
      path: 'block',
      data: { breadcrumbs: true, text: 'Block', path: null },
      children: [
        {
          path: 'rbd',
          data: { breadcrumbs: 'Images' },
          children: [
            { path: '', component: FakeComponent },
            { path: 'add', component: FakeComponent, data: { breadcrumbs: 'Add' } }
          ]
        }
      ]
    }
  ];

  configureTestBed({
    declarations: [BreadcrumbsComponent, FakeComponent],
    imports: [CommonModule, RouterTestingModule.withRoutes(routes)],
    providers: [PerformanceCounterBreadcrumbsResolver]
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(BreadcrumbsComponent);
    router = TestBed.inject(Router);
    component = fixture.componentInstance;
    fixture.detectChanges();
    expect(component.crumbs).toEqual([]);
  });

  it('should create', () => {
    expect(component).toBeTruthy();
    expect(component.subscription).toBeDefined();
  });

  it('should run postProcess and split the breadcrumbs when navigating to hosts', fakeAsync(() => {
    fixture.ngZone.run(() => {
      router.navigateByUrl('/hosts');
    });
    tick();
    expect(component.crumbs).toEqual([
      { path: null, text: 'Cluster' },
      { path: '/hosts', text: 'Hosts' }
    ]);
  }));

  it('should display empty breadcrumb when navigating to perf_counters from unknown path', fakeAsync(() => {
    fixture.ngZone.run(() => {
      router.navigateByUrl('/perf_counters');
    });
    tick();
    expect(component.crumbs).toEqual([
      { path: null, text: 'Cluster' },
      { path: null, text: '' },
      { path: '', text: 'Performance Counters' }
    ]);
  }));

  it('should display Monitor breadcrumb when navigating to perf_counters from Monitors', fakeAsync(() => {
    fixture.ngZone.run(() => {
      router.navigate(['/perf_counters'], { queryParams: { fromLink: '/monitor' } });
    });
    tick();
    expect(component.crumbs).toEqual([
      { path: null, text: 'Cluster' },
      { path: '/monitor', text: 'Monitors' },
      { path: '', text: 'Performance Counters' }
    ]);
  }));

  it('should display Hosts breadcrumb when navigating to perf_counters from Hosts', fakeAsync(() => {
    fixture.ngZone.run(() => {
      router.navigate(['/perf_counters'], { queryParams: { fromLink: '/hosts' } });
    });
    tick();
    expect(component.crumbs).toEqual([
      { path: null, text: 'Cluster' },
      { path: '/hosts', text: 'Hosts' },
      { path: '', text: 'Performance Counters' }
    ]);
  }));

  it('should show all 3 breadcrumbs when navigating to RBD Add', fakeAsync(() => {
    fixture.ngZone.run(() => {
      router.navigateByUrl('/block/rbd/add');
    });
    tick();
    expect(component.crumbs).toEqual([
      { path: null, text: 'Block' },
      { path: '/block/rbd', text: 'Images' },
      { path: '/block/rbd/add', text: 'Add' }
    ]);
  }));

  it('should unsubscribe on ngOnDestroy', () => {
    expect(component.subscription.closed).toBeFalsy();
    component.ngOnDestroy();
    expect(component.subscription.closed).toBeTruthy();
  });
});