]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/blob
85aa96be10dffbd4a5e6dae2080b4771c74dfd18
[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 import { TableComponent } from '~/app/shared/datatable/table/table.component';
21
22 describe('RgwDaemonListComponent', () => {
23   let component: RgwDaemonListComponent;
24   let fixture: ComponentFixture<RgwDaemonListComponent>;
25   let getPermissionsSpy: jasmine.Spy;
26   let getRealmsSpy: jasmine.Spy;
27   let listDaemonsSpy: jest.SpyInstance;
28   const permissions = new Permissions({ grafana: ['read'] });
29   const daemon: RgwDaemon = {
30     id: '8000',
31     service_map_id: '4803',
32     version: 'ceph version',
33     server_hostname: 'ceph',
34     realm_name: 'realm1',
35     zonegroup_name: 'zg1-realm1',
36     zonegroup_id: 'zg1-id',
37     zone_name: 'zone1-zg1-realm1',
38     default: true,
39     port: 80
40   };
41
42   const expectTabsAndHeading = (length: number, heading: string) => {
43     const tabs = TabHelper.getTextContents(fixture);
44     expect(tabs.length).toEqual(length);
45     expect(tabs[length - 1]).toEqual(heading);
46   };
47
48   configureTestBed({
49     declarations: [RgwDaemonListComponent, RgwDaemonDetailsComponent, TableComponent],
50     imports: [
51       BrowserAnimationsModule,
52       HttpClientTestingModule,
53       NgbNavModule,
54       PerformanceCounterModule,
55       SharedModule,
56       RouterTestingModule
57     ]
58   });
59
60   beforeEach(() => {
61     getPermissionsSpy = spyOn(TestBed.inject(AuthStorageService), 'getPermissions');
62     getPermissionsSpy.and.returnValue(new Permissions({}));
63     getRealmsSpy = spyOn(TestBed.inject(RgwSiteService), 'get');
64     getRealmsSpy.and.returnValue(of([]));
65     listDaemonsSpy = jest
66       .spyOn(TestBed.inject(RgwDaemonService), 'list')
67       .mockReturnValue(of([daemon]));
68     fixture = TestBed.createComponent(RgwDaemonListComponent);
69     component = fixture.componentInstance;
70   });
71
72   it('should create', () => {
73     fixture.detectChanges();
74     expect(component).toBeTruthy();
75   });
76
77   it('should show a row with daemon info', fakeAsync(() => {
78     fixture.detectChanges();
79     tick();
80     expect(listDaemonsSpy).toHaveBeenCalledTimes(1);
81     expect(component.daemons).toEqual([daemon]);
82     const cdTableEl = fixture.debugElement.query(By.directive(TableComponent));
83     const cdTableComponent: TableComponent = cdTableEl.componentInstance;
84     cdTableComponent.ngAfterViewInit();
85     fixture.detectChanges();
86     expect(fixture.debugElement.query(By.css('cd-table')).nativeElement.textContent).toContain(
87       '1-1 of 1 item'
88     );
89
90     fixture.destroy();
91   }));
92
93   it('should only show Gateways List tab', () => {
94     fixture.detectChanges();
95
96     expectTabsAndHeading(1, 'Gateways List');
97   });
98
99   it('should show Overall Performance tab', () => {
100     getPermissionsSpy.and.returnValue(permissions);
101     fixture.detectChanges();
102
103     expectTabsAndHeading(2, 'Overall Performance');
104   });
105
106   it('should show Sync Performance tab', () => {
107     getPermissionsSpy.and.returnValue(permissions);
108     getRealmsSpy.and.returnValue(of(['realm1']));
109     fixture.detectChanges();
110
111     expectTabsAndHeading(3, 'Sync Performance');
112   });
113 });