Skip to content

Commit

Permalink
removed _originalMatcher distinction
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Sep 23, 2018
1 parent 4404748 commit f2068d5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
6 changes: 4 additions & 2 deletions src/lib/compile-route.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ const getUrlMatcher = route => {
// of the route to allow for e.g. http://it.at.there being indistinguishable
// from http://it.at.there/ once we start generating Request/Url objects
const expectedUrl = normalizeUrl(matcher);
route._originalMatcher = expectedUrl;
if (route.identifier === matcher) {
route.identifier = expectedUrl;
}

return url => {
if (query && expectedUrl.indexOf('?')) {
Expand All @@ -121,7 +123,7 @@ const sanitizeRoute = route => {
if (route.method) {
route.method = route.method.toLowerCase();
}
route._originalMatcher = route.matcher;
route.identifier = route.name || route.matcher;

return route;
};
Expand Down
11 changes: 4 additions & 7 deletions src/lib/fetch-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ FetchMock.router = function(url, options, request) {
url,
options,
request,
name: route.name,
matcher: route._originalMatcher
identifier: route.identifier
});
return route;
}
Expand All @@ -158,15 +157,13 @@ FetchMock.push = function({
url,
options,
request,
name = null,
unmatched = false,
matcher = null
unmatched,
identifier
}) {
const args = [url, options];
args.request = request;
args.name = name;
args.identifier = identifier;
args.unmatched = unmatched;
args.matcher = matcher;
this._calls.push(args);
};

Expand Down
22 changes: 11 additions & 11 deletions src/lib/inspecting.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ FetchMock.filterCalls = function(nameOrMatcher, options) {
} else if (typeof nameOrMatcher === 'undefined') {
calls = calls;
} else if (isName(nameOrMatcher)) {
calls = calls.filter(({ name }) => name === nameOrMatcher);
calls = calls.filter(({ identifier }) => identifier === nameOrMatcher);
} else {
matcher = normalizeUrl(nameOrMatcher);
if (
this.routes.some(({ _originalMatcher }) => _originalMatcher === matcher)
this.routes.some(({ identifier }) => identifier === matcher)
) {
calls = calls.filter(call => call.matcher === matcher);
calls = calls.filter(call => call.identifier === matcher);
}
}

Expand Down Expand Up @@ -82,30 +82,30 @@ FetchMock.done = function(nameOrMatcher) {
// - nameOrMatcher them

// const calls = this.filterCalls(nameOrMatcher, options);
const names = nameOrMatcher && typeof nameOrMatcher !== 'boolean' ? [{ name: nameOrMatcher, _originalMatcher: nameOrMatcher }] : this.routes;
const identifiers = nameOrMatcher && typeof nameOrMatcher !== 'boolean' ? [{ identifier: nameOrMatcher }] : this.routes;
// Can't use array.every because
// a) not widely supported
// b) would exit after first failure, which would break the logging
return (
names
.map(({ name, _originalMatcher }) => {
identifiers
.map(({ identifier }) => {

if (!this.called(name || _originalMatcher)) {
console.warn(`Warning: ${name || _originalMatcher} not called`); // eslint-disable-line
if (!this.called(identifier)) {
console.warn(`Warning: ${identifier} not called`); // eslint-disable-line
return false;
}

const expectedTimes = (this.routes.find(
r => (r.name && (r.name === name ))||( r._originalMatcher === _originalMatcher)
r => r.identifier === identifier
) || {}).repeat;

if (!expectedTimes) {
return true;
}
const actualTimes = this.filterCalls(name || _originalMatcher).length;
const actualTimes = this.filterCalls(identifier).length;
if (expectedTimes > actualTimes) {
console.warn(
`Warning: ${name || _originalMatcher} only called ${actualTimes} times, but ${expectedTimes} expected`
`Warning: ${identifier} only called ${actualTimes} times, but ${expectedTimes} expected`
); // eslint-disable-line
return false;
} else {
Expand Down
3 changes: 1 addition & 2 deletions src/lib/set-up-and-tear-down.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ const getMatcher = (route, propName) => route2 =>
FetchMock.addRoute = function(uncompiledRoute) {
const route = this.compileRoute(uncompiledRoute);
const clashes = this.routes
.filter(getMatcher(route, 'name'))
.concat(this.routes.filter(getMatcher(route, '_originalMatcher')));
.filter(getMatcher(route, 'identifier'));

const overwriteRoutes =
'overwriteRoutes' in route
Expand Down

0 comments on commit f2068d5

Please sign in to comment.