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';
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';
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;
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();
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', () => {
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();
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', () => {
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();
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', () => {
expect(editAction).toHaveBeenCalledTimes(0);
expect(cloneAction).toHaveBeenCalledTimes(0);
expect(copyAction).toHaveBeenCalledTimes(1);
+ expect(routerNavigate).toHaveBeenCalledTimes(1);
});
});