1- import fetchMock from 'fetch-mock' ;
2- import { Response , Headers } from 'node-fetch' ;
1+ import { Headers , Response } from 'node-fetch' ;
32
43import {
5- HTTP ,
4+ addHttpResponseInterceptor ,
65 getDefaultConfig ,
7- setDefaultConfig ,
6+ HTTP ,
87 HTTP_RESPONSE_INTERCEPTORS ,
9- addHttpResponseInterceptor ,
8+ setDefaultConfig ,
109} from './config' ;
11- import { httpFetch , handleBody , encodePayload , handleHttpResponse } from './http.common' ;
10+ import { encodePayload , handleBody , handleHttpResponse , httpFetch } from './http.common' ;
1211import { HTTP_METHODS , HTTP_STATUS } from './http.constants' ;
1312import { TalendHttpError } from './http.types' ;
1413
@@ -18,6 +17,10 @@ const defaultPayload = {
1817 bar : 42 ,
1918} ;
2019
20+ interface FetchMock extends jest . Mock {
21+ mockResponse ?: Response ;
22+ }
23+
2124beforeEach ( ( ) => {
2225 jest . clearAllMocks ( ) ;
2326} ) ;
@@ -199,19 +202,24 @@ describe('#httpFetch with `CSRF` token', () => {
199202 } ) ;
200203
201204 afterAll ( ( ) => {
202- fetchMock . restore ( ) ;
203205 document . cookie = `csrfToken=${ CSRFToken } ; dwf_section_edit=True; Max-Age=0` ;
204206 } ) ;
205207 it ( 'should get the CRFS token' , async ( ) => {
206208 const url = '/foo' ;
207209 const headers = new Headers ( ) ;
208210 headers . append ( 'Content-Type' , 'application/json' ) ;
209211
210- fetchMock . mock ( url , { body : defaultBody , status : 200 } ) ;
212+ ( global . self . fetch as FetchMock ) . mockResponse = new Response ( JSON . stringify ( defaultBody ) , {
213+ status : 200 ,
214+ headers : {
215+ 'Content-Type' : 'application/json' ,
216+ } ,
217+ } ) ;
218+
211219 const result = await httpFetch ( url , { } , HTTP_METHODS . GET , defaultPayload ) ;
212220
213221 expect ( result . data ) . toEqual ( defaultBody ) ;
214- expect ( fetchMock . calls ( ) [ 0 ] [ 1 ] ) . toEqual ( {
222+ expect ( global . self . fetch ) . toHaveBeenCalledWith ( url , {
215223 body : JSON . stringify ( defaultPayload ) ,
216224 credentials : 'same-origin' ,
217225 headers : {
@@ -239,7 +247,6 @@ describe('#httpFetch with CSRF handling configuration', () => {
239247
240248 afterAll ( ( ) => {
241249 HTTP . defaultConfig = null ;
242- fetchMock . restore ( ) ;
243250 document . cookie = `${ defaultHttpConfiguration . security . CSRFTokenCookieKey } =${ CSRFToken } ; dwf_section_edit=True; Max-Age=0` ;
244251 } ) ;
245252
@@ -251,12 +258,17 @@ describe('#httpFetch with CSRF handling configuration', () => {
251258 const headers = new Headers ( ) ;
252259 headers . append ( 'Content-Type' , 'application/json' ) ;
253260
254- fetchMock . mock ( url , { body : defaultBody , status : 200 } ) ;
261+ ( global . self . fetch as FetchMock ) . mockResponse = new Response ( JSON . stringify ( defaultBody ) , {
262+ status : 200 ,
263+ headers : {
264+ 'Content-Type' : 'application/json' ,
265+ } ,
266+ } ) ;
255267
256268 const result = await httpFetch ( url , { } , HTTP_METHODS . GET , defaultPayload ) ;
257269
258270 expect ( result . data ) . toEqual ( defaultBody ) ;
259- expect ( fetchMock . calls ( ) [ 0 ] [ 1 ] ) . toEqual ( {
271+ expect ( global . self . fetch ) . toHaveBeenCalledWith ( url , {
260272 body : JSON . stringify ( defaultPayload ) ,
261273 credentials : 'same-origin' ,
262274 headers : {
@@ -276,19 +288,23 @@ describe('#httpFetch with CSRF handling configuration', () => {
276288describe ( '#httpFetch' , ( ) => {
277289 afterEach ( ( ) => {
278290 HTTP . defaultConfig = null ;
279- fetchMock . restore ( ) ;
280291 } ) ;
281292
282293 it ( 'should fetch the request' , async ( ) => {
283294 const url = '/foo' ;
284295 const headers = new Headers ( ) ;
285296 headers . append ( 'Content-Type' , 'application/json' ) ;
286- fetchMock . mock ( url , { body : defaultBody , status : 200 } ) ;
297+ ( global . self . fetch as FetchMock ) . mockResponse = new Response ( JSON . stringify ( defaultBody ) , {
298+ status : 200 ,
299+ headers : {
300+ 'Content-Type' : 'application/json' ,
301+ } ,
302+ } ) ;
287303
288304 const result = await httpFetch ( url , { } , HTTP_METHODS . GET , defaultPayload ) ;
289305
290306 expect ( result . data ) . toEqual ( defaultBody ) ;
291- expect ( fetchMock . calls ( ) [ 0 ] [ 1 ] ) . toEqual ( {
307+ expect ( global . self . fetch ) . toHaveBeenCalledWith ( url , {
292308 body : JSON . stringify ( defaultPayload ) ,
293309 credentials : 'same-origin' ,
294310 headers : {
@@ -310,12 +326,17 @@ describe('#httpFetch', () => {
310326 } ,
311327 } ) ;
312328
313- fetchMock . mock ( url , { body : defaultBody , status : 200 } ) ;
329+ ( global . self . fetch as FetchMock ) . mockResponse = new Response ( JSON . stringify ( defaultBody ) , {
330+ status : 200 ,
331+ headers : {
332+ 'Content-Type' : 'application/json' ,
333+ } ,
334+ } ) ;
314335
315336 const result = await httpFetch ( url , { } , HTTP_METHODS . GET , defaultPayload ) ;
316337
317338 expect ( result . data ) . toEqual ( defaultBody ) ;
318- expect ( fetchMock . calls ( ) [ 0 ] [ 1 ] ) . toEqual ( {
339+ expect ( global . self . fetch ) . toHaveBeenCalledWith ( url , {
319340 body : JSON . stringify ( defaultPayload ) ,
320341 credentials : 'same-origin' ,
321342 headers : {
@@ -333,14 +354,22 @@ describe('#httpFetch', () => {
333354 headers . append ( 'Content-Type' , 'application/json' ) ;
334355 const payload = new FormData ( ) ;
335356
336- fetchMock . mock ( url , { body : '{"foo": 42}' , status : 200 } ) ;
357+ ( global . self . fetch as FetchMock ) . mockResponse = new Response ( JSON . stringify ( { foo : 42 } ) , {
358+ status : 200 ,
359+ headers : {
360+ 'Content-Type' : 'application/json' ,
361+ } ,
362+ } ) ;
337363
338364 const result = await httpFetch ( url , { } , HTTP_METHODS . GET , payload ) ;
339- expect ( result . data ) . toEqual ( '{" foo" : 42}' ) ;
365+ expect ( result . data ) . toEqual ( { foo : 42 } ) ;
340366
341- const mockCalls = fetchMock . calls ( ) ;
342- expect ( mockCalls [ 0 ] [ 1 ] ?. credentials ) . toEqual ( 'same-origin' ) ;
343- expect ( mockCalls [ 0 ] [ 1 ] ?. headers ) . toEqual ( { Accept : 'application/json' } ) ;
367+ expect ( global . self . fetch ) . toHaveBeenCalledWith ( url , {
368+ body : payload ,
369+ credentials : 'same-origin' ,
370+ headers : { Accept : 'application/json' } ,
371+ method : 'GET' ,
372+ } ) ;
344373 } ) ;
345374} ) ;
346375
@@ -353,16 +382,17 @@ describe('#httpFetch with interceptors', () => {
353382 }
354383 } ) ;
355384
356- afterEach ( ( ) => {
357- fetchMock . restore ( ) ;
358- } ) ;
359-
360385 it ( 'should call interceptor' , async ( ) => {
361386 const interceptor = jest . fn ( ) . mockImplementation ( ( res , _ ) => res ) ;
362387 addHttpResponseInterceptor ( 'interceptor' , interceptor ) ;
363388
364389 const url = '/foo' ;
365- fetchMock . mock ( url , { body : defaultBody , status : 200 } ) ;
390+ ( global . self . fetch as FetchMock ) . mockResponse = new Response ( JSON . stringify ( defaultBody ) , {
391+ status : 200 ,
392+ headers : {
393+ 'Content-Type' : 'application/json' ,
394+ } ,
395+ } ) ;
366396
367397 await httpFetch ( url , { } , HTTP_METHODS . GET , { } ) ;
368398 expect ( interceptor ) . toHaveBeenCalled ( ) ;
@@ -374,8 +404,12 @@ describe('#httpFetch with interceptors', () => {
374404
375405 const url = '/foo' ;
376406 const context = { async : true } ;
377- const response = { body : defaultBody , status : 200 } ;
378- fetchMock . mock ( url , response ) ;
407+ ( global . self . fetch as FetchMock ) . mockResponse = new Response ( JSON . stringify ( defaultBody ) , {
408+ status : 200 ,
409+ headers : {
410+ 'Content-Type' : 'application/json' ,
411+ } ,
412+ } ) ;
379413
380414 await httpFetch ( url , { context } , HTTP_METHODS . GET , { } ) ;
381415 expect ( interceptor ) . toHaveBeenCalledWith (
0 commit comments