]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/core/context/context.component.spec.ts
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / core / context / context.component.spec.ts
1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
3 import { Router } from '@angular/router';
4 import { RouterTestingModule } from '@angular/router/testing';
5
6 import { of } from 'rxjs';
7
8 import { Permissions } from '~/app/shared/models/permissions';
9 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
10 import {
11 FeatureTogglesMap,
12 FeatureTogglesService
13 } from '~/app/shared/services/feature-toggles.service';
14 import { configureTestBed, RgwHelper } from '~/testing/unit-test-helper';
15 import { ContextComponent } from './context.component';
16
17 describe('ContextComponent', () => {
18 let component: ContextComponent;
19 let fixture: ComponentFixture<ContextComponent>;
20 let router: Router;
21 let routerNavigateByUrlSpy: jasmine.Spy;
22 let routerNavigateSpy: jasmine.Spy;
23 let getPermissionsSpy: jasmine.Spy;
24 let getFeatureTogglesSpy: jasmine.Spy;
25 let ftMap: FeatureTogglesMap;
26 let httpTesting: HttpTestingController;
27
28 const daemonList = RgwHelper.getDaemonList();
29
30 configureTestBed({
31 declarations: [ContextComponent],
32 imports: [HttpClientTestingModule, RouterTestingModule]
33 });
34
35 beforeEach(() => {
36 httpTesting = TestBed.inject(HttpTestingController);
37 router = TestBed.inject(Router);
38 routerNavigateByUrlSpy = spyOn(router, 'navigateByUrl');
39 routerNavigateByUrlSpy.and.returnValue(Promise.resolve(undefined));
40 routerNavigateSpy = spyOn(router, 'navigate');
41 getPermissionsSpy = spyOn(TestBed.inject(AuthStorageService), 'getPermissions');
42 getPermissionsSpy.and.returnValue(
43 new Permissions({ rgw: ['read', 'update', 'create', 'delete'] })
44 );
45 getFeatureTogglesSpy = spyOn(TestBed.inject(FeatureTogglesService), 'get');
46 ftMap = new FeatureTogglesMap();
47 ftMap.rgw = true;
48 getFeatureTogglesSpy.and.returnValue(of(ftMap));
49 fixture = TestBed.createComponent(ContextComponent);
50 component = fixture.componentInstance;
51 });
52
53 it('should create', () => {
54 expect(component).toBeTruthy();
55 });
56
57 it('should not show any info if not in RGW route', () => {
58 component.isRgwRoute = false;
59 expect(fixture.debugElement.nativeElement.textContent).toEqual('');
60 });
61
62 it('should select the default daemon', fakeAsync(() => {
63 component.isRgwRoute = true;
64 fixture.detectChanges();
65 tick();
66 const req = httpTesting.expectOne('api/rgw/daemon');
67 req.flush(daemonList);
68 fixture.detectChanges();
69 const selectedDaemon = fixture.debugElement.nativeElement.querySelector(
70 '.ctx-bar-selected-rgw-daemon'
71 );
72 expect(selectedDaemon.textContent).toEqual(' daemon2 ( zonegroup2 ) ');
73
74 const availableDaemons = fixture.debugElement.nativeElement.querySelectorAll(
75 '.ctx-bar-available-rgw-daemon'
76 );
77 expect(availableDaemons.length).toEqual(daemonList.length);
78 expect(availableDaemons[0].textContent).toEqual(' daemon1 ( zonegroup1 ) ');
79 component.ngOnDestroy();
80 }));
81
82 it('should select the chosen daemon', fakeAsync(() => {
83 component.isRgwRoute = true;
84 fixture.detectChanges();
85 tick();
86 const req = httpTesting.expectOne('api/rgw/daemon');
87 req.flush(daemonList);
88 fixture.detectChanges();
89 component.onDaemonSelection(daemonList[2]);
90 expect(routerNavigateByUrlSpy).toHaveBeenCalledTimes(1);
91 fixture.detectChanges();
92 tick();
93 expect(routerNavigateSpy).toHaveBeenCalledTimes(1);
94 const selectedDaemon = fixture.debugElement.nativeElement.querySelector(
95 '.ctx-bar-selected-rgw-daemon'
96 );
97 expect(selectedDaemon.textContent).toEqual(' daemon3 ( zonegroup3 ) ');
98 component.ngOnDestroy();
99 }));
100 });