From c8ffc1b7f6de592f146e1823d48d582a6718ba2a Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Sun, 16 Aug 2020 11:29:57 +0100 Subject: [PATCH] define equivalents to exitsing mock methods that return the route --- src/lib/set-up-and-tear-down.js | 55 +++++++++++++++++---------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/src/lib/set-up-and-tear-down.js b/src/lib/set-up-and-tear-down.js index f707c2d3..538fb976 100644 --- a/src/lib/set-up-and-tear-down.js +++ b/src/lib/set-up-and-tear-down.js @@ -1,24 +1,26 @@ const { debug, setDebugPhase } = require('./debug'); const FetchMock = {}; - const Route = require('../Route'); FetchMock.addMatcher = function (matcher) { Route.addMatcher(matcher); }; - FetchMock.compileRoute = function (config) { return new Route(config); }; -FetchMock.mock = function (...args) { +FetchMock.$mock = function (...args) { setDebugPhase('setup'); + this._mock(); if (args.length) { - this.addRoute(args); + return this.addRoute(args); } +}; - return this._mock(); +FetchMock.mock = function (...args) { + this.$mock(...args) + return this; }; FetchMock.addRoute = function (uncompiledRoute) { @@ -52,6 +54,7 @@ FetchMock.addRoute = function (uncompiledRoute) { this._uncompiledRoutes.push(uncompiledRoute); this.routes.push(route); + return route; }; FetchMock._mock = function () { @@ -84,7 +87,7 @@ FetchMock.spy = function (route) { : this.catch(this.getNativeFetch()); }; -const defineShorthand = (methodName, underlyingMethod, shorthandOptions) => { +const _defineShorthand = (methodName, underlyingMethod, shorthandOptions) => { FetchMock[methodName] = function (matcher, response, options) { return this[underlyingMethod]( matcher, @@ -94,36 +97,34 @@ const defineShorthand = (methodName, underlyingMethod, shorthandOptions) => { }; }; -const defineGreedyShorthand = (methodName, underlyingMethod) => { +const defineShorthands = (methodName, underlyingMethod, shorthandOptions) => { + _defineShorthand(methodName, underlyingMethod, shorthandOptions) + _defineShorthand(`$${methodName}`, `$${underlyingMethod}`, shorthandOptions) +} + +const _defineGreedyShorthand = (methodName, underlyingMethod) => { FetchMock[methodName] = function (response, options) { return this[underlyingMethod]({}, response, options); }; }; -defineShorthand('sticky', 'mock', { sticky: true }); -defineShorthand('once', 'mock', { repeat: 1 }); -defineGreedyShorthand('any', 'mock'); -defineGreedyShorthand('anyOnce', 'once'); +const defineGreedyShorthands = (methodName, underlyingMethod, shorthandOptions) => { + _defineGreedyShorthand(methodName, underlyingMethod) + _defineGreedyShorthand(`$${methodName}`, `$${underlyingMethod}`) +} + +defineShorthands('sticky', 'mock', { sticky: true }); +defineShorthands('once', 'mock', { repeat: 1 }); +defineGreedyShorthands('any', 'mock'); +defineGreedyShorthands('anyOnce', 'once'); ['get', 'post', 'put', 'delete', 'head', 'patch'].forEach((method) => { - defineShorthand(method, 'mock', { method }); - defineShorthand(`${method}Once`, 'once', { method }); - defineGreedyShorthand(`${method}Any`, method); - defineGreedyShorthand(`${method}AnyOnce`, `${method}Once`); + defineShorthands(method, 'mock', { method }); + defineShorthands(`${method}Once`, 'once', { method }); + defineGreedyShorthands(`${method}Any`, method); + defineGreedyShorthands(`${method}AnyOnce`, `${method}Once`); }); -// defineShorthand('sticky'); -// defineShorthand('once'); -// defineShorthand('any'); -// defineShorthand('anyOnce'); - -// ['get', 'post', 'put', 'delete', 'head', 'patch'].forEach((method) => { -// defineShorthand(method); -// defineShorthand(`${method}Once`); -// defineShorthand(`${method}Any`); -// defineShorthand(`${method}AnyOnce`); -// }); - const mochaAsyncHookWorkaround = (options) => { // HACK workaround for this https://github.com/mochajs/mocha/issues/4280 // Note that it doesn't matter that we call it _before_ carrying out all