UNPKG

axios-hooks-mock

Version:

A library that simplifies mocking for axios-hooks, especially when multiple hooks are used together.

307 lines (253 loc) 8.23 kB
function createCommonjsModule(fn, basedir, module) { return module = { path: basedir, exports: {}, require: function (path, base) { return commonjsRequire(); } }, fn(module, module.exports), module.exports; } function commonjsRequire () { throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs'); } // Copyright Joyent, Inc. and other Node contributors. // If obj.hasOwnProperty has been overridden, then calling // obj.hasOwnProperty(prop) will break. // See: https://github.com/joyent/node/issues/1707 function hasOwnProperty(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); } var decode = function(qs, sep, eq, options) { sep = sep || '&'; eq = eq || '='; var obj = {}; if (typeof qs !== 'string' || qs.length === 0) { return obj; } var regexp = /\+/g; qs = qs.split(sep); var maxKeys = 1000; if (options && typeof options.maxKeys === 'number') { maxKeys = options.maxKeys; } var len = qs.length; // maxKeys <= 0 means that we should not limit keys count if (maxKeys > 0 && len > maxKeys) { len = maxKeys; } for (var i = 0; i < len; ++i) { var x = qs[i].replace(regexp, '%20'), idx = x.indexOf(eq), kstr, vstr, k, v; if (idx >= 0) { kstr = x.substr(0, idx); vstr = x.substr(idx + 1); } else { kstr = x; vstr = ''; } k = decodeURIComponent(kstr); v = decodeURIComponent(vstr); if (!hasOwnProperty(obj, k)) { obj[k] = v; } else if (Array.isArray(obj[k])) { obj[k].push(v); } else { obj[k] = [obj[k], v]; } } return obj; }; // Copyright Joyent, Inc. and other Node contributors. var stringifyPrimitive = function(v) { switch (typeof v) { case 'string': return v; case 'boolean': return v ? 'true' : 'false'; case 'number': return isFinite(v) ? v : ''; default: return ''; } }; var encode = function(obj, sep, eq, name) { sep = sep || '&'; eq = eq || '='; if (obj === null) { obj = undefined; } if (typeof obj === 'object') { return Object.keys(obj).map(function(k) { var ks = encodeURIComponent(stringifyPrimitive(k)) + eq; if (Array.isArray(obj[k])) { return obj[k].map(function(v) { return ks + encodeURIComponent(stringifyPrimitive(v)); }).join(sep); } else { return ks + encodeURIComponent(stringifyPrimitive(obj[k])); } }).join(sep); } if (!name) return ''; return encodeURIComponent(stringifyPrimitive(name)) + eq + encodeURIComponent(stringifyPrimitive(obj)); }; var querystring = createCommonjsModule(function (module, exports) { exports.decode = exports.parse = decode; exports.encode = exports.stringify = encode; }); /** * The main class for the library. * 1. Instantiate: new AxiosHooksMock(implementations) * 2. Add more implementations (if needed): .get('url', implementation) * 3. Implement (completes the chain, creates mock implementation): .implement(); * * This class is chainable, and aside from implement(), always returns itself. */ var AxiosHooksMock = /*#__PURE__*/function () { /** * Standard constructor for the class * @param mockItems Optional, @see AxiosHooksMockItem */ function AxiosHooksMock(mockItems, defaultImplementation) { var _this = this; this.implementations = new Map(); this.detectUrlAndMethod = function (config) { var _config$baseURL, _config$method; if (typeof config === 'string') { return { url: config, method: 'GET' }; } var queryParamsString = config.params ? "?" + querystring.stringify(config.params) : ''; return { url: "" + (!config.url.startsWith('http') ? (_config$baseURL = config.baseURL) != null ? _config$baseURL : '' : '') + config.url + queryParamsString, method: (_config$method = config.method) != null ? _config$method : 'GET' }; }; this.buildCompositeKey = function (url, method) { if (method === void 0) { method = 'GET'; } return method.toUpperCase() + "|" + url; }; if (mockItems) { mockItems.forEach(function (mockItem) { _this.addImplementation(mockItem.config, mockItem.implementation); }); } if (defaultImplementation) { this.defaultImplementation = defaultImplementation; } } /** Alternate constructor for those who hate the 'new' keyword. */ AxiosHooksMock.construct = function construct(mockItems, defaultImplementation) { return new AxiosHooksMock(mockItems, defaultImplementation); } /** * The only method of adding mock implementations to the greater mock class. * All constructors and convenience methods call this method, but this can also be called externally. * @param config @see AxiosRequestConfig or string * @param implementation @see AxiosHooksTuple - The standard output from useAxios */ ; var _proto = AxiosHooksMock.prototype; _proto.addImplementation = function addImplementation(config, implementation) { var _this$detectUrlAndMet = this.detectUrlAndMethod(config), url = _this$detectUrlAndMet.url, method = _this$detectUrlAndMet.method; var compositeKey = this.buildCompositeKey(url, method); this.implementations.set(compositeKey, implementation); return this; } /** * Convenience method to add a get implementation * @param url as string * @param implementation @see AxiosHooksTuple - The standard output from useAxios */ ; _proto.get = function get(url, implementation) { return this.addImplementation(url, implementation); } /** * Convenience method to add a post implementation * @param url as string * @param implementation @see AxiosHooksTuple - The standard output from useAxios */ ; _proto.post = function post(url, implementation) { return this.addImplementation({ url: url, method: 'POST' }, implementation); } /** * Convenience method to add a patch implementation * @param url as string * @param implementation @see AxiosHooksTuple - The standard output from useAxios */ ; _proto.put = function put(url, implementation) { return this.addImplementation({ url: url, method: 'PUT' }, implementation); } /** * Convenience method to add a patch implementation * @param url as string * @param implementation @see AxiosHooksTuple - The standard output from useAxios */ ; _proto.patch = function patch(url, implementation) { return this.addImplementation({ url: url, method: 'PATCH' }, implementation); } /** * Convenience method to add a delete implementation * @param url as string * @param implementation @see AxiosHooksTuple - The standard output from useAxios */ ; _proto["delete"] = function _delete(url, implementation) { return this.addImplementation({ url: url, method: 'DELETE' }, implementation); } /** * If no other implementation matches, this implementation is returned as a backup. * @param implementation @see AxiosHooksTuple - The standard output from useAxios */ ; _proto["default"] = function _default(implementation) { this.defaultImplementation = implementation; return this; } /** * Calling this completes the chain, and returns a function that can be used as a mock implementation in jest (or elsewhere). */ ; _proto.implement = function implement() { var _this2 = this; return function (config) { var _this2$detectUrlAndMe = _this2.detectUrlAndMethod(config), url = _this2$detectUrlAndMe.url, method = _this2$detectUrlAndMe.method; var compositeKey = _this2.buildCompositeKey(url, method); var response = _this2.implementations.get(compositeKey); if (!response && _this2.defaultImplementation) { return _this2.defaultImplementation; } return response; }; }; return AxiosHooksMock; }(); export default AxiosHooksMock; export { AxiosHooksMock }; //# sourceMappingURL=index.m.js.map