2018-03-31 03:30:30 +08:00
|
|
|
import {getTestBed, inject, TestBed} from '@angular/core/testing';
|
|
|
|
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
|
|
|
|
import {NetworkService} from './network.service';
|
|
|
|
import {Message} from '../../../../common/entities/Message';
|
|
|
|
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
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
const testUrl = '/test/url';
|
|
|
|
const testData = {data: 'testData'};
|
|
|
|
const testResponse = 'testResponse';
|
|
|
|
const testResponseMessage = new Message(null, testResponse);
|
|
|
|
let injector;
|
|
|
|
let httpMock: HttpTestingController;
|
2017-06-11 04:32:56 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule({
|
2018-03-31 03:30:30 +08:00
|
|
|
imports: [HttpClientTestingModule],
|
2017-06-11 04:32:56 +08:00
|
|
|
providers: [
|
2017-06-23 03:23:26 +08:00
|
|
|
SlimLoadingBarService,
|
2017-06-11 04:32:56 +08:00
|
|
|
NetworkService
|
|
|
|
]
|
2016-12-27 06:36:38 +08:00
|
|
|
});
|
2018-03-31 03:30:30 +08:00
|
|
|
injector = getTestBed();
|
|
|
|
httpMock = injector.get(HttpTestingController);
|
2017-06-11 04:32:56 +08:00
|
|
|
});
|
2016-12-27 06:36:38 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
afterEach(() => {
|
2018-03-31 03:30:30 +08:00
|
|
|
httpMock.verify();
|
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 GET', inject([NetworkService], (networkService) => {
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
networkService.getJson(testUrl).then((res: string) => {
|
2017-07-04 01:39:09 +08:00
|
|
|
expect(res).toBe(testResponse);
|
2018-03-31 03:30:30 +08:00
|
|
|
}).catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
expect(err).toBeUndefined();
|
2017-06-11 04:32:56 +08:00
|
|
|
});
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
const mockReq = httpMock.expectOne({method: 'GET'});
|
|
|
|
expect(mockReq.cancelled).toBeFalsy();
|
|
|
|
expect(mockReq.request.responseType).toEqual('json');
|
|
|
|
mockReq.flush(testResponseMessage);
|
2017-06-11 04:32:56 +08:00
|
|
|
}));
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
it('should call POST', inject([NetworkService], (networkService) => {
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
networkService.postJson(testUrl, testData).then((res: string) => {
|
2017-07-04 01:39:09 +08:00
|
|
|
expect(res).toBe(testResponse);
|
2018-03-31 03:30:30 +08:00
|
|
|
}).catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
expect(err).toBeUndefined();
|
2017-06-11 04:32:56 +08:00
|
|
|
});
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
let mockReq = httpMock.expectOne('/api' + testUrl);
|
|
|
|
expect(mockReq.cancelled).toBeFalsy();
|
|
|
|
expect(mockReq.request.responseType).toEqual('json');
|
|
|
|
mockReq.flush(testResponseMessage);
|
|
|
|
expect(mockReq.request.body).toBe(testData);
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
networkService.postJson(testUrl).then((res: string) => {
|
2017-07-04 01:39:09 +08:00
|
|
|
expect(res).toBe(testResponse);
|
2018-03-31 03:30:30 +08:00
|
|
|
}).catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
expect(err).toBeUndefined();
|
2017-06-11 04:32:56 +08:00
|
|
|
});
|
2018-03-31 03:30:30 +08:00
|
|
|
|
|
|
|
mockReq = httpMock.expectOne('/api' + testUrl);
|
|
|
|
expect(mockReq.cancelled).toBeFalsy();
|
|
|
|
expect(mockReq.request.responseType).toEqual('json');
|
|
|
|
expect(mockReq.request.body).toEqual({});
|
|
|
|
mockReq.flush(testResponseMessage);
|
|
|
|
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
}));
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
it('should call DELETE', inject([NetworkService], (networkService) => {
|
|
|
|
|
|
|
|
networkService.deleteJson(testUrl).then((res: any) => {
|
2017-07-04 01:39:09 +08:00
|
|
|
expect(res).toBe(testResponse);
|
2017-06-11 04:32:56 +08:00
|
|
|
});
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
const mockReq = httpMock.expectOne({method: 'DELETE'});
|
|
|
|
expect(mockReq.cancelled).toBeFalsy();
|
|
|
|
expect(mockReq.request.responseType).toEqual('json');
|
|
|
|
mockReq.flush(testResponseMessage);
|
|
|
|
}));
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
it('should call PUT', inject([NetworkService], (networkService) => {
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
networkService.putJson(testUrl, testData).then((res: any) => {
|
2017-07-04 01:39:09 +08:00
|
|
|
expect(res).toBe(testResponse);
|
2017-06-11 04:32:56 +08:00
|
|
|
});
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
let mockReq = httpMock.expectOne({});
|
|
|
|
expect(mockReq.cancelled).toBeFalsy();
|
|
|
|
expect(mockReq.request.responseType).toEqual('json');
|
|
|
|
expect(mockReq.request.body).toEqual(testData);
|
|
|
|
mockReq.flush(testResponseMessage);
|
2016-05-05 03:08:05 +08:00
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
|
|
|
|
networkService.putJson(testUrl).then((res: any) => {
|
2017-07-04 01:39:09 +08:00
|
|
|
expect(res).toBe(testResponse);
|
2017-06-11 04:32:56 +08:00
|
|
|
});
|
2018-03-31 03:30:30 +08:00
|
|
|
|
|
|
|
mockReq = httpMock.expectOne({});
|
|
|
|
expect(mockReq.cancelled).toBeFalsy();
|
|
|
|
expect(mockReq.request.responseType).toEqual('json');
|
|
|
|
expect(mockReq.request.body).toEqual({});
|
|
|
|
mockReq.flush(testResponseMessage);
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
}));
|
2018-03-31 03:30:30 +08:00
|
|
|
|
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
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
const testUrl = '/test/url';
|
|
|
|
const testData = {data: 'testData'};
|
|
|
|
const testError = 'testError';
|
|
|
|
let injector;
|
|
|
|
let httpMock: HttpTestingController;
|
2017-06-11 04:32:56 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule({
|
2018-03-31 03:30:30 +08:00
|
|
|
imports: [HttpClientTestingModule],
|
2017-06-11 04:32:56 +08:00
|
|
|
providers: [
|
2017-06-23 03:23:26 +08:00
|
|
|
SlimLoadingBarService,
|
2017-06-11 04:32:56 +08:00
|
|
|
NetworkService
|
|
|
|
]
|
2016-12-27 06:36:38 +08:00
|
|
|
});
|
2018-03-31 03:30:30 +08:00
|
|
|
injector = getTestBed();
|
|
|
|
httpMock = injector.get(HttpTestingController);
|
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(() => {
|
2018-03-31 03:30:30 +08:00
|
|
|
httpMock.verify();
|
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 GET with error', inject([NetworkService], (networkService) => {
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
networkService.getJson(testUrl).then((res: any) => {
|
2017-06-11 04:32:56 +08:00
|
|
|
expect(res).toBe(null);
|
|
|
|
}).catch((err) => {
|
2018-03-31 03:30:30 +08:00
|
|
|
expect(err).toBe('Http failure response for /api/test/url: 0 ' + testError);
|
2017-06-11 04:32:56 +08:00
|
|
|
});
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
|
|
|
|
const mockReq = httpMock.expectOne({method: 'GET'});
|
|
|
|
expect(mockReq.cancelled).toBeFalsy();
|
|
|
|
expect(mockReq.request.responseType).toEqual('json');
|
|
|
|
mockReq.error(null, {statusText: testError});
|
2017-06-11 04:32:56 +08:00
|
|
|
}));
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
it('should call POST with error', inject([NetworkService], (networkService) => {
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
networkService.postJson(testUrl, testData).then((res: any) => {
|
2017-06-11 04:32:56 +08:00
|
|
|
expect(res).toBe(null);
|
|
|
|
}).catch((err) => {
|
2018-03-31 03:30:30 +08:00
|
|
|
expect(err).toBe('Http failure response for /api/test/url: 0 ' + testError);
|
2017-06-11 04:32:56 +08:00
|
|
|
});
|
2018-03-31 03:30:30 +08:00
|
|
|
|
|
|
|
const mockReq = httpMock.expectOne({method: 'POST'});
|
|
|
|
expect(mockReq.cancelled).toBeFalsy();
|
|
|
|
expect(mockReq.request.responseType).toEqual('json');
|
|
|
|
expect(mockReq.request.body).toEqual(testData);
|
|
|
|
mockReq.error(null, {statusText: testError});
|
2017-06-11 04:32:56 +08:00
|
|
|
}));
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
it('should call PUT with error', inject([NetworkService], (networkService) => {
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
networkService.putJson(testUrl, testData).then((res: any) => {
|
2017-06-11 04:32:56 +08:00
|
|
|
expect(res).toBe(null);
|
|
|
|
}).catch((err) => {
|
2018-03-31 03:30:30 +08:00
|
|
|
expect(err).toBe('Http failure response for /api/test/url: 0 ' + testError);
|
2017-06-11 04:32:56 +08:00
|
|
|
});
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
|
|
|
|
const mockReq = httpMock.expectOne({method: 'PUT'});
|
|
|
|
expect(mockReq.cancelled).toBeFalsy();
|
|
|
|
expect(mockReq.request.responseType).toEqual('json');
|
|
|
|
expect(mockReq.request.body).toEqual(testData);
|
|
|
|
mockReq.error(null, {statusText: 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
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
it('should call DELETE with error', inject([NetworkService], (networkService) => {
|
2016-05-05 23:51:51 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
networkService.deleteJson(testUrl).then((res: any) => {
|
2017-06-11 04:32:56 +08:00
|
|
|
expect(res).toBe(null);
|
|
|
|
}).catch((err) => {
|
2018-03-31 03:30:30 +08:00
|
|
|
expect(err).toBe('Http failure response for /api/test/url: 0 ' + testError);
|
2017-06-11 04:32:56 +08:00
|
|
|
});
|
2018-03-31 03:30:30 +08:00
|
|
|
|
|
|
|
const mockReq = httpMock.expectOne({method: 'DELETE'});
|
|
|
|
expect(mockReq.cancelled).toBeFalsy();
|
|
|
|
expect(mockReq.request.responseType).toEqual('json');
|
|
|
|
mockReq.error(null, {statusText: testError});
|
2017-06-11 04:32:56 +08:00
|
|
|
}));
|
2017-06-23 03:23:26 +08:00
|
|
|
});
|