旧版报表、仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

142 lines
3.9 KiB

'use strict';
var _assign = require('babel-runtime/core-js/object/assign');
var _assign2 = _interopRequireDefault(_assign);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var compileRoute = require('./compile-route');
var FetchMock = {};
FetchMock.mock = function (matcher, response) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var route = void 0;
// Handle the variety of parameters accepted by mock (see README)
if (matcher && response) {
route = (0, _assign2.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 _this = this;
var route = this.compileRoute(uncompiledRoute);
var clashes = this.routes.filter(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) {
clashes.forEach(function (clash) {
var index = _this.routes.indexOf(clash);
_this._uncompiledRoutes.splice(index, 1, uncompiledRoute);
_this.routes.splice(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, _assign2.default)({}, options, { repeat: 1 }));
};
['get', 'post', 'put', 'delete', 'head', 'patch'].forEach(function (method) {
var extendOptions = function extendOptions(options) {
return (0, _assign2.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[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 () {
this._calls = [];
this._holdingPromises = [];
this.routes.forEach(function (route) {
return route.reset && route.reset();
});
return this;
};
FetchMock.restore = FetchMock.reset = function () {
this.resetBehavior();
this.resetHistory();
return this;
};
module.exports = FetchMock;