From 87524481d787a02cd5180b99894116355ce39c80 Mon Sep 17 00:00:00 2001 From: Hugo Muller Date: Thu, 10 Aug 2017 08:20:00 +0200 Subject: [PATCH] fixes issue #1368 by adding stub#resolvesThis (#1517) closes #1368 by adding stub#resolvesThis --- lib/sinon/behavior.js | 3 ++ lib/sinon/default-behaviors.js | 12 ++++++++ lib/sinon/stub.js | 1 + test/stub-test.js | 54 ++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+) diff --git a/lib/sinon/behavior.js b/lib/sinon/behavior.js index 640261fe8..042b5ec20 100644 --- a/lib/sinon/behavior.js +++ b/lib/sinon/behavior.js @@ -117,6 +117,7 @@ var proto = { this.exception || typeof this.returnArgAt === "number" || this.returnThis || + this.resolveThis || typeof this.throwArgAt === "number" || this.fakeFn || this.returnValueDefined); @@ -146,6 +147,8 @@ var proto = { throw args[this.throwArgAt]; } else if (this.fakeFn) { return this.fakeFn.apply(context, args); + } else if (this.resolveThis) { + return (this.promiseLibrary || Promise).resolve(context); } else if (this.resolve) { return (this.promiseLibrary || Promise).resolve(this.returnValue); } else if (this.reject) { diff --git a/lib/sinon/default-behaviors.js b/lib/sinon/default-behaviors.js index 46e4f160e..5c5568595 100644 --- a/lib/sinon/default-behaviors.js +++ b/lib/sinon/default-behaviors.js @@ -163,6 +163,7 @@ module.exports = { resolves: function resolves(fake, value) { fake.returnValue = value; fake.resolve = true; + fake.resolveThis = false; fake.reject = false; fake.returnValueDefined = true; fake.exception = undefined; @@ -182,6 +183,7 @@ module.exports = { } fake.returnValue = reason; fake.resolve = false; + fake.resolveThis = false; fake.reject = true; fake.returnValueDefined = true; fake.exception = undefined; @@ -191,6 +193,16 @@ module.exports = { return fake; }, + resolvesThis: function resolvesThis(fake) { + fake.returnValue = undefined; + fake.resolve = false; + fake.resolveThis = true; + fake.reject = false; + fake.returnValueDefined = false; + fake.exception = undefined; + fake.fakeFn = undefined; + }, + callThrough: function callThrough(fake) { fake.callsThrough = true; }, diff --git a/lib/sinon/stub.js b/lib/sinon/stub.js index 19310619a..a7c32ad39 100644 --- a/lib/sinon/stub.js +++ b/lib/sinon/stub.js @@ -120,6 +120,7 @@ var proto = { delete this.throwArgAt; delete this.fakeFn; this.returnThis = false; + this.resolveThis = false; fakes.forEach(function (fake) { fake.resetBehavior(); diff --git a/test/stub-test.js b/test/stub-test.js index 0c2aa60fb..f6ba2c856 100644 --- a/test/stub-test.js +++ b/test/stub-test.js @@ -269,6 +269,50 @@ describe("stub", function () { }); }); + describe(".resolvesThis", function () { + afterEach(function () { + if (Promise.resolve.restore) { + Promise.resolve.restore(); + } + }); + + it("returns a promise resolved with this", function () { + var instance = {}; + instance.stub = createStub.create(); + instance.stub.resolvesThis(); + + return instance.stub().then(function (actual) { + assert.same(actual, instance); + }); + }); + + it("returns a promise resolved with the context bound with stub#call", function () { + var stub = createStub.create(); + stub.resolvesThis(); + var object = {}; + + return stub.call(object).then(function (actual) { + assert.same(actual, object); + }); + }); + + it("returns a promise resolved with the context bound with stub#apply", function () { + var stub = createStub.create(); + stub.resolvesThis(); + var object = {}; + + return stub.apply(object).then(function (actual) { + assert.same(actual, object); + }); + }); + + it("returns the stub itself, allowing to chain function calls", function () { + var stub = createStub.create(); + + assert.same(stub.resolvesThis(), stub); + }); + }); + describe(".returnsArg", function () { it("returns argument at specified index", function () { var stub = createStub.create(); @@ -2225,6 +2269,16 @@ describe("stub", function () { refute.defined(instance.stub()); }); + it("cleans 'resolvesThis' behavior, so the stub does not resolve nor returns anything", function () { + var instance = {}; + instance.stub = createStub.create(); + instance.stub.resolvesThis(); + + instance.stub.resetBehavior(); + + refute.defined(instance.stub()); + }); + describe("does not touch properties that are reset by 'reset'", function () { it(".calledOnce", function () { var stub = createStub();