Skip to content

Commit

Permalink
Merge a2002b6 into c8901e7
Browse files Browse the repository at this point in the history
  • Loading branch information
huttli committed Jun 28, 2018
2 parents c8901e7 + a2002b6 commit 5d3eac5
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/release-source/release/stubs.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,16 @@ The Promise library can be overwritten using the `usingPromise` method.

*Since `sinon@2.0.0`*

#### `stub.resolvesArg(index);`

Causes the stub to return a Promise which resolves to the argument at the
provided index.

`stub.resolvesArg(0);` causes the stub to return a Promise which resolves to the
first argument.

*Since `sinon@6.1.0`*

#### `stub.throws();`

Causes the stub to throw an exception (`Error`).
Expand Down
11 changes: 11 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,16 @@ var proto = {
throw args[this.throwArgAt];
} else if (this.fakeFn) {
return this.fakeFn.apply(context, args);
} else if (typeof this.resolveArgAt === "number") {
var len = this.resolveArgAt + 1;
if (args.length < len) {
throw new TypeError(
"resolvesArg failed: " + len
+ " arguments required but only " + args.length
+ " present"
);
}
return (this.promiseLibrary || Promise).resolve(args[this.resolveArgAt]);
} 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") {
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
80 changes: 80 additions & 0 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,86 @@ 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("zero", "one");
});
});

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

return stub("zero", "one").then(function (actual) {
assert.same(actual, "one");
});
});

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(resolveSpy.notCalled);
});

it("throws if index is not a number", function () {
var stub = createStub.create();

assert.exception(function () {
stub.resolvesArg();
}, {name: "TypeError"});
});

it("throws without enough arguments", function () {
var stub = createStub.create();
stub.resolvesArg(3);

assert.exception(function () {
stub("zero", "one", "two");
}, {
name: "TypeError",
message: "resolvesArg failed: 4 arguments required but only 3 present"
});
});
});

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

0 comments on commit 5d3eac5

Please sign in to comment.