From 10b82f224d8420dcbb59ecd3e4c4bbdca06f56de Mon Sep 17 00:00:00 2001 From: Braun Patrik Date: Wed, 4 May 2016 21:08:05 +0200 Subject: [PATCH] network service test added --- .../network/autehentication.service.spec.ts | 2 +- .../app/model/network/network.service.spec.ts | 88 +++++++++++++++++++ frontend/app/model/network/network.service.ts | 14 +-- 3 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 frontend/app/model/network/network.service.spec.ts diff --git a/frontend/app/model/network/autehentication.service.spec.ts b/frontend/app/model/network/autehentication.service.spec.ts index 9ce2ae65..88d20313 100644 --- a/frontend/app/model/network/autehentication.service.spec.ts +++ b/frontend/app/model/network/autehentication.service.spec.ts @@ -22,7 +22,7 @@ class MockUserService { } } -describe('LoginService', () => { +describe('AuthenticationService', () => { beforeEachProviders(() => [ provide(UserService, {useClass: MockUserService}), AuthenticationService diff --git a/frontend/app/model/network/network.service.spec.ts b/frontend/app/model/network/network.service.spec.ts new file mode 100644 index 00000000..eccc8e59 --- /dev/null +++ b/frontend/app/model/network/network.service.spec.ts @@ -0,0 +1,88 @@ +/// + +import {it, inject, beforeEachProviders, beforeEach, afterEach} from "@angular/core/testing"; +import {BaseRequestOptions, Http, Response, ResponseOptions} from "@angular/http"; +import {MockBackend, MockConnection} from "@angular/http/testing"; +import {provide} from "@angular/core"; +import "rxjs/Rx"; +import {NetworkService} from "./network.service"; +import {Message} from "../../../../common/entities/Message"; + + +describe('NetworkService', () => { + + let connection:MockConnection = null; + + let testUrl = "/test/url"; + let testData = {data: "testData"}; + let testResponse = "testResponse"; + let testResponseMessage = new Message(null, testResponse); + + beforeEachProviders(() => [ + MockBackend, + BaseRequestOptions, + provide(Http, { + useFactory: (backend, options) => { + return new Http(backend, options); + }, deps: [MockBackend, BaseRequestOptions] + }), + NetworkService + ]); + + beforeEach(inject([MockBackend], (backend) => { + + backend.connections.subscribe((c) => { + connection = c; + connection.mockRespond(new Response( + new ResponseOptions( + { + body: testResponseMessage + } + ))); + }); + })); + + afterEach(() => { + + expect(connection.request.url).toBe("/api"+testUrl); + }); + + + it('should call GET', inject([NetworkService], (networkService) => { + + networkService.getJson(testUrl).then((res:Message) => { + expect(res.result).toBe(testResponse); + }); + + })); + + + it('should call POST', inject([NetworkService, MockBackend], (networkService) => { + + networkService.postJson(testUrl, testData).then((res:Message) => { + expect(res.result).toBe(testResponse); + }); + expect(connection.request.text()).toBe(JSON.stringify(testData)); + })); + + + it('should call PUT', inject([NetworkService, MockBackend], (networkService) => { + + networkService.putJson(testUrl, testData).then((res:Message) => { + expect(res.result).toBe(testResponse); + }); + + expect(connection.request.text()).toBe(JSON.stringify(testData)); + + })); + + + it('should call DELETE', inject([NetworkService, MockBackend], (networkService) => { + + networkService.deleteJson(testUrl).then((res:Message) => { + expect(res.result).toBe(testResponse); + }); + })); + + +}); diff --git a/frontend/app/model/network/network.service.ts b/frontend/app/model/network/network.service.ts index 85122692..78245e38 100644 --- a/frontend/app/model/network/network.service.ts +++ b/frontend/app/model/network/network.service.ts @@ -1,9 +1,11 @@ /// +import {Injectable} from '@angular/core'; import {Http, Headers, RequestOptions} from "@angular/http"; import {Message} from "../../../../common/entities/Message"; -import "rxjs/Rx"; +import "rxjs/Rx"; +@Injectable() export class NetworkService{ _baseUrl = "/api"; @@ -29,20 +31,20 @@ export class NetworkService{ .catch(NetworkService.handleError); } - protected postJson(url:string, data:any = {}): Promise{ + public postJson(url:string, data:any = {}): Promise{ return this.callJson("post",url,data); } - protected putJson(url:string, data:any = {}): Promise{ + public putJson(url:string, data:any = {}): Promise{ return this.callJson("put",url,data); } - protected getJson(url:string): Promise{ + public getJson(url:string): Promise{ return this.callJson("get",url); } - protected deleteJson(url:string, data:any = {}): Promise{ - return this.callJson("delete",url,data); + public deleteJson(url:string): Promise{ + return this.callJson("delete",url); } private static handleError (error: any) {