Merge pull request #1886 from abpframework/test/rest-service

test(core): add rest.service tests
pull/1895/head
Yasin Aydın 6 years ago committed by GitHub
commit 7f5979145a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,7 +19,8 @@ export class RestService {
return throwError(err);
}
request<T, R>(request: HttpRequest<T> | Rest.Request<T>, config: Rest.Config = {}, api?: string): Observable<R> {
request<T, R>(request: HttpRequest<T> | Rest.Request<T>, config?: Rest.Config, api?: string): Observable<R> {
config = config || ({} as Rest.Config);
const { observe = Rest.Observe.Body, skipHandleError } = config;
const url = (api || this.store.selectSnapshot(ConfigState.getApiUrl())) + request.url;
const { method, ...options } = request;

@ -1,6 +1,10 @@
import { createHttpFactory, HttpMethod, SpectatorHttp, SpyObject } from '@ngneat/spectator/jest';
import { RestService } from '../services/rest.service';
import { Store } from '@ngxs/store';
import { Subscription, timer, interval, throwError, of } from 'rxjs';
import { mapTo, catchError } from 'rxjs/operators';
import { RestService } from '../services/rest.service';
import { Rest } from '../models';
import { RestOccurError } from '../actions';
describe('HttpClient testing', () => {
let spectator: SpectatorHttp<RestService>;
@ -19,4 +23,73 @@ describe('HttpClient testing', () => {
spectator.service.request({ method: HttpMethod.GET, url: '/test', params: { id: 1 } }).subscribe();
spectator.expectOne(api + '/test?id=1', HttpMethod.GET);
});
test('should send a POST request with body', () => {
spectator.service.request({ method: HttpMethod.POST, url: '/test', body: { id: 1 } }).subscribe();
const req = spectator.expectOne(api + '/test', HttpMethod.POST);
expect(req.request.body['id']).toEqual(1);
});
test('should use the specific api', () => {
spectator.service.request({ method: HttpMethod.GET, url: '/test' }, null, 'http://test.api').subscribe();
spectator.expectOne('http://test.api' + '/test', HttpMethod.GET);
});
test('should close the subscriber when observe equal to body', done => {
jest.spyOn(spectator.httpClient, 'request').mockReturnValue(interval(50));
const subscriber: Subscription = spectator.service
.request({ method: HttpMethod.GET, url: '/test' }, { observe: Rest.Observe.Body })
.subscribe();
timer(51).subscribe(() => {
expect(subscriber.closed).toBe(true);
done();
});
});
test('should open the subscriber when observe not equal to body', done => {
jest.spyOn(spectator.httpClient, 'request').mockReturnValue(interval(50));
const subscriber: Subscription = spectator.service
.request({ method: HttpMethod.GET, url: '/test' }, { observe: Rest.Observe.Events })
.subscribe();
timer(51).subscribe(() => {
expect(subscriber.closed).toBe(false);
done();
});
});
test('should handle the error', () => {
jest.spyOn(spectator.httpClient, 'request').mockReturnValue(throwError('An error'));
const spy = jest.spyOn(store, 'dispatch');
spectator.service
.request({ method: HttpMethod.GET, url: '/test' }, { observe: Rest.Observe.Events })
.pipe(
catchError(err => {
expect(err).toBeTruthy();
expect(spy.mock.calls[0][0] instanceof RestOccurError).toBe(true);
return of(null);
}),
)
.subscribe();
});
test('should not handle the error when skipHandleError is true', () => {
jest.spyOn(spectator.httpClient, 'request').mockReturnValue(throwError('An error'));
const spy = jest.spyOn(store, 'dispatch');
spectator.service
.request({ method: HttpMethod.GET, url: '/test' }, { observe: Rest.Observe.Events, skipHandleError: true })
.pipe(
catchError(err => {
expect(err).toBeTruthy();
expect(spy.mock.calls).toHaveLength(0);
return of(null);
}),
)
.subscribe();
});
});

Loading…
Cancel
Save