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

syntax sugar for resolvesArg issue-1665 #1846

Merged
merged 5 commits into from
Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 @@ -107,6 +107,7 @@ var proto = {
this.exceptionCreator ||
typeof this.returnArgAt === "number" ||
this.returnThis ||
typeof this.resolveArgAt === "number" ||
this.resolveThis ||
typeof this.throwArgAt === "number" ||
this.fakeFn ||
Expand Down Expand Up @@ -137,6 +138,8 @@ var proto = {
throw args[this.throwArgAt];
} else if (this.fakeFn) {
return this.fakeFn.apply(context, args);
} else if (typeof this.resolveArgAt === "number") {
return (this.promiseLibrary || Promise).resolve(args[this.resolveArgAt]);
Copy link
Member

Choose a reason for hiding this comment

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

Will you make this more strict (like throwsArg)? By default this will resolve to undefined, but I think it should throw if there are not enough arguments. Having it resolve to an nonexistent argument doesn't seem like what people expect when writing tests.

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 did the old check just like throwArgAt in this pull request.
I'm happy to use that ensureArgs if PR #1848 get merged first and uses the correct argument index number.

} else if (this.resolveThis) {
return (this.promiseLibrary || Promise).resolve(context);
} else if (this.resolve) {
Expand Down
15 changes: 15 additions & 0 deletions lib/sinon/default-behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ module.exports = {
fake.fakeFn = undefined;
},

resolvesArg: function resolvesArg(fake, index) {
if (typeof index !== "number") {
Copy link
Member

Choose a reason for hiding this comment

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

Add a test for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

throw new TypeError("argument index is not number");
}
fake.resolveArgAt = index;
fake.returnValue = undefined;
fake.resolve = true;
fake.resolveThis = false;
fake.reject = false;
fake.returnValueDefined = false;
fake.exception = undefined;
fake.exceptionCreator = undefined;
fake.fakeFn = undefined;
},

rejects: function rejects(fake, error, message) {
var reason;
if (typeof error === "string") {
Expand Down
1 change: 1 addition & 0 deletions lib/sinon/stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ var proto = {
delete this.returnValue;
delete this.returnArgAt;
delete this.throwArgAt;
delete this.resolveArgAt;
delete this.fakeFn;
this.returnThis = false;
this.resolveThis = false;
Expand Down
58 changes: 58 additions & 0 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,64 @@ describe("stub", function () {
});
});

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

it("returns a promise to the argument at specified index", function () {
var stub = createStub.create();
var object = {};
stub.resolvesArg(0);

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

it("returns a promise to the argument at another specified index", function () {
var stub = createStub.create();
var object = {};
stub.resolvesArg(2);

return stub("ignored", "ignored again", object).then(function (actual) {
assert.same(actual, object);
});
});

it("should return the same stub", function () {
var stub = createStub.create();

assert.same(stub.resolvesArg(1), stub);
});

it("supersedes previous throws", function () {
var stub = createStub.create();
stub.throws().resolvesArg(1);

refute.exception(function () {
stub();
});
});

it("supersedes previous rejects", function () {
var stub = createStub.create();
stub.rejects(Error("should be superseeded")).resolvesArg(1);

return stub().then();
Copy link
Member

Choose a reason for hiding this comment

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

Add some meaningful assertion to this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

});

it("does not invoke Promise.resolve when the behavior is added to the stub", function () {
var resolveSpy = createSpy(Promise, "resolve");
var stub = createStub.create();
stub.resolvesArg(2);

assert.equals(resolveSpy.callCount, 0);
Copy link
Member

Choose a reason for hiding this comment

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

assert(resolveSpy.notCalled)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

});
});

describe(".returnsArg", function () {
it("returns argument at specified index", function () {
var stub = createStub.create();
Expand Down