Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support exact-matching on FormData bodies #1

Merged
merged 2 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ coverage/
.nyc_output

# built files
/es5
/esm
/cjs
# /es5
# /esm
# /cjs
/docs/_site/
150 changes: 150 additions & 0 deletions cjs/Route/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
const builtInMatchers = require('./matchers');

const { debug, setDebugNamespace, getDebug } = require('../lib/debug');

const isUrlMatcher = (matcher) =>
matcher instanceof RegExp ||
typeof matcher === 'string' ||
(typeof matcher === 'object' && 'href' in matcher);

const isFunctionMatcher = (matcher) => typeof matcher === 'function';

class Route {
constructor(args, fetchMock) {
this.fetchMock = fetchMock;
const debug = getDebug('compileRoute()');
debug('Compiling route');
this.init(args);
this.sanitize();
this.validate();
this.generateMatcher();
this.limit();
this.delayResponse();
}

validate() {
if (!('response' in this)) {
throw new Error('fetch-mock: Each route must define a response');
}

if (!Route.registeredMatchers.some(({ name }) => name in this)) {
throw new Error(
"fetch-mock: Each route must specify some criteria for matching calls to fetch. To match all calls use '*'"
);
}
}

init(args) {
const [matcher, response, options = {}] = args;

const routeConfig = {};

if (isUrlMatcher(matcher) || isFunctionMatcher(matcher)) {
routeConfig.matcher = matcher;
} else {
Object.assign(routeConfig, matcher);
}

if (typeof response !== 'undefined') {
routeConfig.response = response;
}

Object.assign(routeConfig, options);
Object.assign(this, routeConfig);
}

sanitize() {
const debug = getDebug('sanitize()');
debug('Sanitizing route properties');

if (this.method) {
debug(`Converting method ${this.method} to lower case`);
this.method = this.method.toLowerCase();
}
if (isUrlMatcher(this.matcher)) {
debug('Mock uses a url matcher', this.matcher);
this.url = this.matcher;
delete this.matcher;
}

this.functionMatcher = this.matcher || this.functionMatcher;

debug('Setting route.identifier...');
debug(` route.name is ${this.name}`);
debug(` route.url is ${this.url}`);
debug(` route.functionMatcher is ${this.functionMatcher}`);
this.identifier = this.name || this.url || this.functionMatcher;
debug(` -> route.identifier set to ${this.identifier}`);
}

generateMatcher() {
setDebugNamespace('generateMatcher()');
debug('Compiling matcher for route');

const activeMatchers = Route.registeredMatchers
.map(
({ name, matcher, usesBody }) =>
this[name] && { matcher: matcher(this, this.fetchMock), usesBody }
)
.filter((matcher) => Boolean(matcher));

this.usesBody = activeMatchers.some(({ usesBody }) => usesBody);

debug('Compiled matcher for route');
setDebugNamespace();
this.matcher = (url, options = {}, request) =>
activeMatchers.every(({ matcher }) => matcher(url, options, request));
}

limit() {
const debug = getDebug('limit()');
debug('Limiting number of requests to handle by route');
if (!this.repeat) {
debug(
' No `repeat` value set on route. Will match any number of requests'
);
return;
}

debug(` Route set to repeat ${this.repeat} times`);
const matcher = this.matcher;
let timesLeft = this.repeat;
this.matcher = (url, options) => {
const match = timesLeft && matcher(url, options);
if (match) {
timesLeft--;
return true;
}
};
this.reset = () => (timesLeft = this.repeat);
}

delayResponse() {
const debug = getDebug('delayResponse()');
debug(`Applying response delay settings`);
if (this.delay) {
debug(` Wrapping response in delay of ${this.delay} miliseconds`);
const response = this.response;
this.response = () => {
debug(`Delaying response by ${this.delay} miliseconds`);
return new Promise((res) =>
setTimeout(() => res(response), this.delay)
);
};
} else {
debug(
` No delay set on route. Will respond 'immediately' (but asynchronously)`
);
}
}

static addMatcher(matcher) {
Route.registeredMatchers.push(matcher);
}
}

Route.registeredMatchers = [];

builtInMatchers.forEach(Route.addMatcher);

module.exports = Route;
Loading