1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2025-01-14 14:43:17 +08:00

implementing network fail tests, refactoring services

This commit is contained in:
Braun Patrik 2016-05-05 17:51:51 +02:00
parent 936bf15f28
commit 3b0bcc44b0
5 changed files with 107 additions and 28 deletions

View File

@ -7,28 +7,27 @@ import {Message} from "../../../common/entities/Message";
import {User} from "../../../common/entities/User";
@Injectable()
export class AdminService extends NetworkService{
export class AdminService {
constructor(_http:Http){
super(_http);
constructor(private _networkService:NetworkService){
}
public createUser(user:User): Promise<Message<string>>{
return this.putJson("/user",{newUser:user});
return this._networkService.putJson("/user",{newUser:user});
}
public getUsers():Promise<Message<Array<User>>>{
return this.getJson("/user/list");
return this._networkService.getJson("/user/list");
}
public deleteUser(user:User) {
return this.deleteJson("/user/"+user.id);
return this._networkService.deleteJson("/user/"+user.id);
}
public updateRole(user:User) {
return this.postJson("/user/"+user.id+"/role",{newRole:user.role});
return this._networkService.postJson("/user/"+user.id+"/role",{newRole:user.role});
}
}

View File

@ -7,15 +7,15 @@ import {Message} from "../../../common/entities/Message";
import {Directory} from "../../../common/entities/Directory";
@Injectable()
export class GalleryService extends NetworkService{
export class GalleryService{
constructor(_http:Http){
super(_http);
constructor(private _networkService:NetworkService){
}
public getDirectory(directoryName:string): Promise<Message<Directory>>{
return this.getJson("/gallery/content/"+directoryName);
return this._networkService.getJson("/gallery/content/"+directoryName);
}

View File

@ -7,15 +7,14 @@ import {AutoCompleteItem} from "../../../../common/entities/AutoCompleteItem";
import {Message} from "../../../../common/entities/Message";
@Injectable()
export class AutoCompleteService extends NetworkService {
export class AutoCompleteService {
constructor(_http:Http) {
super(_http);
constructor(private _networkService:NetworkService){
}
public autoComplete(text:string): Promise<Message<Array<AutoCompleteItem> >> {
return this.getJson("/gallery/autocomplete/"+text);
return this._networkService.getJson("/gallery/autocomplete/"+text);
}

View File

@ -1,7 +1,7 @@
///<reference path="../../../browser.d.ts"/>
import {it, inject, beforeEachProviders, beforeEach, afterEach} from "@angular/core/testing";
import {BaseRequestOptions, Http, Response, ResponseOptions} from "@angular/http";
import {BaseRequestOptions, Http, Response, ResponseOptions, ResponseType, BaseResponseOptions} from "@angular/http";
import {MockBackend, MockConnection} from "@angular/http/testing";
import {provide} from "@angular/core";
import "rxjs/Rx";
@ -9,8 +9,7 @@ import {NetworkService} from "./network.service";
import {Message} from "../../../../common/entities/Message";
describe('NetworkService', () => {
describe('NetworkService Success tests', () => {
let connection:MockConnection = null;
let testUrl = "/test/url";
@ -44,10 +43,9 @@ describe('NetworkService', () => {
afterEach(() => {
expect(connection.request.url).toBe("/api"+testUrl);
expect(connection.request.url).toBe("/api" + testUrl);
});
it('should call GET', inject([NetworkService], (networkService) => {
networkService.getJson(testUrl).then((res:Message<any>) => {
@ -56,16 +54,20 @@ describe('NetworkService', () => {
}));
it('should call POST', inject([NetworkService, MockBackend], (networkService) => {
networkService.postJson(testUrl, testData).then((res:Message<any>) => {
expect(res.result).toBe(testResponse);
});
expect(connection.request.text()).toBe(JSON.stringify(testData));
}));
networkService.postJson(testUrl).then((res:Message<any>) => {
expect(res.result).toBe(testResponse);
});
expect(connection.request.text()).toBe(JSON.stringify({}));
}));
it('should call PUT', inject([NetworkService, MockBackend], (networkService) => {
networkService.putJson(testUrl, testData).then((res:Message<any>) => {
@ -74,8 +76,13 @@ describe('NetworkService', () => {
expect(connection.request.text()).toBe(JSON.stringify(testData));
}));
networkService.putJson(testUrl).then((res:Message<any>) => {
expect(res.result).toBe(testResponse);
});
expect(connection.request.text()).toBe(JSON.stringify({}));
}));
it('should call DELETE', inject([NetworkService, MockBackend], (networkService) => {
@ -83,6 +90,79 @@ describe('NetworkService', () => {
expect(res.result).toBe(testResponse);
});
}));
});
describe('NetworkService Fail tests', () => {
let connection:MockConnection = null;
let testUrl = "/test/url";
let testData = {data: "testData"};
let testError = "testError";
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.mockError({name :"errorName",message:testError});
});
}));
afterEach(() => {
expect(connection.request.url).toBe("/api" + testUrl);
});
it('should call GET with error', inject([NetworkService], (networkService) => {
networkService.getJson(testUrl).then((res:Message<any>) => {
expect(res).toBe(null);
}).catch((err) => {
expect(err).toBe(testError);
});
}));
it('should call POST with error', inject([NetworkService, MockBackend], (networkService) => {
networkService.postJson(testUrl, testData).then((res:Message<any>) => {
expect(res).toBe(null);
}).catch((err) => {
expect(err).toBe(testError);
});
expect(connection.request.text()).toBe(JSON.stringify(testData));
}));
it('should call PUT with error', inject([NetworkService, MockBackend], (networkService) => {
networkService.putJson(testUrl, testData).then((res:Message<any>) => {
expect(res).toBe(null);
}).catch((err) => {
expect(err).toBe(testError);
});
expect(connection.request.text()).toBe(JSON.stringify(testData));
}));
it('should call DELETE with error', inject([NetworkService, MockBackend], (networkService) => {
networkService.deleteJson(testUrl).then((res:Message<any>) => {
expect(res).toBe(null);
}).catch((err) => {
expect(err).toBe(testError);
});
}));
});

View File

@ -38,6 +38,7 @@ export class NetworkService{
public putJson<T>(url:string, data:any = {}): Promise<T>{
return this.callJson("put",url,data);
}
public getJson<T>(url:string): Promise<T>{
return this.callJson("get",url);
}
@ -48,7 +49,7 @@ export class NetworkService{
}
private static handleError (error: any) {
// in a real world app, we may send the error to some remote logging infrastructure
// TODO: in a real world app, we may send the error to some remote logging infrastructure
// instead of just logging it to the console
console.error(error);
return Promise.reject(error.message || error.json().error || 'Server error');