fetch-mock
Advanced tools
Comparing version 8.0.0-alpha.9 to 8.0.0-alpha.10
@@ -43,2 +43,10 @@ const generateMatcher = require('./generate-matcher'); | ||
const delayResponse = route => { | ||
const { delay } = route; | ||
if (delay) { | ||
const response = route.response; | ||
route.response = new Promise(res => setTimeout(() => res(response), delay)); | ||
} | ||
}; | ||
module.exports = route => { | ||
@@ -49,2 +57,3 @@ validateRoute(route); | ||
limitMatcher(route); | ||
delayResponse(route); | ||
return route; | ||
@@ -51,0 +60,0 @@ }; |
const compileRoute = require('./compile-route'); | ||
const FetchMock = {}; | ||
FetchMock.mock = function(matcher, response, options = {}) { | ||
let route; | ||
FetchMock.mock = function(...args) { | ||
if (args.length) { | ||
const [matcher, response, options = {}] = args; | ||
let route; | ||
// Handle the variety of parameters accepted by mock (see README) | ||
if (matcher && response) { | ||
route = Object.assign( | ||
{ | ||
matcher, | ||
response | ||
}, | ||
options | ||
); | ||
} else if (matcher && matcher.matcher) { | ||
route = matcher; | ||
} else { | ||
throw new Error('fetch-mock: Invalid parameters passed to fetch-mock'); | ||
// Handle the variety of parameters accepted by mock (see README) | ||
if (matcher && response) { | ||
route = Object.assign( | ||
{ | ||
matcher, | ||
response | ||
}, | ||
options | ||
); | ||
} else if (matcher && matcher.matcher) { | ||
route = matcher; | ||
} else { | ||
throw new Error('fetch-mock: Invalid parameters passed to fetch-mock'); | ||
} | ||
this.addRoute(route); | ||
} | ||
this.addRoute(route); | ||
return this._mock(); | ||
@@ -25,0 +28,0 @@ }; |
'use strict'; | ||
var _promise = require('babel-runtime/core-js/promise'); | ||
var _promise2 = _interopRequireDefault(_promise); | ||
var _assign = require('babel-runtime/core-js/object/assign'); | ||
@@ -51,2 +55,15 @@ | ||
var delayResponse = function delayResponse(route) { | ||
var delay = route.delay; | ||
if (delay) { | ||
var response = route.response; | ||
route.response = new _promise2.default(function (res) { | ||
return setTimeout(function () { | ||
return res(response); | ||
}, delay); | ||
}); | ||
} | ||
}; | ||
module.exports = function (route) { | ||
@@ -57,2 +74,3 @@ validateRoute(route); | ||
limitMatcher(route); | ||
delayResponse(route); | ||
return route; | ||
@@ -59,0 +77,0 @@ }; |
@@ -12,21 +12,30 @@ 'use strict'; | ||
FetchMock.mock = function (matcher, response) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
FetchMock.mock = function () { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var route = void 0; | ||
if (args.length) { | ||
var matcher = args[0], | ||
response = args[1], | ||
_args$ = args[2], | ||
options = _args$ === undefined ? {} : _args$; | ||
// 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'); | ||
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); | ||
} | ||
this.addRoute(route); | ||
return this._mock(); | ||
@@ -33,0 +42,0 @@ }; |
{ | ||
"name": "fetch-mock", | ||
"type": "module", | ||
"version": "8.0.0-alpha.9", | ||
"version": "8.0.0-alpha.10", | ||
"description": "Mock http requests made using fetch (or isomorphic-fetch)", | ||
@@ -103,4 +103,5 @@ "main": "./cjs/server.js", | ||
"sinon-chai": "^2.14.0", | ||
"typescript": "^3.6.4", | ||
"webpack": "^4.41.2" | ||
} | ||
} |
import fetchMock = require('..'); | ||
fetchMock.mock(); | ||
fetchMock.mock("http://test.com", 200); | ||
@@ -9,2 +10,11 @@ fetchMock.mock("http://test.com", 200, { | ||
}); | ||
fetchMock.mock("http://test.com", 200, { | ||
body: { | ||
test: [{ | ||
string: "value", | ||
number: 1.34, | ||
bool: true, | ||
}] | ||
} | ||
}); | ||
fetchMock.mock("http//test.com", 200, { | ||
@@ -28,2 +38,5 @@ query: { | ||
}); | ||
fetchMock.mock("http://test.com", 200, { | ||
delay: 10 | ||
}); | ||
fetchMock.mock(/test\.com/, 200); | ||
@@ -30,0 +43,0 @@ fetchMock.mock(() => true, 200); |
@@ -120,2 +120,3 @@ // Project: https://github.com/wheresrhys/fetch-mock, http://www.wheresrhys.co.uk/fetch-mock | ||
/** | ||
@@ -157,2 +158,8 @@ * Mock options object | ||
/** | ||
* JSON serialisable object literal. Allowing any object for now | ||
* But in typescript 3.7 will change to JSON | ||
*/ | ||
body?: object; | ||
/** | ||
* A function for arbitrary matching | ||
@@ -189,2 +196,8 @@ */ | ||
/** | ||
* integer, n, delays responding for the number of milliseconds | ||
* specified. | ||
*/ | ||
delay?: number; | ||
/** | ||
* Convert objects into JSON before delivering as stub responses. Can | ||
@@ -256,2 +269,10 @@ * be useful to set to false globally if e.g. dealing with a lot of | ||
/** | ||
* Replaces fetch() with a stub which records its calls, grouped by | ||
* route, and optionally returns a mocked Response object or passes the | ||
* call through to fetch(). Calls to .mock() can be chained. | ||
* @param options The route to mock | ||
*/ | ||
mock(): this; | ||
/** | ||
* Returns a drop-in mock for fetch which can be passed to other mocking | ||
@@ -258,0 +279,0 @@ * libraries. It implements the full fetch-mock api and maintains its |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
2610848
74529
33