@4c/fetch-mock
Version:
Mock http requests made using fetch (or isomorphic-fetch)
159 lines (121 loc) • 4.97 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
var _splice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/splice"));
var _indexOf = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/index-of"));
var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/for-each"));
var _filter = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/filter"));
var _assign = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/assign"));
var _context5;
var compileRoute = require('./compile-route');
var FetchMock = {};
FetchMock.mock = function (matcher, response) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var route; // Handle the variety of parameters accepted by mock (see README)
if (matcher && response) {
route = (0, _assign["default"])({
matcher: matcher,
response: response
}, options);
} else if (matcher && matcher.matcher) {
route = matcher;
} else {
throw new Error('fetch-mock: Invalid parameters passed to fetch-mock');
}
this.addRoute(route);
return this._mock();
};
FetchMock.addRoute = function (uncompiledRoute) {
var _context,
_this = this;
var route = this.compileRoute(uncompiledRoute);
var clashes = (0, _filter["default"])(_context = this.routes).call(_context, function (_ref) {
var identifier = _ref.identifier,
method = _ref.method;
return identifier === route.identifier && (!method || !route.method || method === route.method);
});
var overwriteRoutes = 'overwriteRoutes' in route ? route.overwriteRoutes : this.config.overwriteRoutes;
if (overwriteRoutes === false || !clashes.length) {
this._uncompiledRoutes.push(uncompiledRoute);
return this.routes.push(route);
}
if (overwriteRoutes === true) {
(0, _forEach["default"])(clashes).call(clashes, function (clash) {
var _context2, _context3, _context4;
var index = (0, _indexOf["default"])(_context2 = _this.routes).call(_context2, clash);
(0, _splice["default"])(_context3 = _this._uncompiledRoutes).call(_context3, index, 1, uncompiledRoute);
(0, _splice["default"])(_context4 = _this.routes).call(_context4, index, 1, route);
});
return this.routes;
}
if (clashes.length) {
throw new Error('fetch-mock: Adding route with same name or matcher as existing route. See `overwriteRoutes` option.');
}
this._uncompiledRoutes.push(uncompiledRoute);
this.routes.push(route);
};
FetchMock._mock = function () {
if (!this.isSandbox) {
// Do this here rather than in the constructor to ensure it's scoped to the test
this.realFetch = this.realFetch || this.global.fetch;
this.global.fetch = this.fetchHandler;
}
return this;
};
FetchMock["catch"] = function (response) {
if (this.fallbackResponse) {
console.warn('calling fetchMock.catch() twice - are you sure you want to overwrite the previous fallback response'); // eslint-disable-line
}
this.fallbackResponse = response || 'ok';
return this._mock();
};
FetchMock.spy = function () {
this._mock();
return this["catch"](this.getNativeFetch());
};
FetchMock.compileRoute = compileRoute;
FetchMock.once = function (matcher, response) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
return this.mock(matcher, response, (0, _assign["default"])({}, options, {
repeat: 1
}));
};
(0, _forEach["default"])(_context5 = ['get', 'post', 'put', 'delete', 'head', 'patch']).call(_context5, function (method) {
var extendOptions = function extendOptions(options) {
return (0, _assign["default"])({}, options, {
method: method.toUpperCase()
});
};
FetchMock[method] = function (matcher, response) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
return this.mock(matcher, response, extendOptions(options));
};
FetchMock["".concat(method, "Once")] = function (matcher, response) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
return this.once(matcher, response, extendOptions(options));
};
});
FetchMock.resetBehavior = function () {
if (this.realFetch) {
this.global.fetch = this.realFetch;
this.realFetch = undefined;
}
this.fallbackResponse = undefined;
this.routes = [];
this._uncompiledRoutes = [];
return this;
};
FetchMock.resetHistory = function () {
var _context6;
this._calls = [];
this._holdingPromises = [];
(0, _forEach["default"])(_context6 = this.routes).call(_context6, function (route) {
return route.reset && route.reset();
});
return this;
};
FetchMock.restore = FetchMock.reset = function () {
this.resetBehavior();
this.resetHistory();
return this;
};
module.exports = FetchMock;