2016-12-27 06:36:38 +08:00
|
|
|
import {inject, TestBed} from "@angular/core/testing";
|
2016-05-09 23:04:56 +08:00
|
|
|
import {BaseRequestOptions, Http, Response, ResponseOptions} from "@angular/http";
|
2016-05-05 03:08:05 +08:00
|
|
|
import {MockBackend, MockConnection} from "@angular/http/testing";
|
|
|
|
import "rxjs/Rx";
|
|
|
|
import {NetworkService} from "./network.service";
|
|
|
|
import {Message} from "../../../../common/entities/Message";
|
2017-06-23 03:23:26 +08:00
|
|
|
import {SlimLoadingBarService} from "ng2-slim-loading-bar";
|
2016-05-05 03:08:05 +08:00
|
|
|
|
|
|
|
|
2016-05-05 23:51:51 +08:00
|
|
|
describe('NetworkService Success tests', () => {
|
2017-06-11 04:32:56 +08:00
|
|
|
let connection: MockConnection = null;
|
|
|
|
|
|
|
|
let testUrl = "/test/url";
|
|
|
|
let testData = {data: "testData"};
|
|
|
|
let testResponse = "testResponse";
|
|
|
|
let testResponseMessage = new Message(null, testResponse);
|
|
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
providers: [
|
|
|
|
MockBackend,
|
|
|
|
BaseRequestOptions,
|
2017-06-23 03:23:26 +08:00
|
|
|
SlimLoadingBarService,
|
2017-06-11 04:32:56 +08:00
|
|
|
{
|
|
|
|
provide: Http, useFactory: (backend, options) => {
|
|
|
|
return new Http(backend, options);
|
|
|
|
}, deps: [MockBackend, BaseRequestOptions]
|
|
|
|
},
|
|
|
|
NetworkService
|
|
|
|
]
|
2016-12-27 06:36:38 +08:00
|
|
|
});
|
2017-06-11 04:32:56 +08:00
|
|
|
});
|
2016-12-27 06:36:38 +08:00
|
|
|
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
beforeEach(inject([MockBackend], (backend) => {
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
backend.connections.subscribe((c) => {
|
|
|
|
connection = c;
|
|
|
|
connection.mockRespond(new Response(
|
|
|
|
new ResponseOptions(
|
|
|
|
{
|
|
|
|
body: testResponseMessage
|
|
|
|
}
|
|
|
|
)));
|
|
|
|
});
|
|
|
|
}));
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
afterEach(() => {
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
expect(connection.request.url).toBe("/api" + testUrl);
|
|
|
|
});
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
it('should call GET', inject([NetworkService], (networkService) => {
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
networkService.getJson(testUrl).then((res: Message<any>) => {
|
|
|
|
expect(res.result).toBe(testResponse);
|
|
|
|
});
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
}));
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
it('should call POST', inject([NetworkService, MockBackend], (networkService) => {
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
networkService.postJson(testUrl, testData).then((res: Message<any>) => {
|
|
|
|
expect(res.result).toBe(testResponse);
|
|
|
|
});
|
|
|
|
expect(connection.request.text()).toBe(JSON.stringify(testData));
|
2016-05-05 03:08:05 +08:00
|
|
|
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
networkService.postJson(testUrl).then((res: Message<any>) => {
|
|
|
|
expect(res.result).toBe(testResponse);
|
|
|
|
});
|
|
|
|
expect(connection.request.text()).toBe(JSON.stringify({}));
|
|
|
|
}));
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
it('should call PUT', inject([NetworkService, MockBackend], (networkService) => {
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
networkService.putJson(testUrl, testData).then((res: Message<any>) => {
|
|
|
|
expect(res.result).toBe(testResponse);
|
|
|
|
});
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
expect(connection.request.text()).toBe(JSON.stringify(testData));
|
2016-05-05 03:08:05 +08:00
|
|
|
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
networkService.putJson(testUrl).then((res: Message<any>) => {
|
|
|
|
expect(res.result).toBe(testResponse);
|
|
|
|
});
|
|
|
|
expect(connection.request.text()).toBe(JSON.stringify({}));
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
}));
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
it('should call DELETE', inject([NetworkService, MockBackend], (networkService) => {
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
networkService.deleteJson(testUrl).then((res: Message<any>) => {
|
|
|
|
expect(res.result).toBe(testResponse);
|
|
|
|
});
|
|
|
|
}));
|
2016-05-05 23:51:51 +08:00
|
|
|
});
|
2016-05-05 03:08:05 +08:00
|
|
|
|
|
|
|
|
2016-05-05 23:51:51 +08:00
|
|
|
describe('NetworkService Fail tests', () => {
|
2017-06-11 04:32:56 +08:00
|
|
|
let connection: MockConnection = null;
|
|
|
|
|
|
|
|
let testUrl = "/test/url";
|
|
|
|
let testData = {data: "testData"};
|
|
|
|
let testError = "testError";
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
providers: [
|
|
|
|
MockBackend,
|
|
|
|
BaseRequestOptions,
|
2017-06-23 03:23:26 +08:00
|
|
|
SlimLoadingBarService,
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
{
|
|
|
|
provide: Http, useFactory: (backend, options) => {
|
|
|
|
return new Http(backend, options);
|
|
|
|
}, deps: [MockBackend, BaseRequestOptions]
|
|
|
|
},
|
|
|
|
NetworkService
|
|
|
|
]
|
2016-12-27 06:36:38 +08:00
|
|
|
});
|
2017-06-11 04:32:56 +08:00
|
|
|
});
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
beforeEach(inject([MockBackend], (backend) => {
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
backend.connections.subscribe((c) => {
|
|
|
|
connection = c;
|
|
|
|
connection.mockError({name: "errorName", message: testError});
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
});
|
|
|
|
}));
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
afterEach(() => {
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
expect(connection.request.url).toBe("/api" + testUrl);
|
|
|
|
});
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
it('should call GET with error', inject([NetworkService], (networkService) => {
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
networkService.getJson(testUrl).then((res: Message<any>) => {
|
|
|
|
expect(res).toBe(null);
|
|
|
|
}).catch((err) => {
|
|
|
|
expect(err).toBe(testError);
|
|
|
|
});
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
}));
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
it('should call POST with error', inject([NetworkService, MockBackend], (networkService) => {
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
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));
|
|
|
|
}));
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
it('should call PUT with error', inject([NetworkService, MockBackend], (networkService) => {
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
networkService.putJson(testUrl, testData).then((res: Message<any>) => {
|
|
|
|
expect(res).toBe(null);
|
|
|
|
}).catch((err) => {
|
|
|
|
expect(err).toBe(testError);
|
|
|
|
});
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
expect(connection.request.text()).toBe(JSON.stringify(testData));
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
}));
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
it('should call DELETE with error', inject([NetworkService, MockBackend], (networkService) => {
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
networkService.deleteJson(testUrl).then((res: Message<any>) => {
|
|
|
|
expect(res).toBe(null);
|
|
|
|
}).catch((err) => {
|
|
|
|
expect(err).toBe(testError);
|
|
|
|
});
|
|
|
|
}));
|
2017-06-23 03:23:26 +08:00
|
|
|
});
|