Skip to content

Commit

Permalink
avoid passing arrays around
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Dec 2, 2020
1 parent 9e8e8d8 commit 6d5b0fd
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 23 deletions.
12 changes: 5 additions & 7 deletions src/Route/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const isUrlMatcher = (matcher) =>
const isFunctionMatcher = (matcher) => typeof matcher === 'function';

class Route {
constructor(args) {
constructor(...args) {
const debug = getDebug('compileRoute()');
debug('Compiling route');
this.init(args);
this.init(...args);
this.sanitize();
this.validate();
this.generateMatcher();
Expand All @@ -33,9 +33,7 @@ class Route {
}
}

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

init(matcher, response, options = {}) {
const routeConfig = {};

if (isUrlMatcher(matcher) || isFunctionMatcher(matcher)) {
Expand Down Expand Up @@ -143,8 +141,8 @@ class Route {
Route.registeredMatchers.push(matcher);
}

static newRoute(config) {
return new Route(config);
static newRoute(...config) {
return new Route(...config);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ FetchMock.createInstance = function () {
const instance = Object.create(FetchMock);
instance._uncompiledRoutes = (this._uncompiledRoutes || []).slice();
instance.routes = instance._uncompiledRoutes.map((config) =>
Route.newRoute(config)
Route.newRoute(...config)
);
instance.fallbackResponse = this.fallbackResponse || undefined;
instance.config = Object.assign({}, this.config || FetchMock.config);
Expand Down
6 changes: 3 additions & 3 deletions src/lib/inspecting.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const isName = (nameOrMatcher) =>
typeof nameOrMatcher === 'string' && /^[\da-zA-Z\-]+$/.test(nameOrMatcher);

const filterCallsWithMatcher = function (matcher, options = {}, calls) {
({ matcher } = Route.newRoute([
Object.assign({ matcher, response: 'ok' }, options),
]));
({ matcher } = Route.newRoute(
Object.assign({ matcher, response: 'ok' }, options)
));
return calls.filter(({ url, options }) =>
matcher(normalizeUrl(url), options, null, this)
);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/set-up-and-tear-down.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ FetchMock.mock = function (...args) {

FetchMock.addRoute = function (uncompiledRoute) {
debug('Adding route', uncompiledRoute);
const route = Route.newRoute(uncompiledRoute);
const route = Route.newRoute(...uncompiledRoute);
const clashes = this.routes.filter(({ identifier, method }) => {
const isMatch =
typeof identifier === 'function'
Expand Down
16 changes: 6 additions & 10 deletions test/specs/set-up-and-tear-down.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ describe('Set up and tear down', () => {
response: 200,
};
expect(() => fm.mock(config)).not.to.throw();
expect(Route.newRoute).calledWith([config]);
expect(Route.newRoute).calledWith(config);
expect(fm._mock).called;
});

it('accepts matcher, route pairs', () => {
expect(() => fm.mock('*', 200)).not.to.throw();
expect(Route.newRoute).calledWith(['*', 200]);
expect(Route.newRoute).calledWith('*', 200);
expect(fm._mock).called;
});

Expand All @@ -75,14 +75,10 @@ describe('Set up and tear down', () => {
some: 'prop',
})
).not.to.throw();
expect(Route.newRoute).calledWith([
'*',
'ok',
{
method: 'PUT',
some: 'prop',
},
]);
expect(Route.newRoute).calledWith('*', 'ok', {
method: 'PUT',
some: 'prop',
});
expect(fm._mock).called;
});

Expand Down
2 changes: 1 addition & 1 deletion test/specs/shorthands.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('shorthands', () => {
fm = fetchMock.createInstance();
sinon.spy(Route, 'newRoute');
fm.config.warnOnUnmatched = false;
expectRoute = (...args) => expect(Route.newRoute).calledWith(args);
expectRoute = (...args) => expect(Route.newRoute).calledWith(...args);
});
afterEach(() => {
Route.newRoute.resetHistory();
Expand Down

0 comments on commit 6d5b0fd

Please sign in to comment.