Unit tests added

This commit is contained in:
PiotrP 2018-11-28 02:49:35 -08:00
parent 6c2399357d
commit da1c8b3c11
4 changed files with 212 additions and 15 deletions

View File

@ -4,16 +4,16 @@ import { DrawingResizingComponent } from './drawing-resizing.component'
import { DrawingsWidget } from '../../widgets/drawings';
import { DrawingsEventSource } from '../../events/drawings-event-source';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { Subscription } from 'rxjs';
import { EventEmitter } from '@angular/core';
import { ResizingEnd } from '../../events/resizing';
import { MapDrawing } from '../../models/map/map-drawing';
export class DrawingWidgetMock {
resizingFinished = new EventEmitter<ResizingEnd<MapDrawing>>();
evt: any;
constructor(){}
public emitEvent(){
emitEvent(){
const evt = new ResizingEnd<MapDrawing>();
evt.x = 0;
evt.y = 0;
@ -28,8 +28,8 @@ export class DrawingWidgetMock {
describe('DrawizngResizingComponent', () => {
let component: DrawingResizingComponent;
let fixture: ComponentFixture<DrawingResizingComponent>;
let drawingWidgetMock = new DrawingWidgetMock;
let drawingEventSource = new DrawingsEventSource
let drawingsWidgetMock = new DrawingWidgetMock;
let drawingsEventSource = new DrawingsEventSource;
beforeEach(async(() => {
TestBed.configureTestingModule({
@ -37,8 +37,8 @@ describe('DrawizngResizingComponent', () => {
NoopAnimationsModule
],
providers: [
{ provide: DrawingsWidget, useValue: drawingWidgetMock },
{ provide: DrawingsEventSource, useValue: drawingEventSource}
{ provide: DrawingsWidget, useValue: drawingsWidgetMock },
{ provide: DrawingsEventSource, useValue: drawingsEventSource}
],
declarations: [
DrawingResizingComponent
@ -50,6 +50,7 @@ describe('DrawizngResizingComponent', () => {
beforeEach(() => {
fixture = TestBed.createComponent(DrawingResizingComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
@ -57,11 +58,16 @@ describe('DrawizngResizingComponent', () => {
});
it('should emit event after size changes', () => {
spyOn(drawingEventSource.resized, 'emit');
spyOn(drawingsEventSource.resized, 'emit');
const evt = new ResizingEnd<MapDrawing>();
evt.x = 0;
evt.y = 0;
evt.width = 10;
evt.height = 10;
evt.datum = {} as MapDrawing;
drawingWidgetMock.emitEvent();
fixture.detectChanges();
drawingsWidgetMock.emitEvent();
expect(drawingEventSource.resized.emit).toHaveBeenCalled();
expect(drawingsEventSource.resized.emit).toHaveBeenCalled();
});
});

View File

@ -1,14 +1,126 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ProjectMapComponent } from './project-map.component';
import { MatIconModule, MatToolbarModule, MatMenuModule, MatCheckboxModule } from '@angular/material';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { ServerService } from '../../services/server.service';
import { ProjectService } from '../../services/project.service';
import { SettingsService } from '../../services/settings.service';
import { NodeService } from '../../services/node.service';
import { LinkService } from '../../services/link.service';
import { DrawingService } from '../../services/drawing.service';
import { ProgressService } from '../../common/progress/progress.service';
import { ProjectWebServiceHandler } from '../../handlers/project-web-service-handler';
import { MapChangeDetectorRef } from '../../cartography/services/map-change-detector-ref';
import { NodeWidget } from '../../cartography/widgets/node';
import { MapNodeToNodeConverter } from '../../cartography/converters/map/map-node-to-node-converter';
import { MapPortToPortConverter } from '../../cartography/converters/map/map-port-to-port-converter';
import { NodesDataSource } from '../../cartography/datasources/nodes-datasource';
import { LinksDataSource } from '../../cartography/datasources/links-datasource';
import { NodesEventSource } from '../../cartography/events/nodes-event-source';
import { DrawingsEventSource } from '../../cartography/events/drawings-event-source';
import { LinksEventSource } from '../../cartography/events/links-event-source';
import { MapDrawingToSvgConverter } from '../../cartography/converters/map/map-drawing-to-svg-converter';
import { DrawingsDataSource } from '../../cartography/datasources/drawings-datasource';
import { CommonModule } from '@angular/common';
import { ANGULAR_MAP_DECLARATIONS } from '../../cartography/angular-map.imports';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { SymbolService } from '../../services/symbol.service';
import { MockedSettingsService } from '../../services/settings.service.spec';
import { MockedServerService } from '../../services/server.service.spec';
import { MockedProjectService } from '../../services/project.service.spec';
import { Observable } from 'rxjs/Rx';
import { Drawing } from '../../cartography/models/drawing';
import { D3MapComponent } from '../../cartography/components/d3-map/d3-map.component';
import { Project } from '../../models/project';
import { of } from 'rxjs';
import { DrawingElement } from '../../cartography/models/drawings/drawing-element';
import { RectElement } from '../../cartography/models/drawings/rect-element';
import { MapDrawing } from '../../cartography/models/map/map-drawing';
import { HttpServer } from '../../services/http-server.service';
import { Server } from '../../models/server';
import { ResizedDataEvent } from '../../cartography/events/event-source';
export class MockedProgressService {
public activate() {}
}
export class MockedDrawingService {
public drawing = {} as Drawing;
constructor() {}
add(_server: Server, _project_id:string, _x: number, _y:number, _svg: string) {
return of(this.drawing);
}
updatePosition(_server: Server, _drawing: Drawing, _x: number, _y: number){
return of(this.drawing);
}
updateSizeAndPosition(_server: Server, _drawing: Drawing, _x: number, _y: number, _svg: string){
return of(this.drawing);
}
update(_server: Server, _drawing: Drawing){
return of(this.drawing);
}
delete(_server: Server, _drawing: Drawing){
return of(this.drawing);
}
}
describe('ProjectMapComponent', () => {
let component: ProjectMapComponent;
let fixture: ComponentFixture<ProjectMapComponent>;
let drawingService = new MockedDrawingService;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ProjectMapComponent ],
imports: [
MatIconModule,
MatToolbarModule,
MatMenuModule,
MatCheckboxModule,
CommonModule,
NoopAnimationsModule
],
providers: [
{ provide: ActivatedRoute },
{ provide: ServerService, useClass: MockedServerService },
{ provide: ProjectService, useClass: MockedProjectService },
{ provide: SymbolService },
{ provide: NodeService },
{ provide: LinkService },
{ provide: DrawingService, useValue: drawingService},
{ provide: ProgressService, useClass: MockedProgressService },
{ provide: ProjectWebServiceHandler },
{ provide: MapChangeDetectorRef },
{ provide: NodeWidget },
{ provide: MapNodeToNodeConverter },
{ provide: MapPortToPortConverter },
{ provide: NodesDataSource },
{ provide: LinksDataSource },
{ provide: DrawingsDataSource, useValue: {
get: () => { return new Drawing }
} },
{ provide: NodesEventSource },
{ provide: DrawingsEventSource },
{ provide: LinksEventSource },
{ provide: MapDrawingToSvgConverter, useValue: {
convert: () => { return ''}
} },
{ provide: SettingsService, useClass: MockedSettingsService }
],
declarations: [
ProjectMapComponent,
D3MapComponent,
...ANGULAR_MAP_DECLARATIONS
],
schemas: [
NO_ERRORS_SCHEMA
]
})
.compileComponents();
}));
@ -16,7 +128,68 @@ describe('ProjectMapComponent', () => {
beforeEach(() => {
fixture = TestBed.createComponent(ProjectMapComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
//cannot read subscribe of undefined need to be fixed
xit('should update position on resizing event', () => {
let drawingElement: DrawingElement;
let rect = new RectElement();
rect.fill = "#ffffff";
rect.fill_opacity = 1.0;
rect.stroke = "#000000";
rect.stroke_width = 2;
rect.width = 200;
rect.height = 100;
drawingElement = rect;
let mapDrawing = new MapDrawing;
mapDrawing.id = '1';
mapDrawing.projectId = '1';
mapDrawing.rotation = 1;
mapDrawing.svg = '';
mapDrawing.x = 0;
mapDrawing.y = 0;
mapDrawing.z = 0;
mapDrawing.element = drawingElement;
let event = new ResizedDataEvent<MapDrawing>(mapDrawing,0,0,100,100);
spyOn(drawingService, 'updateSizeAndPosition');
component.onDrawingResized(event);
expect(drawingService.updateSizeAndPosition).toHaveBeenCalled();
});
//cannot read subscribe of undefined need to be fixed
xit('should add selected drawing', () => {
component.mapChild = { context: {} } as D3MapComponent;
component.project = { project_id: "1" } as Project;
component.mapChild.context.getZeroZeroTransformationPoint = jasmine.createSpy('HTML element').and.callFake(() => { return {x: 0, y: 0}});
var dummyElement = document.createElement('map');
document.getElementsByClassName = jasmine.createSpy('HTML element').and.callFake(() => { return ([dummyElement])});
spyOn(drawingService, 'add');
component.addDrawing("rectangle");
dummyElement.click();
expect(drawingService.add).toHaveBeenCalled();
});
it('should hide draw tools when hide menu is called', () => {
var dummyElement = document.createElement('map');
document.getElementsByClassName = jasmine.createSpy('HTML element').and.callFake(() => { return ([dummyElement])});
spyOn(component, 'resetDrawToolChoice');
component.hideMenu();
expect(component.resetDrawToolChoice).toHaveBeenCalled();
});
it('should return drawing mock of correct type'), () => {
let mock = component.getDrawingMock('rectangle');
expect(mock instanceof RectElement).toBe(true);
}
});

View File

@ -93,7 +93,7 @@ export class ProjectMapComponent implements OnInit, OnDestroy {
private symbolService: SymbolService,
private nodeService: NodeService,
private linkService: LinkService,
private drawingService: DrawingService,
public drawingService: DrawingService,
private progressService: ProgressService,
private projectWebServiceHandler: ProjectWebServiceHandler,
private mapChangeDetectorRef: MapChangeDetectorRef,
@ -333,7 +333,7 @@ export class ProjectMapComponent implements OnInit, OnDestroy {
});
}
private onDrawingResized(resizedEvent: ResizedDataEvent<MapDrawing>) {
public onDrawingResized(resizedEvent: ResizedDataEvent<MapDrawing>) {
const drawing = this.drawingsDataSource.get(resizedEvent.datum.id);
let svgString = this.mapDrawingToSvgConverter.convert(resizedEvent.datum);

View File

@ -40,7 +40,7 @@ describe('DrawingService', () => {
it('should be created', inject([DrawingService], (service: DrawingService) => {
expect(service).toBeTruthy();
}));
});
it('should updatePosition of drawing', inject([DrawingService], (service: DrawingService) => {
const drawing = new Drawing();
@ -58,6 +58,24 @@ describe('DrawingService', () => {
});
}));
it('should update size and position of drawing', inject([DrawingService], (service: DrawingService) => {
const drawing = new Drawing();
drawing.project_id = "myproject";
drawing.drawing_id = "id";
let svgSample = "<svg><test></svg>"
service.updateSizeAndPosition(server, drawing, 100, 100, svgSample).subscribe();
const req = httpTestingController.expectOne(
'http://127.0.0.1:3080/v2/projects/myproject/drawings/id');
expect(req.request.method).toEqual("PUT");
expect(req.request.body).toEqual({
'x': 100,
'y': 100,
'svg': svgSample
});
});
it('should update drawing', inject([DrawingService], (service: DrawingService) => {
const drawing = new Drawing();
drawing.project_id = "myproject";