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
Add usingPromise() method on fakes to fix issue #2293 #2301
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -37,70 +37,87 @@ function wrapFunc(f) { | |||||||
return proxy; | ||||||||
} | ||||||||
|
||||||||
function fake(f) { | ||||||||
if (arguments.length > 0 && typeof f !== "function") { | ||||||||
throw new TypeError("Expected f argument to be a Function"); | ||||||||
} | ||||||||
function fakeClass() { | ||||||||
var promiseLib; | ||||||||
|
||||||||
return wrapFunc(f); | ||||||||
} | ||||||||
function fake(f) { | ||||||||
if (arguments.length > 0 && typeof f !== "function") { | ||||||||
throw new TypeError("Expected f argument to be a Function"); | ||||||||
} | ||||||||
|
||||||||
fake.returns = function returns(value) { | ||||||||
function f() { | ||||||||
return value; | ||||||||
return wrapFunc(f); | ||||||||
} | ||||||||
|
||||||||
return wrapFunc(f); | ||||||||
}; | ||||||||
|
||||||||
fake.throws = function throws(value) { | ||||||||
function f() { | ||||||||
throw getError(value); | ||||||||
} | ||||||||
fake.returns = function returns(value) { | ||||||||
function f() { | ||||||||
return value; | ||||||||
} | ||||||||
|
||||||||
return wrapFunc(f); | ||||||||
}; | ||||||||
return wrapFunc(f); | ||||||||
}; | ||||||||
|
||||||||
fake.resolves = function resolves(value) { | ||||||||
function f() { | ||||||||
return Promise.resolve(value); | ||||||||
} | ||||||||
fake.throws = function throws(value) { | ||||||||
function f() { | ||||||||
throw getError(value); | ||||||||
} | ||||||||
|
||||||||
return wrapFunc(f); | ||||||||
}; | ||||||||
return wrapFunc(f); | ||||||||
}; | ||||||||
|
||||||||
fake.rejects = function rejects(value) { | ||||||||
function f() { | ||||||||
return Promise.reject(getError(value)); | ||||||||
} | ||||||||
fake.resolves = function resolves(value) { | ||||||||
function f() { | ||||||||
if (promiseLib) { | ||||||||
return promiseLib.resolve(value); | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
return Promise.resolve(value); | ||||||||
} | ||||||||
|
||||||||
return wrapFunc(f); | ||||||||
}; | ||||||||
return wrapFunc(f); | ||||||||
}; | ||||||||
|
||||||||
function yieldInternal(async, values) { | ||||||||
function f() { | ||||||||
var callback = arguments[arguments.length - 1]; | ||||||||
if (typeof callback !== "function") { | ||||||||
throw new TypeError("Expected last argument to be a function"); | ||||||||
fake.rejects = function rejects(value) { | ||||||||
function f() { | ||||||||
if (promiseLib) { | ||||||||
return promiseLib.reject(value); | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @fatso83, That actually makes a whole lot of sense, will take those edits. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also noticed that i'm not wrapping the the rejected object as an Error. Will correct that as well. |
||||||||
return Promise.reject(getError(value)); | ||||||||
} | ||||||||
if (async) { | ||||||||
nextTick(function() { | ||||||||
|
||||||||
return wrapFunc(f); | ||||||||
}; | ||||||||
|
||||||||
fake.usingPromise = function usingPromise(promiseLibrary) { | ||||||||
promiseLib = promiseLibrary; | ||||||||
return fake; | ||||||||
}; | ||||||||
|
||||||||
function yieldInternal(async, values) { | ||||||||
function f() { | ||||||||
var callback = arguments[arguments.length - 1]; | ||||||||
if (typeof callback !== "function") { | ||||||||
throw new TypeError("Expected last argument to be a function"); | ||||||||
} | ||||||||
if (async) { | ||||||||
nextTick(function() { | ||||||||
callback.apply(null, values); | ||||||||
}); | ||||||||
} else { | ||||||||
callback.apply(null, values); | ||||||||
}); | ||||||||
} else { | ||||||||
callback.apply(null, values); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
return wrapFunc(f); | ||||||||
} | ||||||||
|
||||||||
return wrapFunc(f); | ||||||||
} | ||||||||
fake.yields = function yields() { | ||||||||
return yieldInternal(false, slice(arguments)); | ||||||||
}; | ||||||||
|
||||||||
fake.yields = function yields() { | ||||||||
return yieldInternal(false, slice(arguments)); | ||||||||
}; | ||||||||
fake.yieldsAsync = function yieldsAsync() { | ||||||||
return yieldInternal(true, slice(arguments)); | ||||||||
}; | ||||||||
|
||||||||
fake.yieldsAsync = function yieldsAsync() { | ||||||||
return yieldInternal(true, slice(arguments)); | ||||||||
}; | ||||||||
return fake; | ||||||||
} | ||||||||
|
||||||||
module.exports = fake; | ||||||||
module.exports = fakeClass(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If they always have the same interface, why bother with branching?