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 bb89087
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 29 deletions.
12 changes: 5 additions & 7 deletions src/Route/index.js
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 compileRoute(...config) {
return new Route(...config);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/index.js
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.compileRoute(...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
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.compileRoute(
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
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.compileRoute(...uncompiledRoute);
const clashes = this.routes.filter(({ identifier, method }) => {
const isMatch =
typeof identifier === 'function'
Expand Down
22 changes: 9 additions & 13 deletions test/specs/set-up-and-tear-down.test.js
Expand Up @@ -43,12 +43,12 @@ describe('Set up and tear down', () => {

describe('parameters', () => {
beforeEach(() => {
sinon.spy(Route, 'newRoute');
sinon.spy(Route, 'compileRoute');
sinon.stub(fm, '_mock').returns(fm);
});

afterEach(() => {
Route.newRoute.restore();
Route.compileRoute.restore();
fm._mock.restore();
});

Expand All @@ -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.compileRoute).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.compileRoute).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.compileRoute).calledWith('*', 'ok', {
method: 'PUT',
some: 'prop',
});
expect(fm._mock).called;
});

Expand All @@ -96,7 +92,7 @@ describe('Set up and tear down', () => {

it('can be called with no parameters', () => {
expect(() => fm.mock()).not.to.throw();
expect(Route.newRoute).not.called;
expect(Route.compileRoute).not.called;
expect(fm._mock).called;
});

Expand Down
8 changes: 4 additions & 4 deletions test/specs/shorthands.test.js
Expand Up @@ -26,16 +26,16 @@ describe('shorthands', () => {

before(() => {
fm = fetchMock.createInstance();
sinon.spy(Route, 'newRoute');
sinon.spy(Route, 'compileRoute');
fm.config.warnOnUnmatched = false;
expectRoute = (...args) => expect(Route.newRoute).calledWith(args);
expectRoute = (...args) => expect(Route.compileRoute).calledWith(...args);
});
afterEach(() => {
Route.newRoute.resetHistory();
Route.compileRoute.resetHistory();
fm.restore({ sticky: true });
});

after(() => Route.newRoute.restore());
after(() => Route.compileRoute.restore());

it('has sticky() shorthand method', () => {
fm.sticky('a', 'b');
Expand Down

0 comments on commit bb89087

Please sign in to comment.