]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
c8cddfdc8a9eb06469d02494709fe8d187ff022e
[ceph-ci.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
3 import { By } from '@angular/platform-browser';
4 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
5 import { RouterTestingModule } from '@angular/router/testing';
6
7 import { NgbNavModule } from '@ng-bootstrap/ng-bootstrap';
8 import { of } from 'rxjs';
9
10 import { PerformanceCounterModule } from '~/app/ceph/performance-counter/performance-counter.module';
11 import { RgwDaemon } from '~/app/ceph/rgw/models/rgw-daemon';
12 import { RgwDaemonService } from '~/app/shared/api/rgw-daemon.service';
13 import { RgwSiteService } from '~/app/shared/api/rgw-site.service';
14 import { Permissions } from '~/app/shared/models/permissions';
15 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
16 import { SharedModule } from '~/app/shared/shared.module';
17 import { configureTestBed, TabHelper } from '~/testing/unit-test-helper';
18 import { RgwDaemonDetailsComponent } from '../rgw-daemon-details/rgw-daemon-details.component';
19 import { RgwDaemonListComponent } from './rgw-daemon-list.component';
20
21 describe('RgwDaemonListComponent', () => {
22   let component: RgwDaemonListComponent;
23   let fixture: ComponentFixture<RgwDaemonListComponent>;
24   let getPermissionsSpy: jasmine.Spy;
25   let getRealmsSpy: jasmine.Spy;
26   let listDaemonsSpy: jest.SpyInstance;
27   const permissions = new Permissions({ grafana: ['read'] });
28   const daemon: RgwDaemon = {
29     id: '8000',
30     service_map_id: '4803',
31     version: 'ceph version',
32     server_hostname: 'ceph',
33     realm_name: 'realm1',
34     zonegroup_name: 'zg1-realm1',
35     zone_name: 'zone1-zg1-realm1',
36     default: true
37   };
38
39   const expectTabsAndHeading = (length: number, heading: string) => {
40     const tabs = TabHelper.getTextContents(fixture);
41     expect(tabs.length).toEqual(length);
42     expect(tabs[length - 1]).toEqual(heading);
43   };
44
45   configureTestBed({
46     declarations: [RgwDaemonListComponent, RgwDaemonDetailsComponent],
47     imports: [
48       BrowserAnimationsModule,
49       HttpClientTestingModule,
50       NgbNavModule,
51       PerformanceCounterModule,
52       SharedModule,
53       RouterTestingModule
54     ]
55   });
56
57   beforeEach(() => {
58     getPermissionsSpy = spyOn(TestBed.inject(AuthStorageService), 'getPermissions');
59     getPermissionsSpy.and.returnValue(new Permissions({}));
60     getRealmsSpy = spyOn(TestBed.inject(RgwSiteService), 'get');
61     getRealmsSpy.and.returnValue(of([]));
62     listDaemonsSpy = jest
63       .spyOn(TestBed.inject(RgwDaemonService), 'list')
64       .mockReturnValue(of([daemon]));
65     fixture = TestBed.createComponent(RgwDaemonListComponent);
66     component = fixture.componentInstance;
67   });
68
69   it('should create', () => {
70     fixture.detectChanges();
71     expect(component).toBeTruthy();
72   });
73
74   it('should show a row with daemon info', fakeAsync(() => {
75     fixture.detectChanges();
76     tick();
77     expect(listDaemonsSpy).toHaveBeenCalledTimes(1);
78     expect(component.daemons).toEqual([daemon]);
79     expect(fixture.debugElement.query(By.css('cd-table')).nativeElement.textContent).toContain(
80       'total of 1'
81     );
82
83     fixture.destroy();
84   }));
85
86   it('should only show Daemons List tab', () => {
87     fixture.detectChanges();
88
89     expectTabsAndHeading(1, 'Daemons List');
90   });
91
92   it('should show Overall Performance tab', () => {
93     getPermissionsSpy.and.returnValue(permissions);
94     fixture.detectChanges();
95
96     expectTabsAndHeading(2, 'Overall Performance');
97   });
98
99   it('should show Sync Performance tab', () => {
100     getPermissionsSpy.and.returnValue(permissions);
101     getRealmsSpy.and.returnValue(of(['realm1']));
102     fixture.detectChanges();
103
104     expectTabsAndHeading(3, 'Sync Performance');
105   });
106 });