Open/close projects, Fixes: #148

This commit is contained in:
ziajka 2018-07-02 11:09:19 +02:00
parent e6669a7df8
commit 9138d73b5d
5 changed files with 142 additions and 24 deletions

View File

@ -1,20 +1,67 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { MatIconModule, MatSortModule, MatTableModule, MatTooltipModule } from "@angular/material";
import { ProjectsComponent } from './projects.component';
import { MatTooltipModule } from "@angular/material";
import { RouterTestingModule } from "@angular/router/testing";
import { ServerService } from "../../services/server.service";
import { MockedServerService } from "../../services/server.service.spec";
import { ProjectService } from "../../services/project.service";
import { MockedProjectService } from "../../services/project.service.spec";
import { SettingsService } from "../../services/settings.service";
import { MockedSettingsService } from "../../services/settings.service.spec";
import { ProgressService } from "../../common/progress/progress.service";
import { NoopAnimationsModule } from "@angular/platform-browser/animations";
import { Server } from "../../models/server";
import { Observable } from "rxjs/Observable";
import { Settings } from "../../services/settings.service";
import { Project } from "../../models/project";
describe('ProjectsComponent', () => {
let component: ProjectsComponent;
let fixture: ComponentFixture<ProjectsComponent>;
let settingsService: SettingsService;
let projectService: ProjectService;
let serverService: ServerService;
let server: Server;
let progressService: ProgressService;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
MatTableModule,
MatTooltipModule,
MatIconModule,
MatSortModule,
NoopAnimationsModule,
RouterTestingModule.withRoutes([]),
],
providers: [
{ provide: ServerService, useClass: MockedServerService },
{ provide: ProjectService, useClass: MockedProjectService },
{ provide: SettingsService, useClass: MockedSettingsService },
ProgressService
],
declarations: [ ProjectsComponent ]
})
.compileComponents();
serverService = TestBed.get(ServerService);
settingsService = TestBed.get(SettingsService);
projectService = TestBed.get(ProjectService);
progressService = TestBed.get(ProgressService);
server = new Server();
server.id = 99;
const settings = {} as Settings;
spyOn(serverService, 'get').and.returnValue(Promise.resolve(server));
spyOn(settingsService, 'getAll').and.returnValue(settings);
spyOn(projectService, 'list').and.returnValue(Observable.of([]));
spyOn(progressService, 'activate');
spyOn(progressService, 'deactivate');
}));
beforeEach(() => {
@ -23,4 +70,52 @@ describe('ProjectsComponent', () => {
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
describe('ProjectComponent open', () => {
let project: Project;
beforeEach(() => {
project = new Project();
project.project_id = "1";
spyOn(projectService, 'open').and.returnValue(Observable.of(project));
component.server = server;
});
it('should open project', () => {
component.open(project);
expect(projectService.open).toHaveBeenCalledWith(server, project.project_id);
expect(progressService.activate).toHaveBeenCalled();
expect(progressService.deactivate).toHaveBeenCalled();
});
});
describe('ProjectComponent close', () => {
let project: Project;
beforeEach(() => {
project = new Project();
project.project_id = "1";
spyOn(projectService, 'close').and.returnValue(Observable.of(project));
component.server = server;
});
it('should close project', () => {
component.close(project);
expect(projectService.close).toHaveBeenCalledWith(server, project.project_id);
expect(progressService.activate).toHaveBeenCalled();
expect(progressService.deactivate).toHaveBeenCalled();
});
});
});

View File

@ -42,7 +42,6 @@ export class ProjectsComponent implements OnInit {
start: 'asc'
}
);
this.dataSource = new ProjectDataSource(this.projectDatabase, this.sort);
this.route.paramMap
@ -64,8 +63,6 @@ export class ProjectsComponent implements OnInit {
.subscribe((projects: Project[]) => {
this.projectDatabase.addProjects(projects);
});
}
delete(project: Project) {

View File

@ -8,12 +8,29 @@ import { getTestServer } from './testing';
import { ProjectService } from './project.service';
import { SettingsService } from "./settings.service";
import { MockedSettingsService } from "./settings.service.spec";
import { Observable } from "rxjs/Observable";
import { Project } from "../models/project";
/**
* Mocks ProjectsService so it's not based on settings
*/
export class MockedProjectService {
public projects: Project[] = [];
list(server: Server) {
return Observable.of(this.projects);
}
open(server: Server, project: Project) {
return Observable.of(project);
}
close(server: Server, project: Project) {
return Observable.of(project);
}
isReadOnly(project) {
return project.readonly;
}
@ -50,80 +67,80 @@ describe('ProjectService', () => {
httpTestingController.verify();
});
it('should be created', inject([ProjectService], (service: ProjectService) => {
it('should be created', () => {
expect(service).toBeTruthy();
}));
});
it('should get the project', inject([ProjectService], (service: ProjectService) => {
it('should get the project', () => {
service.get(server, "myproject").subscribe();
const req = httpTestingController.expectOne(
'http://127.0.0.1:3080/v2/projects/myproject');
expect(req.request.method).toEqual("GET");
}));
});
it('should open the project', inject([ProjectService], (service: ProjectService) => {
it('should open the project', () => {
service.open(server, "myproject").subscribe();
const req = httpTestingController.expectOne(
'http://127.0.0.1:3080/v2/projects/myproject/open');
expect(req.request.method).toEqual("POST");
expect(req.request.body).toEqual({});
}));
});
it('should close the project', inject([ProjectService], (service: ProjectService) => {
it('should close the project', () => {
service.close(server, "myproject").subscribe();
const req = httpTestingController.expectOne(
'http://127.0.0.1:3080/v2/projects/myproject/close');
expect(req.request.method).toEqual("POST");
expect(req.request.body).toEqual({});
}));
});
it('should list projects', inject([ProjectService], (service: ProjectService) => {
it('should list projects', () => {
service.list(server).subscribe();
const req = httpTestingController.expectOne(
'http://127.0.0.1:3080/v2/projects');
expect(req.request.method).toEqual("GET");
}));
});
it('should get nodes of project', inject([ProjectService], (service: ProjectService) => {
it('should get nodes of project', () => {
service.nodes(server, "myproject").subscribe();
const req = httpTestingController.expectOne(
'http://127.0.0.1:3080/v2/projects/myproject/nodes');
expect(req.request.method).toEqual("GET");
}));
});
it('should get links of project', inject([ProjectService], (service: ProjectService) => {
it('should get links of project', () => {
service.links(server, "myproject").subscribe();
const req = httpTestingController.expectOne(
'http://127.0.0.1:3080/v2/projects/myproject/links');
expect(req.request.method).toEqual("GET");
}));
});
it('should get drawings of project', inject([ProjectService], (service: ProjectService) => {
it('should get drawings of project', () => {
service.drawings(server, "myproject").subscribe();
const req = httpTestingController.expectOne(
'http://127.0.0.1:3080/v2/projects/myproject/drawings');
expect(req.request.method).toEqual("GET");
}));
});
it('should delete the project', inject([ProjectService], (service: ProjectService) => {
it('should delete the project', () => {
service.delete(server, "myproject").subscribe();
const req = httpTestingController.expectOne(
'http://127.0.0.1:3080/v2/projects/myproject');
expect(req.request.method).toEqual("DELETE");
}));
});
it('should get notifications path of project', inject([ProjectService], (service: ProjectService) => {
it('should get notifications path of project', () => {
const path = service.notificationsPath(server, "myproject");
expect(path).toEqual('ws://127.0.0.1:3080/v2/projects/myproject/notifications/ws')
}));
});
});

View File

@ -3,7 +3,14 @@ import { TestBed, inject } from '@angular/core/testing';
import { ServerService } from './server.service';
import { Server } from "../models/server";
export class MockedServerService {
public get(server_id: number) {
const server = new Server();
server.id = server_id;
return Promise.resolve(server);
}
public getLocalServer(hostname: string, port: number) {
return new Promise((resolve, reject) => {
const server = new Server();
@ -13,6 +20,7 @@ export class MockedServerService {
}
}
describe('ServerService', () => {
beforeEach(() => {
TestBed.configureTestingModule({

View File

@ -9,6 +9,7 @@ export class MockedSettingsService {
isExperimentalEnabled() {
return true;
}
getAll() {}
}