Skip to content
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

fixes issue #1368 by adding stub#resolvesThis #1517

Merged
merged 3 commits into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 11 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,15 @@ module.exports = {
return fake;
},

resolvesThis: function resolvesThis(fake) {
fake.resolveThis = true;
fake.resolve = false;
fake.reject = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we should keep this consistent with other behaviors, such as rejects, which also sets many other flags, as you can see here. Even though this is enough to make it work, I think that by keeping the whole API consistent when it comes to flagging fakes is extremely important.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you created the wrong link? It just points back to your comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I can set returnValueDefined and exception flags to false to keep consistency. Do you think some other flags need to be turned off?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fatso83 I think that link only works inside the files tab. When I click it inside of it the link works.

Basically I think the following flags should be set:

  • fake.returnValue
  • fake.returnValueDefined
  • fake.exception

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay now resolves, rejects and resolvesThis all act on the same flags.
I set returnValue to undefined as the stub resolves with itself.
Is it okay by you?

fake.returnValueDefined = false;
fake.exception = undefined;
fake.fakeFn = undefined;
},

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
54 changes: 54 additions & 0 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,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();
Expand Down Expand Up @@ -2171,6 +2215,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();
Expand Down