Skip to content

Commit c958a54

Browse files
committed
Migrate to class
1 parent cf3d881 commit c958a54

File tree

1 file changed

+57
-65
lines changed

1 file changed

+57
-65
lines changed

src/index.js

Lines changed: 57 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,11 @@ const VERBS = [
1515
"unlink",
1616
];
1717

18-
function adapter() {
19-
return (config) => {
20-
return handleRequest(this, config);
21-
};
22-
}
23-
2418
function getVerbArray() {
2519
const arr = [];
2620
VERBS.forEach(function (verb) {
2721
Object.defineProperty(arr, verb, {
28-
get: function () {
22+
get () {
2923
return arr.filter(function (h) {
3024
return !h.method || h.method === verb;
3125
});
@@ -35,49 +29,52 @@ function getVerbArray() {
3529
return arr;
3630
}
3731

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+
}
5551
}
56-
}
5752

58-
MockAdapter.prototype.adapter = adapter;
53+
adapter () {
54+
return (config) => handleRequest(this, config);
55+
}
5956

60-
MockAdapter.prototype.restore = function restore() {
61-
if (this.axiosInstance) {
57+
restore () {
58+
if (!this.axiosInstance) return;
6259
this.axiosInstance.defaults.adapter = this.originalAdapter;
6360
this.axiosInstance = undefined;
6461
}
65-
};
6662

67-
MockAdapter.prototype.reset = function reset() {
68-
this.resetHandlers();
69-
this.resetHistory();
70-
};
63+
reset () {
64+
this.resetHandlers();
65+
this.resetHistory();
66+
}
7167

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+
}
7672

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+
}
8178

8279
const methodsWithConfigsAsSecondArg = ["any", "get", "delete", "head", "options"];
8380
function convertDataAndConfigToConfig (method, data, config) {
@@ -111,14 +108,14 @@ function toMethodName (method) {
111108
}
112109

113110
VERBS.concat("any").forEach(function (method) {
114-
MockAdapter.prototype[toMethodName(method)] = function (matcher, data, config) {
111+
AxiosMockAdapter.prototype[toMethodName(method)] = function (matcher, data, config) {
115112
const self = this;
116113
let delay;
117114
matcher = matcher === undefined ? /.*/ : matcher;
118115

119116
const paramsAndBody = convertDataAndConfigToConfig(method, data, config);
120117

121-
function reply(code, response, headers) {
118+
function reply (code, response, headers) {
122119
const handler = {
123120
url: matcher,
124121
method: method === "any" ? undefined : method,
@@ -137,14 +134,14 @@ VERBS.concat("any").forEach(function (method) {
137134
return self;
138135
}
139136

140-
function withDelayInMs(_delay) {
137+
function withDelayInMs (_delay) {
141138
delay = _delay;
142139
const respond = requestApi.reply.bind(requestApi);
143140
Object.assign(respond, requestApi);
144141
return respond;
145142
}
146143

147-
function replyOnce(code, response, headers) {
144+
function replyOnce (code, response, headers) {
148145
const handler = {
149146
url: matcher,
150147
method: method === "any" ? undefined : method,
@@ -164,13 +161,10 @@ VERBS.concat("any").forEach(function (method) {
164161
}
165162

166163
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 () {
174168
const handler = {
175169
passThrough: true,
176170
method: method === "any" ? undefined : method,
@@ -182,8 +176,7 @@ VERBS.concat("any").forEach(function (method) {
182176
addHandler(method, self.handlers, handler);
183177
return self;
184178
},
185-
186-
abortRequest: function () {
179+
abortRequest () {
187180
return reply(async function (config) {
188181
throw utils.createAxiosError(
189182
"Request aborted",
@@ -193,8 +186,7 @@ VERBS.concat("any").forEach(function (method) {
193186
);
194187
});
195188
},
196-
197-
abortRequestOnce: function () {
189+
abortRequestOnce () {
198190
return replyOnce(async function (config) {
199191
throw utils.createAxiosError(
200192
"Request aborted",
@@ -205,19 +197,19 @@ VERBS.concat("any").forEach(function (method) {
205197
});
206198
},
207199

208-
networkError: function () {
200+
networkError () {
209201
return reply(async function (config) {
210202
throw utils.createAxiosError("Network Error", config);
211203
});
212204
},
213205

214-
networkErrorOnce: function () {
206+
networkErrorOnce () {
215207
return replyOnce(async function (config) {
216208
throw utils.createAxiosError("Network Error", config);
217209
});
218210
},
219211

220-
timeout: function () {
212+
timeout () {
221213
return reply(async function (config) {
222214
throw utils.createAxiosError(
223215
config.timeoutErrorMessage ||
@@ -231,7 +223,7 @@ VERBS.concat("any").forEach(function (method) {
231223
});
232224
},
233225

234-
timeoutOnce: function () {
226+
timeoutOnce () {
235227
return replyOnce(async function (config) {
236228
throw utils.createAxiosError(
237229
config.timeoutErrorMessage ||
@@ -250,7 +242,7 @@ VERBS.concat("any").forEach(function (method) {
250242
};
251243
});
252244

253-
function findInHandlers(method, handlers, handler) {
245+
function findInHandlers (handlers, handler) {
254246
let index = -1;
255247
for (let i = 0; i < handlers.length; i += 1) {
256248
const item = handlers[i];
@@ -273,11 +265,11 @@ function findInHandlers(method, handlers, handler) {
273265
return index;
274266
}
275267

276-
function addHandler(method, handlers, handler) {
268+
function addHandler (method, handlers, handler) {
277269
if (method === "any") {
278270
handlers.push(handler);
279271
} else {
280-
const indexOfExistingHandler = findInHandlers(method, handlers, handler);
272+
const indexOfExistingHandler = findInHandlers(handlers, handler);
281273
// handler.replyOnce indicates that a handler only runs once.
282274
// It's supported to register muliple ones like that without
283275
// overwriting the previous one.
@@ -289,5 +281,5 @@ function addHandler(method, handlers, handler) {
289281
}
290282
}
291283

292-
module.exports = MockAdapter;
293-
module.exports.default = MockAdapter;
284+
module.exports = AxiosMockAdapter;
285+
module.exports.default = AxiosMockAdapter;

0 commit comments

Comments
 (0)