From 5bab2dabfc5be17e12192ada6f5a11edfdcde9d3 Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Thu, 19 Mar 2020 16:46:25 +0300 Subject: [PATCH] feat(core): get apiName from config parameter and use it in request --- .../core/src/lib/services/rest.service.ts | 12 +++- .../core/src/lib/tests/rest.service.spec.ts | 56 +++++++++++++++---- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/services/rest.service.ts b/npm/ng-packs/packages/core/src/lib/services/rest.service.ts index 0ad32ba010..15cc13cf50 100644 --- a/npm/ng-packs/packages/core/src/lib/services/rest.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/rest.service.ts @@ -19,10 +19,15 @@ export class RestService { return throwError(err); } - request(request: HttpRequest | Rest.Request, config?: Rest.Config, api?: string): Observable { + request( + request: HttpRequest | Rest.Request, + config?: Rest.Config, + api?: string, + ): Observable { config = config || ({} as Rest.Config); const { observe = Rest.Observe.Body, skipHandleError } = config; - const url = (api || this.store.selectSnapshot(ConfigState.getApiUrl())) + request.url; + const url = + (api || this.store.selectSnapshot(ConfigState.getApiUrl(config.apiName))) + request.url; const { method, params, ...options } = request; return this.http @@ -32,7 +37,8 @@ export class RestService { params: Object.keys(params).reduce( (acc, key) => ({ ...acc, - ...(typeof params[key] !== 'undefined' && params[key] !== '' && { [key]: params[key] }), + ...(typeof params[key] !== 'undefined' && + params[key] !== '' && { [key]: params[key] }), }), {}, ), diff --git a/npm/ng-packs/packages/core/src/lib/tests/rest.service.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/rest.service.spec.ts index 3d5cc11287..ecc8e9dce8 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/rest.service.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/rest.service.spec.ts @@ -1,40 +1,71 @@ +import { ConfigState } from '@abp/ng.core'; import { createHttpFactory, HttpMethod, SpectatorHttp, SpyObject } from '@ngneat/spectator/jest'; -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 { NgxsModule, Store } from '@ngxs/store'; +import { interval, of, Subscription, throwError, timer } from 'rxjs'; +import { catchError } from 'rxjs/operators'; import { RestOccurError } from '../actions'; +import { Rest } from '../models'; +import { RestService } from '../services/rest.service'; describe('HttpClient testing', () => { let spectator: SpectatorHttp; let store: SpyObject; const api = 'https://abp.io'; - const createHttp = createHttpFactory({ dataService: RestService, mocks: [Store] }); + const createHttp = createHttpFactory({ + dataService: RestService, + imports: [NgxsModule.forRoot([ConfigState])], + }); beforeEach(() => { spectator = createHttp(); store = spectator.get(Store); - store.selectSnapshot.andReturn(api); + store.reset({ + ConfigState: { + environment: { + apis: { + default: { + url: api, + }, + foo: { + url: 'bar', + }, + }, + }, + }, + }); }); test('should send a GET request with params', () => { - spectator.service.request({ method: HttpMethod.GET, url: '/test', params: { id: 1 } }).subscribe(); + 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(); + 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.service + .request({ method: HttpMethod.GET, url: '/test' }, null, 'http://test.api') + .subscribe(); spectator.expectOne('http://test.api' + '/test', HttpMethod.GET); }); + test('should use the url of a specific API when apiName is given', () => { + spectator.service + .request({ method: HttpMethod.GET, url: '/test' }, { apiName: 'foo' }) + .subscribe(); + + spectator.expectOne('bar' + '/test', HttpMethod.GET); + }); + test('should close the subscriber when observe equal to body', done => { jest.spyOn(spectator.httpClient, 'request').mockReturnValue(interval(50)); @@ -82,7 +113,10 @@ describe('HttpClient testing', () => { const spy = jest.spyOn(store, 'dispatch'); spectator.service - .request({ method: HttpMethod.GET, url: '/test' }, { observe: Rest.Observe.Events, skipHandleError: true }) + .request( + { method: HttpMethod.GET, url: '/test' }, + { observe: Rest.Observe.Events, skipHandleError: true }, + ) .pipe( catchError(err => { expect(err).toBeTruthy();