mirror of
https://github.com/xuthus83/pigallery2.git
synced 2024-11-03 21:04:03 +08:00
network service test added
This commit is contained in:
parent
6949a8c1e5
commit
10b82f224d
@ -22,7 +22,7 @@ class MockUserService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('LoginService', () => {
|
describe('AuthenticationService', () => {
|
||||||
beforeEachProviders(() => [
|
beforeEachProviders(() => [
|
||||||
provide(UserService, {useClass: MockUserService}),
|
provide(UserService, {useClass: MockUserService}),
|
||||||
AuthenticationService
|
AuthenticationService
|
||||||
|
88
frontend/app/model/network/network.service.spec.ts
Normal file
88
frontend/app/model/network/network.service.spec.ts
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
///<reference path="../../../browser.d.ts"/>
|
||||||
|
|
||||||
|
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<any>) => {
|
||||||
|
expect(res.result).toBe(testResponse);
|
||||||
|
});
|
||||||
|
|
||||||
|
}));
|
||||||
|
|
||||||
|
|
||||||
|
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));
|
||||||
|
}));
|
||||||
|
|
||||||
|
|
||||||
|
it('should call PUT', inject([NetworkService, MockBackend], (networkService) => {
|
||||||
|
|
||||||
|
networkService.putJson(testUrl, testData).then((res:Message<any>) => {
|
||||||
|
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<any>) => {
|
||||||
|
expect(res.result).toBe(testResponse);
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
|
||||||
|
|
||||||
|
});
|
@ -1,9 +1,11 @@
|
|||||||
///<reference path="../../../browser.d.ts"/>
|
///<reference path="../../../browser.d.ts"/>
|
||||||
|
|
||||||
|
import {Injectable} from '@angular/core';
|
||||||
import {Http, Headers, RequestOptions} from "@angular/http";
|
import {Http, Headers, RequestOptions} from "@angular/http";
|
||||||
import {Message} from "../../../../common/entities/Message";
|
import {Message} from "../../../../common/entities/Message";
|
||||||
import "rxjs/Rx";
|
import "rxjs/Rx";
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
export class NetworkService{
|
export class NetworkService{
|
||||||
|
|
||||||
_baseUrl = "/api";
|
_baseUrl = "/api";
|
||||||
@ -29,20 +31,20 @@ export class NetworkService{
|
|||||||
.catch(NetworkService.handleError);
|
.catch(NetworkService.handleError);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected postJson<T>(url:string, data:any = {}): Promise<T>{
|
public postJson<T>(url:string, data:any = {}): Promise<T>{
|
||||||
return this.callJson("post",url,data);
|
return this.callJson("post",url,data);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected putJson<T>(url:string, data:any = {}): Promise<T>{
|
public putJson<T>(url:string, data:any = {}): Promise<T>{
|
||||||
return this.callJson("put",url,data);
|
return this.callJson("put",url,data);
|
||||||
}
|
}
|
||||||
protected getJson<T>(url:string): Promise<T>{
|
public getJson<T>(url:string): Promise<T>{
|
||||||
return this.callJson("get",url);
|
return this.callJson("get",url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected deleteJson<T>(url:string, data:any = {}): Promise<T>{
|
public deleteJson<T>(url:string): Promise<T>{
|
||||||
return this.callJson("delete",url,data);
|
return this.callJson("delete",url);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static handleError (error: any) {
|
private static handleError (error: any) {
|
||||||
|
Loading…
Reference in New Issue
Block a user