Skip to content

Commit

Permalink
Migrate handlers and history to a flat array
Browse files Browse the repository at this point in the history
  • Loading branch information
marcbachmann committed Oct 9, 2024
1 parent 1291950 commit 6acbf99
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/handle_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function handleRequest(mockAdapter, resolve, reject, config) {
}

delete config.adapter;
mockAdapter.history[config.method].push(config);
mockAdapter.history.push(config);

var handler = utils.findHandler(
mockAdapter.handlers,
Expand Down
70 changes: 37 additions & 33 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use strict";

var handleRequest = require("./handle_request");
var utils = require("./utils");

Expand All @@ -25,28 +24,22 @@ function adapter() {
}.bind(this);
}

function getVerbObject() {
return VERBS.reduce(function (accumulator, verb) {
accumulator[verb] = [];
return accumulator;
}, {});
}

function reset() {
resetHandlers.call(this);
resetHistory.call(this);
}

function resetHandlers() {
this.handlers = getVerbObject();
}

function resetHistory() {
this.history = getVerbObject();
function getVerbArray() {
var arr = [];
VERBS.forEach(function (verb) {
Object.defineProperty(arr, verb, {
get: function () {
return arr.filter(function (h) {
return !h.method || h.method === verb;
});
},
});
});
return arr;
}

function MockAdapter(axiosInstance, options) {
reset.call(this);
this.reset();

if (axiosInstance) {
this.axiosInstance = axiosInstance;
Expand Down Expand Up @@ -75,9 +68,20 @@ MockAdapter.prototype.restore = function restore() {
}
};

MockAdapter.prototype.reset = reset;
MockAdapter.prototype.resetHandlers = resetHandlers;
MockAdapter.prototype.resetHistory = resetHistory;
MockAdapter.prototype.reset = function reset() {
this.resetHandlers();
this.resetHistory();
};

MockAdapter.prototype.resetHandlers = function resetHandlers() {
if (this.handlers) this.handlers.length = 0;
else this.handlers = getVerbArray();
};

MockAdapter.prototype.resetHistory = function resetHistory() {
if (this.history) this.history.length = 0;
else this.history = getVerbArray();
};

var methodsWithConfigsAsSecondArg = ["any", "get", "delete", "head", "options"];
function convertDataAndConfigToConfig (method, data, config) {
Expand Down Expand Up @@ -120,7 +124,7 @@ VERBS.concat("any").forEach(function (method) {
function reply(code, response, headers) {
var handler = {
url: matcher,
method: method,
method: method === 'any' ? undefined : method,
params: paramsAndBody.params,
data: paramsAndBody.data,
headers: paramsAndBody.headers,
Expand All @@ -146,7 +150,7 @@ VERBS.concat("any").forEach(function (method) {
function replyOnce(code, response, headers) {
var handler = {
url: matcher,
method: method,
method: method === 'any' ? undefined : method,
params: paramsAndBody.params,
data: paramsAndBody.data,
headers: paramsAndBody.headers,
Expand All @@ -172,7 +176,7 @@ VERBS.concat("any").forEach(function (method) {
passThrough: function passThrough() {
var handler = {
passThrough: true,
method: method,
method: method === 'any' ? undefined : method,
url: matcher,
params: paramsAndBody.params,
data: paramsAndBody.data,
Expand Down Expand Up @@ -257,13 +261,15 @@ VERBS.concat("any").forEach(function (method) {

function findInHandlers(method, handlers, handler) {
var index = -1;
for (var i = 0; i < handlers[method].length; i += 1) {
var item = handlers[method][i];
for (var i = 0; i < handlers.length; i += 1) {
var item = handlers[i];
var comparePaths =
item.url instanceof RegExp && handler.url instanceof RegExp
? String(item.url) === String(handler.url)
: item.url === handler.url;

var isSame =
(!item.method || item.method === handler.method) &&
comparePaths &&
utils.isEqual(item.params, handler.params) &&
utils.isEqual(item.data, handler.data) &&
Expand All @@ -277,18 +283,16 @@ function findInHandlers(method, handlers, handler) {

function addHandler(method, handlers, handler) {
if (method === "any") {
VERBS.forEach(function (verb) {
handlers[verb].push(handler);
});
handlers.push(handler);
} else {
var indexOfExistingHandler = findInHandlers(method, handlers, handler);
// handler.replyOnce indicates that a handler only runs once.
// It's supported to register muliple ones like that without
// overwriting the previous one.
if (indexOfExistingHandler > -1 && !handler.replyOnce) {
handlers[method].splice(indexOfExistingHandler, 1, handler);
handlers.splice(indexOfExistingHandler, 1, handler);
} else {
handlers[method].push(handler);
handlers.push(handler);
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,10 @@ function isBodyMatching(body, requiredBody) {
}

function purgeIfReplyOnce(mock, handler) {
Object.keys(mock.handlers).forEach(function (key) {
var index = mock.handlers[key].indexOf(handler);
if (index > -1) {
mock.handlers[key].splice(index, 1);
}
});
var index = mock.handlers.indexOf(handler);
if (index > -1) {
mock.handlers.splice(index, 1);
}
}

function settle(resolve, reject, response, delay) {
Expand Down
16 changes: 15 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ type NoBodyRequestMatcherFunc = (
config?: ConfigMatcher
) => MockAdapter.RequestHandler;

type verb =
| 'get'
| 'post'
| 'put'
| 'delete'
| 'patch'
| 'options'
| 'head'
| 'list'
| 'link'
| 'unlink';

type HistoryArray = AxiosRequestConfig[] & Record<verb, AxiosRequestConfig[]>

declare class MockAdapter {
static default: typeof MockAdapter;

Expand All @@ -75,7 +89,7 @@ declare class MockAdapter {
resetHistory(): void;
restore(): void;

history: { [method: string]: AxiosRequestConfig[] };
history: HistoryArray;

onAny: NoBodyRequestMatcherFunc;
onGet: NoBodyRequestMatcherFunc;
Expand Down
15 changes: 15 additions & 0 deletions types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ namespace SupportsResetHistory {
mock.resetHistory();
}

namespace SupportsHistoryArray {
mock.history.length;
mock.history[0].method;
mock.history[0].url;
mock.history[0].params;
mock.history[0].data;
mock.history[0].headers;

mock.history.get[0].method;
mock.history.get[0].url;
mock.history.get[0].params;
mock.history.get[0].data;
mock.history.get[0].headers;
}

namespace SupportsRestore {
mock.restore();
}
Expand Down

0 comments on commit 6acbf99

Please sign in to comment.