]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
bdb4decd9dab36e7c8c350688211445ca018c8e1
[ceph.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     port: 80
38   };
39
40   const expectTabsAndHeading = (length: number, heading: string) => {
41     const tabs = TabHelper.getTextContents(fixture);
42     expect(tabs.length).toEqual(length);
43     expect(tabs[length - 1]).toEqual(heading);
44   };
45
46   configureTestBed({
47     declarations: [RgwDaemonListComponent, RgwDaemonDetailsComponent],
48     imports: [
49       BrowserAnimationsModule,
50       HttpClientTestingModule,
51       NgbNavModule,
52       PerformanceCounterModule,
53       SharedModule,
54       RouterTestingModule
55     ]
56   });
57
58   beforeEach(() => {
59     getPermissionsSpy = spyOn(TestBed.inject(AuthStorageService), 'getPermissions');
60     getPermissionsSpy.and.returnValue(new Permissions({}));
61     getRealmsSpy = spyOn(TestBed.inject(RgwSiteService), 'get');
62     getRealmsSpy.and.returnValue(of([]));
63     listDaemonsSpy = jest
64       .spyOn(TestBed.inject(RgwDaemonService), 'list')
65       .mockReturnValue(of([daemon]));
66     fixture = TestBed.createComponent(RgwDaemonListComponent);
67     component = fixture.componentInstance;
68   });
69
70   it('should create', () => {
71     fixture.detectChanges();
72     expect(component).toBeTruthy();
73   });
74
75   it('should show a row with daemon info', fakeAsync(() => {
76     fixture.detectChanges();
77     tick();
78     expect(listDaemonsSpy).toHaveBeenCalledTimes(1);
79     expect(component.daemons).toEqual([daemon]);
80     expect(fixture.debugElement.query(By.css('cd-table')).nativeElement.textContent).toContain(
81       'total of 1'
82     );
83
84     fixture.destroy();
85   }));
86
87   it('should only show Gateways List tab', () => {
88     fixture.detectChanges();
89
90     expectTabsAndHeading(1, 'Gateways List');
91   });
92
93   it('should show Overall Performance tab', () => {
94     getPermissionsSpy.and.returnValue(permissions);
95     fixture.detectChanges();
96
97     expectTabsAndHeading(2, 'Overall Performance');
98   });
99
100   it('should show Sync Performance tab', () => {
101     getPermissionsSpy.and.returnValue(permissions);
102     getRealmsSpy.and.returnValue(of(['realm1']));
103     fixture.detectChanges();
104
105     expectTabsAndHeading(3, 'Sync Performance');
106   });
107 });