1
- import { HttpRequest , HttpMethod } from 'simple-http-request-builder' ;
2
- import ApiHttpClient from './ApiHttpClient' ;
3
- import { genericError , networkError , timeoutError } from '../lib/client/HttpResponse' ;
1
+ import { HttpMethod , HttpRequest } from 'simple-http-request-builder' ;
4
2
import { fetchClient } from '../lib/client/FetchClient' ;
5
- import { unwrapHttpPromise } from '../lib/promise/HttpPromise' ;
3
+ import {
4
+ genericError ,
5
+ HttpError ,
6
+ HttpResponse ,
7
+ networkError ,
8
+ timeoutError ,
9
+ toErrorResponsePromise ,
10
+ } from '../lib/client/HttpResponse' ;
11
+ import { HttpPromise , unwrapHttpPromise } from '../lib/promise/HttpPromise' ;
12
+ import ApiHttpClient from './ApiHttpClient' ;
6
13
7
14
const waitTimeout = ( durationInMillis : number ) => new Promise (
8
15
// eslint-disable-next-line no-promise-executor-return
@@ -14,6 +21,9 @@ const mockedFetchResponseStatus: ResponseInit = {
14
21
status : 200 ,
15
22
statusText : 'Ok' ,
16
23
// headers
24
+ headers : {
25
+ 'content-type' : 'application/json' ,
26
+ } ,
17
27
} ;
18
28
let mockedPromiseError : Error | undefined ;
19
29
@@ -28,6 +38,19 @@ const mockedFetch = (): Promise<Response> => {
28
38
return Promise . resolve ( new Response ( mockedFetchResponseBody , mockedFetchResponseStatus ) ) ;
29
39
} ;
30
40
41
+ type MockUser = { id : number } ;
42
+
43
+ const executeGetUserRequest = ( mockUser : MockUser ) : HttpPromise < MockUser > => {
44
+ // Mock body
45
+ setMockedBody ( mockUser ) ;
46
+ // Add client
47
+ const apiClient : ApiHttpClient = new ApiHttpClient ( ) ;
48
+ // Execute request
49
+ return apiClient
50
+ . restRequest < MockUser > ( HttpMethod . GET , 'https://hostname/users' )
51
+ . execute ( ) ;
52
+ } ;
53
+
31
54
describe ( 'Tests fetch client' , ( ) => {
32
55
const oldFetch = global . fetch ;
33
56
global . fetch = mockedFetch ;
@@ -77,7 +100,9 @@ describe('Tests fetch client', () => {
77
100
78
101
test ( 'Check handler execution error rejects promise with genericError' , async ( ) => {
79
102
const httpClientWithErrorHandler = ( httpRequest : HttpRequest < unknown > ) => unwrapHttpPromise ( fetchClient (
80
- httpRequest , ( ) => { throw new Error ( ) ; } ,
103
+ httpRequest , ( ) => {
104
+ throw new Error ( ) ;
105
+ } ,
81
106
) ) ;
82
107
const response = new HttpRequest (
83
108
httpClientWithErrorHandler ,
@@ -87,4 +112,80 @@ describe('Tests fetch client', () => {
87
112
) . execute ( ) ;
88
113
await expect ( response ) . rejects . toEqual ( genericError ) ;
89
114
} ) ;
115
+
116
+ test ( 'Check return type of rest request must be User' , async ( ) => {
117
+ const mockFirstUser : MockUser = { id : 1 } ;
118
+ const result : MockUser = await executeGetUserRequest ( mockFirstUser ) ;
119
+ expect ( result . id ) . toEqual ( 1 ) ;
120
+ } ) ;
121
+
122
+ test ( 'Check return type of then function must be unwrap in the next then call' , async ( ) => {
123
+ // Mock body
124
+ const mockFirstUser : MockUser = { id : 1 } ;
125
+ const mockSecondUser : MockUser = { id : 2 } ;
126
+
127
+ // Add client
128
+ // Execute request in request
129
+ const result : MockUser = await executeGetUserRequest ( mockFirstUser )
130
+ . then ( ( ) => executeGetUserRequest ( mockSecondUser ) ) ;
131
+ expect ( result . id ) . toEqual ( 2 ) ;
132
+ } ) ;
133
+
134
+ test ( 'Check return type of catch function must be unwrap in the next then call' , async ( ) => {
135
+ const httpClientWithErrorHandler = (
136
+ httpRequest : HttpRequest < unknown > ,
137
+ ) : Promise < HttpResponse < MockUser > > => fetchClient (
138
+ httpRequest ,
139
+ ( ) => {
140
+ const httpError : HttpError = {
141
+ errorCode : 'INTERNAL_ERROR' ,
142
+ } ;
143
+ return toErrorResponsePromise < HttpError > ( httpError ) ;
144
+ } ,
145
+ ) ;
146
+
147
+ const result : HttpError = await new HttpRequest < HttpPromise < MockUser > > (
148
+ ( httpRequest : HttpRequest < unknown > ) => new HttpPromise < MockUser > (
149
+ unwrapHttpPromise ( httpClientWithErrorHandler ( httpRequest ) ) ,
150
+ httpRequest ,
151
+ ) ,
152
+ 'http://localhost' ,
153
+ HttpMethod . GET ,
154
+ '/users' ,
155
+ )
156
+ . execute ( )
157
+ . catch ( ( httpError : HttpError ) => httpError ) ;
158
+
159
+ expect ( result . errorCode ) . toEqual ( 'INTERNAL_ERROR' ) ;
160
+ } ) ;
161
+
162
+ test ( 'Check return type of catch function must be unwrap in the next then call' , async ( ) => {
163
+ const mockFirstUser : MockUser = { id : 1 } ;
164
+
165
+ const httpClientWithErrorHandler = (
166
+ httpRequest : HttpRequest < unknown > ,
167
+ ) : Promise < HttpResponse < MockUser > > => fetchClient (
168
+ httpRequest ,
169
+ ( ) => {
170
+ const httpError : HttpError = {
171
+ errorCode : 'INTERNAL_ERROR' ,
172
+ } ;
173
+ return toErrorResponsePromise < HttpError > ( httpError ) ;
174
+ } ,
175
+ ) ;
176
+
177
+ const result : MockUser = await new HttpRequest < HttpPromise < MockUser > > (
178
+ ( httpRequest : HttpRequest < unknown > ) => new HttpPromise < MockUser > (
179
+ unwrapHttpPromise ( httpClientWithErrorHandler ( httpRequest ) ) ,
180
+ httpRequest ,
181
+ ) ,
182
+ 'http://localhost' ,
183
+ HttpMethod . GET ,
184
+ '/users' ,
185
+ )
186
+ . execute ( )
187
+ . catch ( ( ) => executeGetUserRequest ( mockFirstUser ) ) ;
188
+
189
+ expect ( result . id ) . toEqual ( 1 ) ;
190
+ } ) ;
90
191
} ) ;
0 commit comments