@@ -15,17 +15,11 @@ const VERBS = [
15
15
"unlink" ,
16
16
] ;
17
17
18
- function adapter ( ) {
19
- return ( config ) => {
20
- return handleRequest ( this , config ) ;
21
- } ;
22
- }
23
-
24
18
function getVerbArray ( ) {
25
19
const arr = [ ] ;
26
20
VERBS . forEach ( function ( verb ) {
27
21
Object . defineProperty ( arr , verb , {
28
- get : function ( ) {
22
+ get ( ) {
29
23
return arr . filter ( function ( h ) {
30
24
return ! h . method || h . method === verb ;
31
25
} ) ;
@@ -35,49 +29,52 @@ function getVerbArray() {
35
29
return arr ;
36
30
}
37
31
38
- function MockAdapter ( axiosInstance , options = { } ) {
39
- this . reset ( ) ;
40
-
41
- if ( axiosInstance ) {
42
- this . axiosInstance = axiosInstance ;
43
- // Clone the axios instance to remove interceptors
44
- // this is used for the passThrough mode with axios > 1.2
45
- this . axiosInstanceWithoutInterceptors = axiosInstance . create
46
- ? axiosInstance . create ( )
47
- : undefined ;
48
-
49
- this . originalAdapter = axiosInstance . defaults . adapter ;
50
- this . delayResponse = options . delayResponse > 0 ? options . delayResponse : null ;
51
- this . onNoMatch = options . onNoMatch || null ;
52
- axiosInstance . defaults . adapter = this . adapter . call ( this ) ;
53
- } else {
54
- throw new Error ( "Please provide an instance of axios to mock" ) ;
32
+ class AxiosMockAdapter {
33
+ constructor ( axiosInstance , options = { } ) {
34
+ this . reset ( ) ;
35
+
36
+ if ( axiosInstance ) {
37
+ this . axiosInstance = axiosInstance ;
38
+ // Clone the axios instance to remove interceptors
39
+ // this is used for the passThrough mode with axios > 1.2
40
+ this . axiosInstanceWithoutInterceptors = axiosInstance . create
41
+ ? axiosInstance . create ( )
42
+ : undefined ;
43
+
44
+ this . originalAdapter = axiosInstance . defaults . adapter ;
45
+ this . delayResponse = options . delayResponse > 0 ? options . delayResponse : null ;
46
+ this . onNoMatch = options . onNoMatch || null ;
47
+ axiosInstance . defaults . adapter = this . adapter ( ) ;
48
+ } else {
49
+ throw new Error ( "Please provide an instance of axios to mock" ) ;
50
+ }
55
51
}
56
- }
57
52
58
- MockAdapter . prototype . adapter = adapter ;
53
+ adapter ( ) {
54
+ return ( config ) => handleRequest ( this , config ) ;
55
+ }
59
56
60
- MockAdapter . prototype . restore = function restore ( ) {
61
- if ( this . axiosInstance ) {
57
+ restore ( ) {
58
+ if ( ! this . axiosInstance ) return ;
62
59
this . axiosInstance . defaults . adapter = this . originalAdapter ;
63
60
this . axiosInstance = undefined ;
64
61
}
65
- } ;
66
62
67
- MockAdapter . prototype . reset = function reset ( ) {
68
- this . resetHandlers ( ) ;
69
- this . resetHistory ( ) ;
70
- } ;
63
+ reset ( ) {
64
+ this . resetHandlers ( ) ;
65
+ this . resetHistory ( ) ;
66
+ }
71
67
72
- MockAdapter . prototype . resetHandlers = function resetHandlers ( ) {
73
- if ( this . handlers ) this . handlers . length = 0 ;
74
- else this . handlers = getVerbArray ( ) ;
75
- } ;
68
+ resetHandlers ( ) {
69
+ if ( this . handlers ) this . handlers . length = 0 ;
70
+ else this . handlers = getVerbArray ( ) ;
71
+ }
76
72
77
- MockAdapter . prototype . resetHistory = function resetHistory ( ) {
78
- if ( this . history ) this . history . length = 0 ;
79
- else this . history = getVerbArray ( ) ;
80
- } ;
73
+ resetHistory ( ) {
74
+ if ( this . history ) this . history . length = 0 ;
75
+ else this . history = getVerbArray ( ) ;
76
+ }
77
+ }
81
78
82
79
const methodsWithConfigsAsSecondArg = [ "any" , "get" , "delete" , "head" , "options" ] ;
83
80
function convertDataAndConfigToConfig ( method , data , config ) {
@@ -111,14 +108,14 @@ function toMethodName (method) {
111
108
}
112
109
113
110
VERBS . concat ( "any" ) . forEach ( function ( method ) {
114
- MockAdapter . prototype [ toMethodName ( method ) ] = function ( matcher , data , config ) {
111
+ AxiosMockAdapter . prototype [ toMethodName ( method ) ] = function ( matcher , data , config ) {
115
112
const self = this ;
116
113
let delay ;
117
114
matcher = matcher === undefined ? / .* / : matcher ;
118
115
119
116
const paramsAndBody = convertDataAndConfigToConfig ( method , data , config ) ;
120
117
121
- function reply ( code , response , headers ) {
118
+ function reply ( code , response , headers ) {
122
119
const handler = {
123
120
url : matcher ,
124
121
method : method === "any" ? undefined : method ,
@@ -137,14 +134,14 @@ VERBS.concat("any").forEach(function (method) {
137
134
return self ;
138
135
}
139
136
140
- function withDelayInMs ( _delay ) {
137
+ function withDelayInMs ( _delay ) {
141
138
delay = _delay ;
142
139
const respond = requestApi . reply . bind ( requestApi ) ;
143
140
Object . assign ( respond , requestApi ) ;
144
141
return respond ;
145
142
}
146
143
147
- function replyOnce ( code , response , headers ) {
144
+ function replyOnce ( code , response , headers ) {
148
145
const handler = {
149
146
url : matcher ,
150
147
method : method === "any" ? undefined : method ,
@@ -164,13 +161,10 @@ VERBS.concat("any").forEach(function (method) {
164
161
}
165
162
166
163
const requestApi = {
167
- reply : reply ,
168
-
169
- replyOnce : replyOnce ,
170
-
171
- withDelayInMs : withDelayInMs ,
172
-
173
- passThrough : function passThrough ( ) {
164
+ reply,
165
+ replyOnce,
166
+ withDelayInMs,
167
+ passThrough ( ) {
174
168
const handler = {
175
169
passThrough : true ,
176
170
method : method === "any" ? undefined : method ,
@@ -182,8 +176,7 @@ VERBS.concat("any").forEach(function (method) {
182
176
addHandler ( method , self . handlers , handler ) ;
183
177
return self ;
184
178
} ,
185
-
186
- abortRequest : function ( ) {
179
+ abortRequest ( ) {
187
180
return reply ( async function ( config ) {
188
181
throw utils . createAxiosError (
189
182
"Request aborted" ,
@@ -193,8 +186,7 @@ VERBS.concat("any").forEach(function (method) {
193
186
) ;
194
187
} ) ;
195
188
} ,
196
-
197
- abortRequestOnce : function ( ) {
189
+ abortRequestOnce ( ) {
198
190
return replyOnce ( async function ( config ) {
199
191
throw utils . createAxiosError (
200
192
"Request aborted" ,
@@ -205,19 +197,19 @@ VERBS.concat("any").forEach(function (method) {
205
197
} ) ;
206
198
} ,
207
199
208
- networkError : function ( ) {
200
+ networkError ( ) {
209
201
return reply ( async function ( config ) {
210
202
throw utils . createAxiosError ( "Network Error" , config ) ;
211
203
} ) ;
212
204
} ,
213
205
214
- networkErrorOnce : function ( ) {
206
+ networkErrorOnce ( ) {
215
207
return replyOnce ( async function ( config ) {
216
208
throw utils . createAxiosError ( "Network Error" , config ) ;
217
209
} ) ;
218
210
} ,
219
211
220
- timeout : function ( ) {
212
+ timeout ( ) {
221
213
return reply ( async function ( config ) {
222
214
throw utils . createAxiosError (
223
215
config . timeoutErrorMessage ||
@@ -231,7 +223,7 @@ VERBS.concat("any").forEach(function (method) {
231
223
} ) ;
232
224
} ,
233
225
234
- timeoutOnce : function ( ) {
226
+ timeoutOnce ( ) {
235
227
return replyOnce ( async function ( config ) {
236
228
throw utils . createAxiosError (
237
229
config . timeoutErrorMessage ||
@@ -250,7 +242,7 @@ VERBS.concat("any").forEach(function (method) {
250
242
} ;
251
243
} ) ;
252
244
253
- function findInHandlers ( method , handlers , handler ) {
245
+ function findInHandlers ( handlers , handler ) {
254
246
let index = - 1 ;
255
247
for ( let i = 0 ; i < handlers . length ; i += 1 ) {
256
248
const item = handlers [ i ] ;
@@ -273,11 +265,11 @@ function findInHandlers(method, handlers, handler) {
273
265
return index ;
274
266
}
275
267
276
- function addHandler ( method , handlers , handler ) {
268
+ function addHandler ( method , handlers , handler ) {
277
269
if ( method === "any" ) {
278
270
handlers . push ( handler ) ;
279
271
} else {
280
- const indexOfExistingHandler = findInHandlers ( method , handlers , handler ) ;
272
+ const indexOfExistingHandler = findInHandlers ( handlers , handler ) ;
281
273
// handler.replyOnce indicates that a handler only runs once.
282
274
// It's supported to register muliple ones like that without
283
275
// overwriting the previous one.
@@ -289,5 +281,5 @@ function addHandler(method, handlers, handler) {
289
281
}
290
282
}
291
283
292
- module . exports = MockAdapter ;
293
- module . exports . default = MockAdapter ;
284
+ module . exports = AxiosMockAdapter ;
285
+ module . exports . default = AxiosMockAdapter ;
0 commit comments