From 9138d73b5dcb92a70e6cf6d1e3b03e2c84d00428 Mon Sep 17 00:00:00 2001 From: ziajka Date: Mon, 2 Jul 2018 11:09:19 +0200 Subject: [PATCH] Open/close projects, Fixes: #148 --- .../projects/projects.component.spec.ts | 97 ++++++++++++++++++- .../components/projects/projects.component.ts | 3 - src/app/services/project.service.spec.ts | 57 +++++++---- src/app/services/server.service.spec.ts | 8 ++ src/app/services/settings.service.spec.ts | 1 + 5 files changed, 142 insertions(+), 24 deletions(-) diff --git a/src/app/components/projects/projects.component.spec.ts b/src/app/components/projects/projects.component.spec.ts index 488bd51f..d2d892fc 100644 --- a/src/app/components/projects/projects.component.spec.ts +++ b/src/app/components/projects/projects.component.spec.ts @@ -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; + 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(); + }); + }); }); diff --git a/src/app/components/projects/projects.component.ts b/src/app/components/projects/projects.component.ts index bd25cb2b..47c327c7 100644 --- a/src/app/components/projects/projects.component.ts +++ b/src/app/components/projects/projects.component.ts @@ -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) { diff --git a/src/app/services/project.service.spec.ts b/src/app/services/project.service.spec.ts index ecb28bd5..9dc0d652 100644 --- a/src/app/services/project.service.spec.ts +++ b/src/app/services/project.service.spec.ts @@ -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') - })); + }); }); diff --git a/src/app/services/server.service.spec.ts b/src/app/services/server.service.spec.ts index da1a9f63..c308bd62 100644 --- a/src/app/services/server.service.spec.ts +++ b/src/app/services/server.service.spec.ts @@ -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({ diff --git a/src/app/services/settings.service.spec.ts b/src/app/services/settings.service.spec.ts index 7f5650cf..dc510920 100644 --- a/src/app/services/settings.service.spec.ts +++ b/src/app/services/settings.service.spec.ts @@ -9,6 +9,7 @@ export class MockedSettingsService { isExperimentalEnabled() { return true; } + getAll() {} }