Skip to content

Commit

Permalink
fixes issue sinonjs#1368 by adding stub#resolvesThis
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoMuller committed Aug 7, 2017
1 parent 112af55 commit 62a2a80
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/sinon/behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ var proto = {
this.exception ||
typeof this.returnArgAt === "number" ||
this.returnThis ||
this.resolveThis ||
typeof this.throwArgAt === "number" ||
this.fakeFn ||
this.returnValueDefined);
Expand All @@ -142,6 +143,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) {
Expand Down
8 changes: 8 additions & 0 deletions lib/sinon/default-behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,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;
Expand All @@ -173,6 +174,7 @@ module.exports = {
}
fake.returnValue = reason;
fake.resolve = false;
fake.resolveThis = false;
fake.reject = true;
fake.returnValueDefined = true;
fake.exception = undefined;
Expand All @@ -181,6 +183,12 @@ module.exports = {
return fake;
},

resolvesThis: function resolvesThis(fake) {
fake.resolveThis = true;
fake.resolve = false;
fake.reject = false;
},

callThrough: function callThrough(fake) {
fake.callsThrough = true;
},
Expand Down
1 change: 1 addition & 0 deletions lib/sinon/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ var proto = {
delete this.throwArgAt;
delete this.fakeFn;
this.returnThis = false;
this.resolveThis = false;

fakes.forEach(function (fake) {
fake.resetBehavior();
Expand Down
69 changes: 69 additions & 0 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,65 @@ describe("stub", function () {
});
});

describe(".resolvesThis", function () {
afterEach(function () {
if (Promise.resolve.restore) {
Promise.resolve.restore();
}
});

it("returns a promise resolves with this", function () {
var instance = {};
instance.stub = createStub.create();
instance.stub.resolvesThis();

return instance.stub().then(function (actual) {
assert.same(actual, instance);
});
});

var strictMode = (function () {
return this;
}()) === undefined;
if (strictMode) {
it("returns a promise resolves with undefined when detached", function () {
var stub = createStub.create();
stub.resolvesThis();

// Due to strict mode, would be `global` otherwise
return stub().then(function (actual) {
assert.same(actual, undefined);
});
});
}

it("stub respects call", function () {
var stub = createStub.create();
stub.resolvesThis();
var object = {};

return stub.call(object).then(function (actual) {
assert.same(actual, object);
});
});

it("stub respects apply", function () {
var stub = createStub.create();
stub.resolvesThis();
var object = {};

return stub.apply(object).then(function (actual) {
assert.same(actual, object);
});
});

it("returns stub", function () {
var stub = createStub.create();

assert.same(stub.resolvesThis(), stub);
});
});

describe(".returnsArg", function () {
it("returns argument at specified index", function () {
var stub = createStub.create();
Expand Down Expand Up @@ -2171,6 +2230,16 @@ describe("stub", function () {
refute.defined(instance.stub());
});

it("cleans 'resolvesThis' behavior", 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();
Expand Down

0 comments on commit 62a2a80

Please sign in to comment.