Don't delete items on topology when in readonly mode

This commit is contained in:
ziajka
2018-05-17 13:46:43 +02:00
parent ff2f6f4969
commit edcf42cf77
9 changed files with 129 additions and 53 deletions

View File

@ -1,25 +1,102 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { HttpClientTestingModule } from "@angular/common/http/testing";
import { inject } from "@angular/core/testing";
import { mock, instance, capture, when } from "ts-mockito";
import { HotkeyModule, HotkeysService, Hotkey } from "angular2-hotkeys";
import { Observable } from "rxjs";
import { ProjectMapShortcutsComponent } from './project-map-shortcuts.component';
import {
ToasterService,
MockedToasterService
} from "../../shared/services/toaster.service";
import { NodeService } from "../../shared/services/node.service";
import { HttpServer } from "../../shared/services/http-server.service";
import { SelectionManager } from "../../cartography/shared/managers/selection-manager";
import { Server } from "../../shared/models/server";
import { Node } from "../../cartography/shared/models/node";
import { Project } from "../../shared/models/project";
describe('ProjectMapShortcutsComponent', () => {
let component: ProjectMapShortcutsComponent;
let fixture: ComponentFixture<ProjectMapShortcutsComponent>;
let hotkeyServiceMock: HotkeysService;
let hotkeyServiceInstanceMock: HotkeysService;
let nodeServiceMock: NodeService;
beforeEach(async(() => {
nodeServiceMock = mock(NodeService);
hotkeyServiceMock = mock(HotkeysService);
hotkeyServiceInstanceMock = instance(hotkeyServiceMock);
TestBed.configureTestingModule({
imports: [
HotkeyModule.forRoot(),
HttpClientTestingModule
],
providers: [
HttpServer,
{ provide: NodeService, useFactory: () => instance(nodeServiceMock)},
{ provide: HotkeysService, useFactory: () => hotkeyServiceInstanceMock},
{ provide: ToasterService, useClass: MockedToasterService},
],
declarations: [ ProjectMapShortcutsComponent ]
})
.compileComponents();
}));
// beforeEach(() => {
// fixture = TestBed.createComponent(ProjectMapShortcutsComponent);
// component = fixture.componentInstance;
// fixture.detectChanges();
// });
beforeEach(() => {
fixture = TestBed.createComponent(ProjectMapShortcutsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should bind delete key', () => {
component.ngOnInit();
const [hotkey] = capture(hotkeyServiceMock.add).last();
expect((hotkey as Hotkey).combo).toEqual([ 'del' ]);
expect((hotkey as Hotkey).callback).toEqual(component.onDeleteHandler);
});
it('should remove binding', () => {
component.ngOnDestroy();
const [hotkey] = capture(hotkeyServiceMock.remove).last();
expect((hotkey as Hotkey).combo).toEqual([ 'del' ]);
});
describe('onDeleteHandler', () => {
beforeEach(() => {
const selectionManagerMock = mock(SelectionManager);
const node = new Node();
node.node_id = "nodeid";
const server = new Server();
const project = new Project();
when(selectionManagerMock.getSelectedNodes()).thenReturn([node]);
when(nodeServiceMock.delete(server, node))
.thenReturn(Observable.of(node).mapTo(null));
component.project = project;
component.server = server;
component.selectionManager = instance(selectionManagerMock);
});
it('should handle delete', inject([ToasterService], (toaster: MockedToasterService) => {
component.project.readonly = false;
component.onDeleteHandler(null);
expect(toaster.successes).toEqual(["Node has been deleted"]);
}));
it('should not delete when project in readonly mode', inject([ToasterService], (toaster: MockedToasterService) => {
component.project.readonly = true;
component.onDeleteHandler(null);
expect(toaster.successes).toEqual([]);
}));
});
// it('should create', () => {
// expect(component).toBeTruthy();
// });
});

View File

@ -5,13 +5,15 @@ import { SelectionManager } from '../../cartography/shared/managers/selection-ma
import { NodeService } from '../../shared/services/node.service';
import { Server } from '../../shared/models/server';
import { ToasterService } from '../../shared/services/toaster.service';
import { Project } from "../../shared/models/project";
@Component({
selector: 'app-project-map-shortcuts',
templateUrl: './project-map-shortcuts.component.html'
template: ''
})
export class ProjectMapShortcutsComponent implements OnInit, OnDestroy {
@Input() project: Project;
@Input() server: Server;
@Input() selectionManager: SelectionManager;
@ -24,7 +26,12 @@ export class ProjectMapShortcutsComponent implements OnInit, OnDestroy {
) { }
ngOnInit() {
this.deleteHotkey = new Hotkey('del', (event: KeyboardEvent): boolean => {
this.deleteHotkey = new Hotkey('del', this.onDeleteHandler);
this.hotkeysService.add(this.deleteHotkey);
}
onDeleteHandler(event: KeyboardEvent):boolean {
if (!this.project.readonly) {
const selectedNodes = this.selectionManager.getSelectedNodes();
if (selectedNodes) {
selectedNodes.forEach((node) => {
@ -33,10 +40,8 @@ export class ProjectMapShortcutsComponent implements OnInit, OnDestroy {
});
});
}
return false;
});
this.hotkeysService.add(this.deleteHotkey);
}
return false;
}
ngOnDestroy() {

View File

@ -55,4 +55,4 @@
</mat-spinner>
</div>
<app-project-map-shortcuts [server]="server" [selectionManager]="selectionManager"></app-project-map-shortcuts>
<app-project-map-shortcuts *ngIf="project" [project]="project" [server]="server" [selectionManager]="selectionManager"></app-project-map-shortcuts>

View File

@ -9,4 +9,5 @@ export class Project {
scene_height: number;
scene_width: number;
status: string;
readonly: boolean = true;
}

View File

@ -64,7 +64,9 @@ describe('SettingsService', () => {
let changedSettings: Settings;
service.set('crash_reports', true);
service.subscribe(settings => changedSettings = settings);
service.subscribe(settings => {
changedSettings = settings
});
service.set('crash_reports', false);
expect(changedSettings.crash_reports).toEqual(false);

View File

@ -4,8 +4,6 @@ import { HttpClient } from '@angular/common/http';
import { HttpTestingController, HttpClientTestingModule } from '@angular/common/http/testing';
import { HttpServer } from './http-server.service';
import { Server } from '../models/server';
import { Node } from '../../cartography/shared/models/node';
import { Port } from '../models/port';
import { getTestServer } from './testing';
import { SymbolService } from './symbol.service';
import { Symbol } from '../../cartography/shared/models/symbol';
@ -61,7 +59,6 @@ describe('SymbolService', () => {
}));
it('should load symbols', inject([SymbolService], (service: SymbolService) => {
let call = 0;
service.load(server).subscribe();
const req = httpTestingController.expectOne(

View File

@ -13,3 +13,23 @@ export class ToasterService {
this.snackbar.open(message, null, { duration: 2000 });
}
}
@Injectable()
export class MockedToasterService {
public errors: string[];
public successes: string[];
constructor() {
this.errors = [];
this.successes = [];
}
public error(message: string) {
this.errors.push(message);
}
public success(message: string) {
this.successes.push(message);
}
}