1- import {
2- createJSONResponsePromise ,
3- createResponsePromise ,
4- del ,
5- get ,
6- HttpStatus ,
7- post ,
8- postJSON ,
9- ResponsePromiseWithBodyMethods
10- } from '@tkrotoff/fetch' ;
1+ import * as Http from '@tkrotoff/fetch' ;
112
123import {
134 abortRequestExample ,
@@ -21,23 +12,9 @@ import {
2112 uploadFilesExample
2213} from './requests' ;
2314
24- beforeEach ( ( ) => jest . resetAllMocks ( ) ) ;
25-
26- jest . mock ( '@tkrotoff/fetch' , ( ) => ( {
27- ...jest . requireActual ( '@tkrotoff/fetch' ) ,
28- get : jest . fn ( ) ,
29- post : jest . fn ( ) ,
30- postJSON : jest . fn ( ) ,
31- //put: jest.fn(),
32- //putJSON: jest.fn(),
33- //patch: jest.fn(),
34- //patchJSON: jest.fn(),
35- del : jest . fn ( )
36- } ) ) ;
37-
3815test ( 'get200OKExample()' , async ( ) => {
39- jest . mocked ( get ) . mockImplementation ( ( ) =>
40- createJSONResponsePromise ( {
16+ const mock = jest . spyOn ( Http , ' get' ) . mockImplementation ( ( ) =>
17+ Http . createJSONResponsePromise ( {
4118 userId : 1 ,
4219 id : 1 ,
4320 title : 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit' ,
@@ -46,13 +23,15 @@ test('get200OKExample()', async () => {
4623 ) ;
4724
4825 await get200OKExample ( ) ;
49- expect ( get ) . toHaveBeenCalledTimes ( 1 ) ;
50- expect ( get ) . toHaveBeenCalledWith ( 'https://jsonplaceholder.typicode.com/posts/1' ) ;
26+ expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
27+ expect ( mock ) . toHaveBeenCalledWith ( 'https://jsonplaceholder.typicode.com/posts/1' ) ;
28+
29+ mock . mockRestore ( ) ;
5130} ) ;
5231
5332test ( 'postJSON201CreatedExample()' , async ( ) => {
54- jest . mocked ( postJSON ) . mockImplementation ( ( ) =>
55- createJSONResponsePromise ( {
33+ const mock = jest . spyOn ( Http , ' postJSON' ) . mockImplementation ( ( ) =>
34+ Http . createJSONResponsePromise ( {
5635 id : 101 ,
5736 title : 'foo' ,
5837 body : 'bar' ,
@@ -61,59 +40,69 @@ test('postJSON201CreatedExample()', async () => {
6140 ) ;
6241
6342 await postJSON201CreatedExample ( ) ;
64- expect ( postJSON ) . toHaveBeenCalledTimes ( 1 ) ;
65- expect ( postJSON ) . toHaveBeenCalledWith ( 'https://jsonplaceholder.typicode.com/posts' , {
43+ expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
44+ expect ( mock ) . toHaveBeenCalledWith ( 'https://jsonplaceholder.typicode.com/posts' , {
6645 body : 'bar' ,
6746 title : 'foo' ,
6847 userId : 1
6948 } ) ;
49+
50+ mock . mockRestore ( ) ;
7051} ) ;
7152
7253test ( 'del200OKExample()' , async ( ) => {
73- jest . mocked ( del ) . mockImplementation ( ( ) => createJSONResponsePromise ( { } ) ) ;
54+ const mock = jest . spyOn ( Http , ' del' ) . mockImplementation ( ( ) => Http . createJSONResponsePromise ( { } ) ) ;
7455
7556 await del200OKExample ( ) ;
76- expect ( del ) . toHaveBeenCalledTimes ( 1 ) ;
77- expect ( del ) . toHaveBeenCalledWith ( 'https://jsonplaceholder.typicode.com/posts/1' ) ;
57+ expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
58+ expect ( mock ) . toHaveBeenCalledWith ( 'https://jsonplaceholder.typicode.com/posts/1' ) ;
59+
60+ mock . mockRestore ( ) ;
7861} ) ;
7962
8063test ( 'get404NotFoundExample()' , async ( ) => {
81- jest . mocked ( get ) . mockImplementation ( ( ) =>
82- createResponsePromise ( '404 Not Found' , {
83- status : HttpStatus . _404_NotFound ,
64+ const mock = jest . spyOn ( Http , ' get' ) . mockImplementation ( ( ) =>
65+ Http . createResponsePromise ( '404 Not Found' , {
66+ status : Http . HttpStatus . _404_NotFound ,
8467 statusText : 'Not Found'
8568 } )
8669 ) ;
8770
8871 await get404NotFoundExample ( ) ;
89- expect ( get ) . toHaveBeenCalledTimes ( 1 ) ;
90- expect ( get ) . toHaveBeenCalledWith ( 'https://httpstat.us/404/cors' ) ;
72+ expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
73+ expect ( mock ) . toHaveBeenCalledWith ( 'https://httpstat.us/404/cors' ) ;
74+
75+ mock . mockRestore ( ) ;
9176} ) ;
9277
9378test ( 'get500InternalServerErrorExample()' , async ( ) => {
94- jest . mocked ( get ) . mockImplementation ( ( ) =>
95- createResponsePromise ( '500 Internal Server Error' , {
96- status : HttpStatus . _500_InternalServerError ,
79+ const mock = jest . spyOn ( Http , ' get' ) . mockImplementation ( ( ) =>
80+ Http . createResponsePromise ( '500 Internal Server Error' , {
81+ status : Http . HttpStatus . _500_InternalServerError ,
9782 statusText : 'Internal Server Error'
9883 } )
9984 ) ;
10085
10186 await get500InternalServerErrorExample ( ) ;
102- expect ( get ) . toHaveBeenCalledTimes ( 1 ) ;
103- expect ( get ) . toHaveBeenCalledWith ( 'https://httpstat.us/500/cors' ) ;
87+ expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
88+ expect ( mock ) . toHaveBeenCalledWith ( 'https://httpstat.us/500/cors' ) ;
89+
90+ mock . mockRestore ( ) ;
10491} ) ;
10592
10693test ( 'getCorsBlockedExample()' , async ( ) => {
107- jest . mocked ( get ) . mockRejectedValue ( new TypeError ( 'Failed to fetch' ) ) ;
94+ const mock = jest . spyOn ( Http , ' get' ) . mockRejectedValue ( new TypeError ( 'Failed to fetch' ) ) ;
10895
10996 await getCorsBlockedExample ( ) ;
110- expect ( get ) . toHaveBeenCalledTimes ( 1 ) ;
111- expect ( get ) . toHaveBeenCalledWith ( 'https://postman-echo.com/get?foo1=bar1&foo2=bar2' ) ;
97+ expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
98+ expect ( mock ) . toHaveBeenCalledWith ( 'https://postman-echo.com/get?foo1=bar1&foo2=bar2' ) ;
99+
100+ mock . mockRestore ( ) ;
112101} ) ;
113102
114103test ( 'uploadFilesExample()' , async ( ) => {
115- jest . mocked ( post ) . mockImplementation ( ( ) =>
116- createJSONResponsePromise ( {
104+ const mock = jest . spyOn ( Http , ' post' ) . mockImplementation ( ( ) =>
105+ Http . createJSONResponsePromise ( {
117106 files : { file0 : 'file0Content' , file1 : 'file1Content' }
118107 } )
119108 ) ;
@@ -122,14 +111,16 @@ test('uploadFilesExample()', async () => {
122111 const file1 = new File ( [ 'file1Content' ] , 'file1' , { type : 'text/plain' } ) ;
123112
124113 await uploadFilesExample ( [ file0 , file1 ] as unknown as FileList ) ;
125- expect ( post ) . toHaveBeenCalledTimes ( 1 ) ;
126- expect ( post ) . toHaveBeenCalledWith ( 'https://httpbin.org/anything' , expect . any ( FormData ) ) ;
114+ expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
115+ expect ( mock ) . toHaveBeenCalledWith ( 'https://httpbin.org/anything' , expect . any ( FormData ) ) ;
116+
117+ mock . mockRestore ( ) ;
127118} ) ;
128119
129120test ( 'abortRequestExample()' , async ( ) => {
130121 const abortError = new DOMException ( 'The user aborted a request.' , 'AbortError' ) ;
131122
132- jest . mocked ( get ) . mockImplementation ( ( _input , init ) => {
123+ const mock = jest . spyOn ( Http , ' get' ) . mockImplementation ( ( _input , init ) => {
133124 // Mock aborted request
134125 // https://github.com/github/fetch/blob/v3.4.1/fetch.js#L497
135126 const response = new Promise ( ( resolve , reject ) => {
@@ -141,17 +132,19 @@ test('abortRequestExample()', async () => {
141132 } , 600 ) ;
142133 } ) ;
143134
144- return response as ResponsePromiseWithBodyMethods ;
135+ return response as Http . ResponsePromiseWithBodyMethods ;
145136 } ) ;
146137
147138 await abortRequestExample ( ) ;
148- expect ( get ) . toHaveBeenCalledTimes ( 1 ) ;
149- expect ( get ) . toHaveBeenCalledWith (
139+ expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
140+ expect ( mock ) . toHaveBeenCalledWith (
150141 'https://httpbin.org/drip?duration=2&numbytes=10&code=200&delay=2' ,
151142 {
152143 signal : expect . any ( AbortSignal )
153144 }
154145 ) ;
146+
147+ mock . mockRestore ( ) ;
155148} ) ;
156149
157150// FIXME jsdom does not support Blob.stream https://github.com/jsdom/jsdom/issues/2555
@@ -169,17 +162,19 @@ test.skip('downloadProgressExample()', async () => {
169162 const blob = new Blob ( [ content . buffer ] ) ;
170163 const stream = blob . stream ( ) ;
171164
172- jest . mocked ( get ) . mockImplementation ( ( ) =>
173- createResponsePromise ( stream , {
165+ const mock = jest . spyOn ( Http , ' get' ) . mockImplementation ( ( ) =>
166+ Http . createResponsePromise ( stream , {
174167 headers : {
175168 'content-length' : blob . size . toString ( )
176169 }
177170 } )
178171 ) ;
179172
180173 await downloadProgressExample ( ) ;
181- expect ( get ) . toHaveBeenCalledTimes ( 1 ) ;
182- expect ( get ) . toHaveBeenCalledWith (
174+ expect ( mock ) . toHaveBeenCalledTimes ( 1 ) ;
175+ expect ( mock ) . toHaveBeenCalledWith (
183176 'https://fetch-progress.anthum.com/30kbps/images/sunrise-baseline.jpg'
184177 ) ;
178+
179+ mock . mockRestore ( ) ;
185180} ) ;
0 commit comments