]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-form/rbd-form.component.spec.ts
import 15.2.2 octopus source
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / block / rbd-form / rbd-form.component.spec.ts
index 77330e648d670d615f25f9dda814158ca15e2ac3..5556a847a6b7d02268f6062b515adc297ae7e86d 100644 (file)
@@ -1,5 +1,5 @@
 import { HttpClientTestingModule } from '@angular/common/http/testing';
-import { ComponentFixture, discardPeriodicTasks, fakeAsync, TestBed } from '@angular/core/testing';
+import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
 import { ReactiveFormsModule } from '@angular/forms';
 import { ActivatedRoute, Router } from '@angular/router';
 import { RouterTestingModule } from '@angular/router/testing';
@@ -8,7 +8,7 @@ import { TooltipModule } from 'ngx-bootstrap/tooltip';
 import { ToastrModule } from 'ngx-toastr';
 
 import { By } from '@angular/platform-browser';
-import { of } from 'rxjs';
+import { NEVER, of } from 'rxjs';
 import { delay } from 'rxjs/operators';
 
 import { ActivatedRouteStub } from '../../../../testing/activated-route-stub';
@@ -65,14 +65,18 @@ describe('RbdFormComponent', () => {
     let cloneAction: jasmine.Spy;
     let copyAction: jasmine.Spy;
     let rbdServiceGetSpy: jasmine.Spy;
+    let routerNavigate: jasmine.Spy;
+
+    const DELAY = 100;
 
     beforeEach(() => {
-      createAction = spyOn(component, 'createAction').and.stub();
-      editAction = spyOn(component, 'editAction').and.stub();
-      cloneAction = spyOn(component, 'cloneAction').and.stub();
-      copyAction = spyOn(component, 'copyAction').and.stub();
+      createAction = spyOn(component, 'createAction').and.returnValue(of(null));
+      editAction = spyOn(component, 'editAction');
+      editAction.and.returnValue(of(null));
+      cloneAction = spyOn(component, 'cloneAction').and.returnValue(of(null));
+      copyAction = spyOn(component, 'copyAction').and.returnValue(of(null));
       spyOn(component, 'setResponse').and.stub();
-      spyOn(TestBed.get(Router), 'navigate').and.stub();
+      routerNavigate = spyOn(TestBed.get(Router), 'navigate').and.stub();
       rbdServiceGetSpy = spyOn(TestBed.get(RbdService), 'get');
       rbdServiceGetSpy.and.returnValue(of({ pool_name: 'foo', pool_image: 'bar' }));
       component.mode = undefined;
@@ -86,12 +90,28 @@ describe('RbdFormComponent', () => {
       expect(editAction).toHaveBeenCalledTimes(0);
       expect(cloneAction).toHaveBeenCalledTimes(0);
       expect(copyAction).toHaveBeenCalledTimes(0);
+      expect(routerNavigate).toHaveBeenCalledTimes(1);
+    });
+
+    it('should unsubscribe right after image data is received', () => {
+      component.mode = RbdFormMode.editing;
+      rbdServiceGetSpy.and.returnValue(of({ pool_name: 'foo', pool_image: 'bar' }));
+      editAction.and.returnValue(NEVER);
+      component.ngOnInit();
+      component.submit();
+
+      expect(component['rbdImage'].observers.length).toEqual(0);
+      expect(createAction).toHaveBeenCalledTimes(0);
+      expect(editAction).toHaveBeenCalledTimes(1);
+      expect(cloneAction).toHaveBeenCalledTimes(0);
+      expect(copyAction).toHaveBeenCalledTimes(0);
+      expect(routerNavigate).toHaveBeenCalledTimes(0);
     });
 
     it('should not edit image if no image data is received', fakeAsync(() => {
       component.mode = RbdFormMode.editing;
       rbdServiceGetSpy.and.returnValue(
-        of({ pool_name: 'foo', pool_image: 'bar' }).pipe(delay(100))
+        of({ pool_name: 'foo', pool_image: 'bar' }).pipe(delay(DELAY))
       );
       component.ngOnInit();
       component.submit();
@@ -100,8 +120,9 @@ describe('RbdFormComponent', () => {
       expect(editAction).toHaveBeenCalledTimes(0);
       expect(cloneAction).toHaveBeenCalledTimes(0);
       expect(copyAction).toHaveBeenCalledTimes(0);
+      expect(routerNavigate).toHaveBeenCalledTimes(0);
 
-      discardPeriodicTasks();
+      tick(DELAY);
     }));
 
     it('should edit image after image data is received', () => {
@@ -113,12 +134,13 @@ describe('RbdFormComponent', () => {
       expect(editAction).toHaveBeenCalledTimes(1);
       expect(cloneAction).toHaveBeenCalledTimes(0);
       expect(copyAction).toHaveBeenCalledTimes(0);
+      expect(routerNavigate).toHaveBeenCalledTimes(1);
     });
 
     it('should not clone image if no image data is received', fakeAsync(() => {
       component.mode = RbdFormMode.cloning;
       rbdServiceGetSpy.and.returnValue(
-        of({ pool_name: 'foo', pool_image: 'bar' }).pipe(delay(100))
+        of({ pool_name: 'foo', pool_image: 'bar' }).pipe(delay(DELAY))
       );
       component.ngOnInit();
       component.submit();
@@ -127,8 +149,9 @@ describe('RbdFormComponent', () => {
       expect(editAction).toHaveBeenCalledTimes(0);
       expect(cloneAction).toHaveBeenCalledTimes(0);
       expect(copyAction).toHaveBeenCalledTimes(0);
+      expect(routerNavigate).toHaveBeenCalledTimes(0);
 
-      discardPeriodicTasks();
+      tick(DELAY);
     }));
 
     it('should clone image after image data is received', () => {
@@ -140,12 +163,13 @@ describe('RbdFormComponent', () => {
       expect(editAction).toHaveBeenCalledTimes(0);
       expect(cloneAction).toHaveBeenCalledTimes(1);
       expect(copyAction).toHaveBeenCalledTimes(0);
+      expect(routerNavigate).toHaveBeenCalledTimes(1);
     });
 
     it('should not copy image if no image data is received', fakeAsync(() => {
       component.mode = RbdFormMode.copying;
       rbdServiceGetSpy.and.returnValue(
-        of({ pool_name: 'foo', pool_image: 'bar' }).pipe(delay(100))
+        of({ pool_name: 'foo', pool_image: 'bar' }).pipe(delay(DELAY))
       );
       component.ngOnInit();
       component.submit();
@@ -154,8 +178,9 @@ describe('RbdFormComponent', () => {
       expect(editAction).toHaveBeenCalledTimes(0);
       expect(cloneAction).toHaveBeenCalledTimes(0);
       expect(copyAction).toHaveBeenCalledTimes(0);
+      expect(routerNavigate).toHaveBeenCalledTimes(0);
 
-      discardPeriodicTasks();
+      tick(DELAY);
     }));
 
     it('should copy image after image data is received', () => {
@@ -167,6 +192,7 @@ describe('RbdFormComponent', () => {
       expect(editAction).toHaveBeenCalledTimes(0);
       expect(cloneAction).toHaveBeenCalledTimes(0);
       expect(copyAction).toHaveBeenCalledTimes(1);
+      expect(routerNavigate).toHaveBeenCalledTimes(1);
     });
   });