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

Refactor for obj matcher #492

Merged
merged 3 commits into from
Dec 28, 2019
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
29 changes: 26 additions & 3 deletions src/lib/compile-route.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@ const isUrlMatcher = matcher =>
matcher instanceof RegExp ||
typeof matcher === 'string' ||
(typeof matcher === 'object' && 'href' in matcher);
const isFunctionMatcher = matcher => typeof matcher === 'function';

const argsToRoute = args => {
const [matcher, response, options = {}] = args;

const routeConfig = {};

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

if (response) {
routeConfig.response = response;
}

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

const sanitizeRoute = route => {
route = Object.assign({}, route);
Expand Down Expand Up @@ -68,13 +88,16 @@ const delayResponse = route => {
}
};

module.exports = route => {
route = sanitizeRoute(route);
const compileRoute = function(args) {
const route = sanitizeRoute(argsToRoute(args));
validateRoute(route);
route.matcher = generateMatcher(route);
limitMatcher(route);
delayResponse(route);
return route;
};

module.exports.sanitizeRoute = sanitizeRoute;
module.exports = {
compileRoute,
sanitizeRoute
};
35 changes: 6 additions & 29 deletions src/lib/set-up-and-tear-down.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,9 @@
const compileRoute = require('./compile-route');
const { compileRoute } = require('./compile-route');
const FetchMock = {};

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

const argsToRoute = args => {
const [matcher, response, options = {}] = args;

const routeConfig = {};

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

if (response) {
routeConfig.response = response;
}

Object.assign(routeConfig, options);

return routeConfig;
};

FetchMock.mock = function(...args) {
if (args.length) {
this.addRoute(argsToRoute(args));
this.addRoute(args);
}

return this._mock();
Expand Down Expand Up @@ -99,9 +74,11 @@ FetchMock.spy = function() {
FetchMock.compileRoute = compileRoute;

const defineShorthand = (methodName, underlyingMethod, shorthandOptions) => {
FetchMock[methodName] = function(...args) {
FetchMock[methodName] = function(matcher, response, options) {
return this[underlyingMethod](
Object.assign(argsToRoute(args), shorthandOptions)
matcher,
response,
Object.assign(options || {}, shorthandOptions)
);
};
};
Expand Down
67 changes: 40 additions & 27 deletions test/specs/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,45 +195,58 @@ module.exports = fetchMock => {

describe('strict matching shorthands', () => {
it('has once shorthand method', () => {
sinon.stub(fm, 'mock');
sinon.spy(fm, 'compileRoute');
fm['once']('a', 'b');
fm['once']('a', 'b', { opt: 'c' });
expect(fm.mock.calledWith({ matcher: 'a', response: 'b', repeat: 1 }))
.to.be.true;
fm['once']('c', 'd', { opt: 'e' });
expect(
fm.mock.calledWith({
matcher: 'a',
response: 'b',
opt: 'c',
repeat: 1
})
fm.compileRoute.calledWith([
'a',
'b',
{
repeat: 1
}
])
).to.be.true;
expect(
fm.compileRoute.calledWith([
'c',
'd',
{
opt: 'e',
repeat: 1
}
])
).to.be.true;
fm.mock.restore();
fm.compileRoute.restore();
});

'get,post,put,delete,head,patch'.split(',').forEach(method => {
it(`has once shorthand for ${method.toUpperCase()}`, () => {
sinon.stub(fm, 'mock');
sinon.spy(fm, 'compileRoute');
fm[method + 'Once']('a', 'b');
fm[method + 'Once']('a', 'b', { opt: 'c' });
fm[method + 'Once']('c', 'd', { opt: 'e' });
expect(
fm.mock.calledWith({
matcher: 'a',
response: 'b',
method: method,
repeat: 1
})
fm.compileRoute.calledWith([
'a',
'b',
{
method: method,
repeat: 1
}
])
).to.be.true;
expect(
fm.mock.calledWith({
matcher: 'a',
response: 'b',
opt: 'c',
method: method,
repeat: 1
})
fm.compileRoute.calledWith([
'c',
'd',
{
opt: 'e',
method: method,
repeat: 1
}
])
).to.be.true;
fm.mock.restore();
fm.compileRoute.restore();
});
});
});
Expand Down
74 changes: 29 additions & 45 deletions test/specs/set-up-and-tear-down.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,13 @@ module.exports = fetchMock => {
response: 200
};
expect(() => fm.mock(config)).not.to.throw();
expect(fm.compileRoute).calledWith(config);
expect(fm.compileRoute).calledWith([config]);
expect(fm._mock).called;
});

it('accepts matcher, route pairs', () => {
expect(() => fm.mock('http://it.at.there/', 200)).not.to.throw();
expect(fm.compileRoute).calledWith({
matcher: 'http://it.at.there/',
response: 200
});
expect(fm.compileRoute).calledWith(['http://it.at.there/', 200]);
expect(fm._mock).called;
});

Expand All @@ -79,12 +76,14 @@ module.exports = fetchMock => {
some: 'prop'
})
).not.to.throw();
expect(fm.compileRoute).calledWith({
matcher: 'http://it.at.there/',
response: 'ok',
method: 'PUT',
some: 'prop'
});
expect(fm.compileRoute).calledWith([
'http://it.at.there/',
'ok',
{
method: 'PUT',
some: 'prop'
}
]);
expect(fm._mock).called;
});

Expand Down Expand Up @@ -115,46 +114,31 @@ module.exports = fetchMock => {
testChainableMethod(() => fm, method, [/a/, 200]);

it(`has shorthand for ${method.toUpperCase()}`, () => {
sinon.stub(fm, 'mock');
sinon.spy(fm, 'compileRoute');
fm[method]('a', 'b');
fm[method]('a', 'b', { opt: 'c' });
expect(fm.mock).calledWith({
matcher: 'a',
response: 'b',
method: method
});
expect(fm.mock).calledWith({
matcher: 'a',
response: 'b',
opt: 'c',
method: method
});
fm.mock.restore();
fm[method]('c', 'd', { opt: 'e' });
expect(fm.compileRoute).calledWith([
'a',
'b',
{
method: method
}
]);
expect(fm.compileRoute).calledWith([
'c',
'd',
{
opt: 'e',
method: method
}
]);
fm.compileRoute.restore();
fm.restore();
});

testChainableMethod(() => fm, `${method}Once`, [/a/, 200]);

it(`has shorthand for ${method.toUpperCase()} called once`, () => {
sinon.stub(fm, 'mock');
fm[`${method}Once`]('a', 'b');
fm[`${method}Once`]('a', 'b', { opt: 'c' });
expect(fm.mock).calledWith({
matcher: 'a',
response: 'b',
method: method,
repeat: 1
});
expect(fm.mock).calledWith({
matcher: 'a',
response: 'b',
opt: 'c',
method: method,
repeat: 1
});
fm.mock.restore();
fm.restore();
});
// tests for behaviour of 'once' shorthands are in repeat.test.js
});
});

Expand Down