diff --git a/src/app/services/http-server.service.spec.ts b/src/app/services/http-server.service.spec.ts index 5ff73d16..461f1a20 100644 --- a/src/app/services/http-server.service.spec.ts +++ b/src/app/services/http-server.service.spec.ts @@ -1,9 +1,9 @@ import { TestBed, } from '@angular/core/testing'; import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; -import { HttpClient } from "@angular/common/http"; +import {HttpClient, HttpErrorResponse} from "@angular/common/http"; import { Server } from '../models/server'; -import { HttpServer } from './http-server.service'; +import {HttpServer, ServerError, ServerErrorHandler} from './http-server.service'; import { getTestServer } from './testing'; import { AppTestingModule } from "../testing/app-testing/app-testing.module"; @@ -11,6 +11,41 @@ class MyType { id: number; } +describe('ServerError', () => { + it('should construct with message', () => { + const error = new Error("test"); + expect(error.message).toEqual("test"); + }); + + it('should construct ServerError from error', () => { + const error = new Error("test"); + const serverError = ServerError.fromError("new message", error); + expect(serverError.originalError).toEqual(error); + expect(serverError.message).toEqual("new message"); + }); +}); + + +describe('ServerErrorHandler', () => { + it('should handle HttpErrorResponse with status 0', () => { + const error = new HttpErrorResponse({ status: 0 }); + + const handler = new ServerErrorHandler(); + const result = handler.handleError(error); + + expect(result.error.message).toEqual('Server is unreachable'); + }); + + it('should not handle HttpErrorResponse with status!=0', () => { + const error = new HttpErrorResponse({ status: 499 }); + + const handler = new ServerErrorHandler(); + const result = handler.handleError(error); + + expect(result.error.message).toEqual('Http failure response for (unknown url): 499 undefined'); + }); +}); + describe('HttpServer', () => { let httpClient: HttpClient; diff --git a/src/app/services/project.service.spec.ts b/src/app/services/project.service.spec.ts index a634a446..50c0389a 100644 --- a/src/app/services/project.service.spec.ts +++ b/src/app/services/project.service.spec.ts @@ -44,6 +44,7 @@ describe('ProjectService', () => { let httpServer: HttpServer; let service: ProjectService; let server: Server; + let settingsService: SettingsService; beforeEach(() => { TestBed.configureTestingModule({ @@ -62,6 +63,8 @@ describe('ProjectService', () => { httpTestingController = TestBed.get(HttpTestingController); httpServer = TestBed.get(HttpServer); service = TestBed.get(ProjectService); + settingsService = TestBed.get(SettingsService); + server = getTestServer(); }); @@ -142,7 +145,29 @@ describe('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') + expect(path).toEqual('ws://127.0.0.1:3080/v2/projects/myproject/notifications/ws'); }); + it('project should be readonly when defined as readonly ', () => { + const project = new Project(); + project.readonly = true; + + expect(service.isReadOnly(project)).toEqual(true); + }); + + it('project should be readonly when experimentalFeatures disabled ', () => { + const project = new Project(); + project.readonly = false; + spyOn(settingsService, 'isExperimentalEnabled').and.returnValue(false); + + expect(service.isReadOnly(project)).toEqual(true); + }); + + it('project should not be readonly when experimentalFeatures enabled ', () => { + const project = new Project(); + project.readonly = false; + spyOn(settingsService, 'isExperimentalEnabled').and.returnValue(true); + + expect(service.isReadOnly(project)).toEqual(false); + }); }); diff --git a/src/app/services/server.service.spec.ts b/src/app/services/server.service.spec.ts index c308bd62..3a0204f1 100644 --- a/src/app/services/server.service.spec.ts +++ b/src/app/services/server.service.spec.ts @@ -1,7 +1,10 @@ -import { TestBed, inject } from '@angular/core/testing'; +import { TestBed } from '@angular/core/testing'; import { ServerService } from './server.service'; import { Server } from "../models/server"; +import { IndexedDbService } from "./indexed-db.service"; +import { AngularIndexedDB } from "angular2-indexeddb"; +import Spy = jasmine.Spy; export class MockedServerService { @@ -22,13 +25,144 @@ export class MockedServerService { describe('ServerService', () => { + let indexedDbService: IndexedDbService; + let db: AngularIndexedDB; + let service: ServerService; + let openDatabaseSpy: Spy; + beforeEach(() => { + indexedDbService = new IndexedDbService(); + + db = indexedDbService.get(); + + openDatabaseSpy = spyOn(db, 'openDatabase').and.returnValue(Promise.resolve(true)); + TestBed.configureTestingModule({ - providers: [ ServerService ] + providers: [ + ServerService, + { provide: IndexedDbService, useValue: indexedDbService} + ] + }); + + service = TestBed.get(ServerService); + }); + + it('should be created and create database', () => { + expect(service).toBeTruthy(); + expect(db.openDatabase).toHaveBeenCalled(); + expect(openDatabaseSpy.calls.first().args[0]).toEqual(1); + + const evnt = { + currentTarget: { + result: { + createObjectStore: function () {} + } + } + }; + + spyOn(evnt.currentTarget.result, 'createObjectStore'); + + const upgradeCallback = openDatabaseSpy.calls.first().args[1]; + upgradeCallback(evnt); + + expect(evnt.currentTarget.result.createObjectStore).toHaveBeenCalledWith( 'servers', { keyPath: 'id', autoIncrement: true }); + }); + + describe('operations on records', () => { + let record: any; + + beforeEach(() => { + record = new Server(); + record.name = 'test'; + }); + + it('should get an object', (done) => { + spyOn(db, 'getByKey').and.returnValue(Promise.resolve([record])); + + service.get(1).then((result) => { + expect(db.getByKey).toHaveBeenCalledWith('servers', 1); + expect(result).toEqual([record]); + done(); + }); + }); + + it('should create an object', (done) => { + const created = new Server(); + created.id = 22; + + spyOn(db, 'add').and.returnValue(Promise.resolve(created)); + + service.create(record).then((result) => { + expect(db.add).toHaveBeenCalledWith('servers', record); + done(); + }); + }); + + it('should update an object', (done) => { + spyOn(db, 'update').and.returnValue(Promise.resolve(record)); + + service.update(record).then((result) => { + expect(db.update).toHaveBeenCalledWith('servers', record); + expect(result).toEqual(record); + done(); + }); + }); + + it('should delete an object', (done) => { + record.id = 88; + + spyOn(db, 'delete').and.returnValue(Promise.resolve()); + + service.delete(record).then(() => { + expect(db.delete).toHaveBeenCalledWith('servers', record.id); + done(); + }); + }); + }); + + it('should call findAll', (done) => { + spyOn(db, 'getAll').and.returnValue(Promise.resolve(true)); + + service.findAll().then((result) => { + expect(result).toEqual(true); + expect(db.getAll).toHaveBeenCalledWith('servers'); + done(); + }); + }); + + it('should create local server when missing', (done) => { + spyOn(db, 'getAll').and.returnValue(Promise.resolve([])); + spyOn(service, 'create').and.returnValue(Promise.resolve(new Server())); + + const expectedServer = new Server(); + expectedServer.name = 'local'; + expectedServer.ip = 'hostname'; + expectedServer.port = 9999; + expectedServer.is_local = true; + + service.getLocalServer('hostname', 9999).then(() => { + expect(service.create).toHaveBeenCalledWith(expectedServer) + done(); + }); + }); + + it('should update local server when found', (done) => { + const server = new Server(); + server.name = 'local'; + server.ip = 'hostname'; + server.port = 9999; + server.is_local = true; + + spyOn(db, 'getAll').and.returnValue(Promise.resolve([server])); + spyOn(service, 'update').and.returnValue(Promise.resolve(new Server())); + + service.getLocalServer('hostname-2', 11111).then(() => { + server.ip = 'hostname-2'; + server.port = 11111; + + expect(service.update).toHaveBeenCalledWith(server); + done(); }); }); - // it('should be created', inject([ServerService], (service: ServerService) => { - // expect(service).toBeTruthy(); - // })); }); diff --git a/src/app/services/server.service.ts b/src/app/services/server.service.ts index b5b456cf..b8a735ea 100644 --- a/src/app/services/server.service.ts +++ b/src/app/services/server.service.ts @@ -12,7 +12,7 @@ export class ServerService { constructor(private indexedDbService: IndexedDbService) { this.ready = indexedDbService.get().openDatabase(1, (evt) => { - const store = evt.currentTarget.result.createObjectStore( + evt.currentTarget.result.createObjectStore( this.tablename, { keyPath: "id", autoIncrement: true }); }); } @@ -81,7 +81,7 @@ export class ServerService { return promise; } - private onReady(query) { + protected onReady(query) { const promise = new Promise((resolve, reject) => { this.ready.then(() => { query() diff --git a/yarn.lock b/yarn.lock index 50b145af..8400b71e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -433,6 +433,10 @@ version "2.8.8" resolved "https://registry.yarnpkg.com/@types/jasmine/-/jasmine-2.8.8.tgz#bf53a7d193ea8b03867a38bfdb4fbb0e0bf066c9" +"@types/jasmine@~2.8.8": + version "2.8.8" + resolved "https://registry.yarnpkg.com/@types/jasmine/-/jasmine-2.8.8.tgz#bf53a7d193ea8b03867a38bfdb4fbb0e0bf066c9" + "@types/jasminewd2@~2.0.2": version "2.0.3" resolved "https://registry.yarnpkg.com/@types/jasminewd2/-/jasminewd2-2.0.3.tgz#0d2886b0cbdae4c0eeba55e30792f584bf040a95"