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';
6 import { of } from 'rxjs';
8 import { Permissions } from '~/app/shared/models/permissions';
9 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
13 } from '~/app/shared/services/feature-toggles.service';
14 import { configureTestBed, RgwHelper } from '~/testing/unit-test-helper';
15 import { ContextComponent } from './context.component';
17 describe('ContextComponent', () => {
18 let component: ContextComponent;
19 let fixture: ComponentFixture<ContextComponent>;
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;
28 const daemonList = RgwHelper.getDaemonList();
31 declarations: [ContextComponent],
32 imports: [HttpClientTestingModule, RouterTestingModule]
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'] })
45 getFeatureTogglesSpy = spyOn(TestBed.inject(FeatureTogglesService), 'get');
46 ftMap = new FeatureTogglesMap();
48 getFeatureTogglesSpy.and.returnValue(of(ftMap));
49 fixture = TestBed.createComponent(ContextComponent);
50 component = fixture.componentInstance;
53 it('should create', () => {
54 expect(component).toBeTruthy();
57 it('should not show any info if not in RGW route', () => {
58 component.isRgwRoute = false;
59 expect(fixture.debugElement.nativeElement.textContent).toEqual('');
62 it('should select the default daemon', fakeAsync(() => {
63 component.isRgwRoute = true;
64 fixture.detectChanges();
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'
72 expect(selectedDaemon.textContent).toEqual(' daemon2 ( zonegroup2 ) ');
74 const availableDaemons = fixture.debugElement.nativeElement.querySelectorAll(
75 '.ctx-bar-available-rgw-daemon'
77 expect(availableDaemons.length).toEqual(daemonList.length);
78 expect(availableDaemons[0].textContent).toEqual(' daemon1 ( zonegroup1 ) ');
79 component.ngOnDestroy();
82 it('should select the chosen daemon', fakeAsync(() => {
83 component.isRgwRoute = true;
84 fixture.detectChanges();
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();
93 expect(routerNavigateSpy).toHaveBeenCalledTimes(1);
94 const selectedDaemon = fixture.debugElement.nativeElement.querySelector(
95 '.ctx-bar-selected-rgw-daemon'
97 expect(selectedDaemon.textContent).toEqual(' daemon3 ( zonegroup3 ) ');
98 component.ngOnDestroy();