Skip to content

Commit

Permalink
method factory
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Dec 14, 2019
1 parent ddba054 commit 5d83bba
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/lib/set-up-and-tear-down.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,18 @@ FetchMock.spy = function() {

FetchMock.compileRoute = compileRoute;

const applyShorthand = (args, shorthandOptions) =>
Object.assign(argsToRoute(args), shorthandOptions);

FetchMock.once = function(...args) {
return this.mock(applyShorthand(args, { repeat: 1 }));
const defineShorthand = (methodName, underlyingMethod, shorthandOptions) => {
FetchMock[methodName] = function(...args) {
return this[underlyingMethod](
Object.assign(argsToRoute(args), shorthandOptions)
);
};
};
defineShorthand('once', 'mock', { repeat: 1 });

['get', 'post', 'put', 'delete', 'head', 'patch'].forEach(method => {
FetchMock[method] = function(...args) {
return this.mock(applyShorthand(args, { method }));
};
FetchMock[`${method}Once`] = function(...args) {
return this.once(applyShorthand(args, { method }));
};
defineShorthand(method, 'mock', { method });
defineShorthand(`${method}Once`, 'once', { method });
});

FetchMock.resetBehavior = function() {
Expand Down

0 comments on commit 5d83bba

Please sign in to comment.