-
-
Notifications
You must be signed in to change notification settings - Fork 768
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -165,6 +165,21 @@ module.exports = { | |
fake.fakeFn = undefined; | ||
}, | ||
|
||
resolvesArg: function resolvesArg(fake, index) { | ||
if (typeof index !== "number") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a test for this. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add some meaningful assertion to this test. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
There was a problem hiding this comment.
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 toundefined
, but I think it shouldthrow
if there are not enough arguments. Having it resolve to an nonexistent argument doesn't seem like what people expect when writing tests.There was a problem hiding this comment.
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.