11import { ApiBuilder } from "../api-builder" ;
22import fetchMock from "fetch-mock" ;
3- import { ApiRequestBinaryBody , OAuthResponseDto , HttpMethods , ApiRequest } from "../contracts" ;
3+ import { ApiRequestBinaryBody , OAuthResponseDto , HttpMethods , ApiRequest , BaseApiRequest , QueryParams } from "../contracts" ;
44import { OAuthIdentity } from "../identities/oauth-identity" ;
55jest . useFakeTimers ( ) ;
66
7+ interface ApiTestClient {
8+ get : ( requestDto : BaseApiRequest < never > ) => Promise < Response > ;
9+ post : < TBody = { } > ( requestDto : BaseApiRequest < TBody > ) => Promise < Response > ;
10+ put : < TBody = { } > ( requestDto : BaseApiRequest < TBody > ) => Promise < Response > ;
11+ patch : < TBody = { } > ( requestDto : BaseApiRequest < TBody > ) => Promise < Response > ;
12+ delete : < TBody = { } > ( requestDto : BaseApiRequest < TBody > ) => Promise < Response > ;
13+
14+ getItem : ( ) => Promise < Response > ;
15+ }
16+
717const API_TEST_HOST = "https://example.com" ;
818const PATH = "/api" ;
919const PATH_GET = "/get" ;
@@ -25,6 +35,38 @@ const LOGIN_RESPONSE: OAuthResponseDto = {
2535 expires_in : 28800
2636} ;
2737
38+ class ApiClient extends ApiBuilder {
39+ constructor ( identity ?: OAuthIdentity , queryParams ?: QueryParams , queueLimit ?: number , usePath ?: boolean ) {
40+ super ( {
41+ host : API_TEST_HOST ,
42+ path : usePath === false ? undefined : PATH ,
43+ identity : identity ,
44+ defaultQueryParams : queryParams ,
45+ requestQueueLimit : queueLimit
46+ } ) ;
47+ }
48+
49+ public async getItem ( ) : Promise < Response > {
50+ const request : ApiRequest = {
51+ requestPath : PATH_GET ,
52+ method : HttpMethods . GET
53+ } ;
54+ return this . get ( request ) ;
55+ }
56+ }
57+
58+ // FIXME: Temporary solution.
59+ // tslint:disable-next-line:no-any
60+ const ApiTestClient : { new ( ) : ApiTestClient } = ApiClient as any ;
61+ // FIXME: Temporary solution.
62+ // tslint:disable-next-line:no-any
63+ const ApiTestClientIdentity : { new ( identity : OAuthIdentity ) : ApiTestClient } = ApiClient as any ;
64+ const ApiTestClientQueryParams : { new ( identity : undefined , queryParams : QueryParams ) : ApiTestClient } = ApiClient as any ;
65+ const ApiTestClientQueueLimits : { new ( identity : undefined , queryParams : undefined , queueLimit : number ) : ApiTestClient } = ApiClient as any ;
66+ const ApiTestClientNoPath : {
67+ new ( identity : undefined , queryParams : undefined , queueLimit : undefined , usePath : boolean ) : ApiTestClient ;
68+ } = ApiClient as any ;
69+
2870// #region Mocked fetch results.
2971function mockLoginSuccess ( ) : void {
3072 fetchMock . post (
@@ -123,27 +165,17 @@ afterEach(() => {
123165} ) ;
124166
125167it ( "make Get request" , async done => {
126- const apiBuilder = new ApiBuilder ( {
127- host : API_TEST_HOST ,
128- path : PATH
129- } ) ;
168+ const apiClient = new ApiTestClient ( ) ;
130169
131170 mockGetSuccess ( ) ;
132- const request : ApiRequest = {
133- requestPath : PATH_GET ,
134- method : HttpMethods . GET
135- } ;
136- const getExample = await apiBuilder . get ( request ) ;
137171
172+ const getExample = await apiClient . getItem ( ) ;
138173 expect ( getExample . status ) . toEqual ( 200 ) ;
139174 done ( ) ;
140175} ) ;
141176
142177it ( "make Post request" , async done => {
143- const apiBuilder = new ApiBuilder ( {
144- host : API_TEST_HOST ,
145- path : PATH
146- } ) ;
178+ const apiBuilder = new ApiTestClient ( ) ;
147179
148180 mockPostSuccess ( ) ;
149181 const getExample = await apiBuilder . post ( {
@@ -155,10 +187,7 @@ it("make Post request", async done => {
155187} ) ;
156188
157189it ( "make Post request with body of type string" , async done => {
158- const apiBuilder = new ApiBuilder ( {
159- host : API_TEST_HOST ,
160- path : PATH
161- } ) ;
190+ const apiBuilder = new ApiTestClient ( ) ;
162191
163192 mockPostSuccess ( ) ;
164193 const getExample = await apiBuilder . post ( {
@@ -171,10 +200,7 @@ it("make Post request with body of type string", async done => {
171200} ) ;
172201
173202it ( "make Post request with body of type object" , async done => {
174- const apiBuilder = new ApiBuilder ( {
175- host : API_TEST_HOST ,
176- path : PATH
177- } ) ;
203+ const apiBuilder = new ApiTestClient ( ) ;
178204
179205 mockPostSuccess ( ) ;
180206 const getExample = await apiBuilder . post < { name : string ; surname : string } > ( {
@@ -190,10 +216,7 @@ it("make Post request with body of type object", async done => {
190216} ) ;
191217
192218it ( "make Put request" , async done => {
193- const apiBuilder = new ApiBuilder ( {
194- host : API_TEST_HOST ,
195- path : PATH
196- } ) ;
219+ const apiBuilder = new ApiTestClient ( ) ;
197220
198221 mockPutSuccess ( ) ;
199222 const getExample = await apiBuilder . put < { name : string } > ( {
@@ -208,9 +231,7 @@ it("make Put request", async done => {
208231} ) ;
209232
210233it ( "make Get request without config path given" , async done => {
211- const apiBuilder = new ApiBuilder ( {
212- host : API_TEST_HOST
213- } ) ;
234+ const apiBuilder = new ApiTestClientNoPath ( undefined , undefined , undefined , false ) ;
214235
215236 mockGetPathSuccess ( ) ;
216237 const getExample = await apiBuilder . get ( {
@@ -222,11 +243,7 @@ it("make Get request without config path given", async done => {
222243} ) ;
223244
224245it ( "make Get request with queue limits" , async done => {
225- const apiBuilder = new ApiBuilder ( {
226- host : API_TEST_HOST ,
227- path : PATH ,
228- requestQueueLimit : 1
229- } ) ;
246+ const apiBuilder = new ApiTestClientQueueLimits ( undefined , undefined , 1 ) ;
230247
231248 mockGetSuccess ( ) ;
232249 const getExampleOne = apiBuilder . get ( {
@@ -245,10 +262,7 @@ it("make Get request with queue limits", async done => {
245262} ) ;
246263
247264it ( "make forced Get request" , async done => {
248- const apiBuilder = new ApiBuilder ( {
249- host : API_TEST_HOST ,
250- path : PATH
251- } ) ;
265+ const apiBuilder = new ApiTestClient ( ) ;
252266
253267 mockGetSuccess ( ) ;
254268 const getExample = await apiBuilder . get ( {
@@ -261,13 +275,10 @@ it("make forced Get request", async done => {
261275} ) ;
262276
263277it ( "make Get request with api configuration query params" , async done => {
264- const apiBuilder = new ApiBuilder ( {
265- host : API_TEST_HOST ,
266- path : PATH ,
267- defaultQueryParams : {
268- page : 2
269- }
270- } ) ;
278+ const queryParams : QueryParams = {
279+ page : 2
280+ } ;
281+ const apiBuilder = new ApiTestClientQueryParams ( undefined , queryParams ) ;
271282
272283 mockGetQueryParamsPathSuccess ( ) ;
273284 const getExample = await apiBuilder . get ( {
@@ -280,10 +291,7 @@ it("make Get request with api configuration query params", async done => {
280291} ) ;
281292
282293it ( "make Get request with request query params" , async done => {
283- const apiBuilder = new ApiBuilder ( {
284- host : API_TEST_HOST ,
285- path : PATH
286- } ) ;
294+ const apiBuilder = new ApiTestClient ( ) ;
287295
288296 mockGetQueryParamsPathSuccess ( ) ;
289297 const getExample = await apiBuilder . get ( {
@@ -299,10 +307,7 @@ it("make Get request with request query params", async done => {
299307} ) ;
300308
301309it ( "make Post request with Uint8Array body" , async done => {
302- const apiBuilder = new ApiBuilder ( {
303- host : API_TEST_HOST ,
304- path : PATH
305- } ) ;
310+ const apiBuilder = new ApiTestClient ( ) ;
306311
307312 mockPostSuccess ( ) ;
308313 const getExample = await apiBuilder . post < ApiRequestBinaryBody > ( {
@@ -318,10 +323,7 @@ it("make Post request with Uint8Array body", async done => {
318323} ) ;
319324
320325it ( "make Delete request" , async done => {
321- const apiBuilder = new ApiBuilder ( {
322- host : API_TEST_HOST ,
323- path : PATH
324- } ) ;
326+ const apiBuilder = new ApiTestClient ( ) ;
325327
326328 mockDeleteSuccess ( ) ;
327329 const getExample = await apiBuilder . delete < { id : number } > ( {
@@ -336,10 +338,7 @@ it("make Delete request", async done => {
336338} ) ;
337339
338340it ( "make Patch request" , async done => {
339- const apiBuilder = new ApiBuilder ( {
340- host : API_TEST_HOST ,
341- path : PATH
342- } ) ;
341+ const apiBuilder = new ApiTestClient ( ) ;
343342
344343 mockPatchSuccess ( ) ;
345344 const getExample = await apiBuilder . patch < { id : number } > ( {
@@ -363,11 +362,7 @@ it("authenticated request", async done => {
363362 mockLoginSuccess ( ) ;
364363 await identity . login ( "" , "" ) ;
365364
366- const apiBuilder = new ApiBuilder ( {
367- host : API_TEST_HOST ,
368- path : PATH ,
369- identity : identity
370- } ) ;
365+ const apiBuilder = new ApiTestClientIdentity ( identity ) ;
371366
372367 mockGetSuccess ( ) ;
373368 const getExample = await apiBuilder . get ( {
@@ -389,11 +384,7 @@ it("authenticated request but failed", async done => {
389384 mockLoginSuccess ( ) ;
390385 await identity . login ( "" , "" ) ;
391386
392- const apiBuilder = new ApiBuilder ( {
393- host : API_TEST_HOST ,
394- path : PATH ,
395- identity : identity
396- } ) ;
387+ const apiBuilder = new ApiTestClientIdentity ( identity ) ;
397388
398389 mockGetAuthFailed ( ) ;
399390 mockLogoutSuccess ( ) ;
@@ -419,11 +410,7 @@ it("authenticated request and then logout", async done => {
419410 mockLoginSuccess ( ) ;
420411 await identity . login ( "" , "" ) ;
421412
422- const apiBuilder = new ApiBuilder ( {
423- host : API_TEST_HOST ,
424- path : PATH ,
425- identity : identity
426- } ) ;
413+ const apiBuilder = new ApiTestClientIdentity ( identity ) ;
427414
428415 mockGetSuccess ( ) ;
429416 const getExample = await apiBuilder . get ( {
0 commit comments