1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { DebugElement } from '@angular/core';
3 import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
5 import { TreeModule } from '@circlon/angular-tree-component';
6 import { of } from 'rxjs';
8 import { CrushRuleService } from '~/app/shared/api/crush-rule.service';
9 import { SharedModule } from '~/app/shared/shared.module';
10 import { configureTestBed } from '~/testing/unit-test-helper';
11 import { CrushmapComponent } from './crushmap.component';
13 describe('CrushmapComponent', () => {
14 let component: CrushmapComponent;
15 let fixture: ComponentFixture<CrushmapComponent>;
16 let debugElement: DebugElement;
17 let crushRuleService: CrushRuleService;
18 let crushRuleServiceInfoSpy: jasmine.Spy;
20 imports: [HttpClientTestingModule, TreeModule, SharedModule],
21 declarations: [CrushmapComponent]
25 fixture = TestBed.createComponent(CrushmapComponent);
26 component = fixture.componentInstance;
27 debugElement = fixture.debugElement;
28 crushRuleService = TestBed.inject(CrushRuleService);
29 crushRuleServiceInfoSpy = spyOn(crushRuleService, 'getInfo');
32 it('should create', () => {
33 expect(component).toBeTruthy();
36 it('should display right title', () => {
37 const span = debugElement.nativeElement.querySelector('.card-header');
38 expect(span.textContent).toBe('CRUSH map viewer');
41 it('should display "No nodes!" if ceph tree nodes is empty array', fakeAsync(() => {
42 crushRuleServiceInfoSpy.and.returnValue(of({ nodes: [] }));
43 fixture.detectChanges();
45 expect(crushRuleService.getInfo).toHaveBeenCalled();
46 expect(component.nodes[0].name).toEqual('No nodes!');
47 component.ngOnDestroy();
50 it('should have two root nodes', fakeAsync(() => {
51 crushRuleServiceInfoSpy.and.returnValue(
54 { children: [-2], type: 'root', name: 'default', id: -1 },
55 { children: [1, 0, 2], type: 'host', name: 'my-host', id: -2 },
56 { status: 'up', type: 'osd', name: 'osd.0', id: 0 },
57 { status: 'down', type: 'osd', name: 'osd.1', id: 1 },
58 { status: 'up', type: 'osd', name: 'osd.2', id: 2 },
59 { children: [-4], type: 'datacenter', name: 'site1', id: -3 },
60 { children: [4], type: 'host', name: 'my-host-2', id: -4 },
61 { status: 'up', type: 'osd', name: 'osd.0-2', id: 4 }
66 fixture.detectChanges();
68 expect(crushRuleService.getInfo).toHaveBeenCalled();
69 expect(component.nodes).toEqual([
76 id: component.nodes[0].children[0].children[0].id,
83 id: component.nodes[0].children[0].id,
87 name: 'my-host-2 (host)'
90 id: component.nodes[0].id,
93 name: 'site1 (datacenter)'
100 id: component.nodes[1].children[0].children[0].id,
107 id: component.nodes[1].children[0].children[1].id,
114 id: component.nodes[1].children[0].children[2].id,
121 id: component.nodes[1].children[0].id,
125 name: 'my-host (host)'
128 id: component.nodes[1].id,
132 name: 'default (root)'
135 component.ngOnDestroy();