Skip to content

Commit

Permalink
Add usingPromise() method on fakes to fix issue #2293 (#2301)
Browse files Browse the repository at this point in the history
* Add usingPromise() method on fakes to fix issue #2293

* Simplify implementation of injected promise implementation, reducing required code branching

Co-authored-by: Roman Balayan <roman.balayan@paymaya.com>
  • Loading branch information
romanbalayan and Roman Balayan committed Oct 13, 2020
1 parent 7978ed3 commit 3b80dbb
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 49 deletions.
109 changes: 60 additions & 49 deletions lib/sinon/fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,70 +37,81 @@ 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 = Promise;

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() {
return promiseLib.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() {
return promiseLib.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();
43 changes: 43 additions & 0 deletions test/fake-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,47 @@ describe("fake", function() {
assert.same(myFake.printf, proxy.printf);
});
});

describe(".usingPromise", function() {
before(requirePromiseSupport);

it("should exist and be a function", function() {
assert(fake.usingPromise);
assert.isFunction(fake.usingPromise);
});

it("should set the promise used by resolve", function() {
var promise = {
resolve: function(value) {
return Promise.resolve(value);
}
};
var object = {};

var myFake = fake.usingPromise(promise).resolves(object);

return myFake().then(function(actual) {
assert.same(actual, object, "Same object resolved");
});
});

it("should set the promise used by reject", function() {
var promise = {
reject: function(err) {
return Promise.reject(err);
}
};
var reason = new Error();

var myFake = fake.usingPromise(promise).rejects(reason);

return myFake()
.then(function() {
referee.fail("this should not resolve");
})
.catch(function(actual) {
assert.same(actual, reason, "Same object resolved");
});
});
});
});

0 comments on commit 3b80dbb

Please sign in to comment.