-
-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue #1919 by adding a new util function to prevent duplicate co…
…de (#1942) * Issue #1919 Refactor to reduce duplicate code and add tests * undo changes to package-lock.json
- Loading branch information
1 parent
07e4994
commit 96f8e40
Showing
4 changed files
with
67 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"use strict"; | ||
|
||
var arrayProto = require("@sinonjs/commons").prototypes.array; | ||
var reduce = arrayProto.reduce; | ||
|
||
module.exports = function exportAsyncBehaviors(behaviorMethods) { | ||
return reduce( | ||
Object.keys(behaviorMethods), | ||
function(acc, method) { | ||
// need to avoid creating another async versions of the newly added async methods | ||
if (method.match(/^(callsArg|yields)/) && !method.match(/Async/)) { | ||
acc[method + "Async"] = function() { | ||
var result = behaviorMethods[method].apply(this, arguments); | ||
this.callbackAsync = true; | ||
return result; | ||
}; | ||
} | ||
return acc; | ||
}, | ||
{} | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"use strict"; | ||
|
||
var referee = require("@sinonjs/referee"); | ||
var assert = referee.assert; | ||
var exportAsyncBehaviors = require("../../../lib/sinon/util/core/export-async-behaviors"); | ||
|
||
describe("util/core/exportAsyncBehaviors", function() { | ||
describe("for methods with names starting with 'callsArg' or 'yields'", function() { | ||
it("should create an async version", function() { | ||
var methods = { | ||
yieldsOn: function yieldsOn() { | ||
return "2"; | ||
}, | ||
callsArg: function callsArg() { | ||
return "3"; | ||
} | ||
}; | ||
assert.equals(Object.keys(exportAsyncBehaviors(methods)), ["yieldsOnAsync", "callsArgAsync"]); | ||
}); | ||
}); | ||
|
||
describe("for methods with names not starting with 'callsArg' or 'yields'", function() { | ||
it("should not add any new methods", function() { | ||
var methods = { | ||
callsFake: function callsFake() { | ||
return "1"; | ||
}, | ||
resolvesThisAsync: function resolvesThisAsync() { | ||
return "4"; | ||
} | ||
}; | ||
assert.equals(Object.keys(exportAsyncBehaviors(methods)), []); | ||
}); | ||
}); | ||
}); |