Skip to content

Commit

Permalink
findCall argument
Browse files Browse the repository at this point in the history
allows searching for a call by its arguments
  • Loading branch information
nathanmacinnes committed Jul 22, 2016
1 parent 4583621 commit 4e21532
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/pretendr.js
Expand Up @@ -165,6 +165,16 @@ function pretendrFunction(fn) {
return instance.mock;
});
return template;
},
findCall : function (param) {
return p.calls.find(function (call) {
if (typeof param === "number") {
return call.args.length === param;
}
return param.every(function (expectedArg, index) {
return expectedArg === call.args[index] || expectedArg === null;
});
});
}
};
p.Mock = p.mock;
Expand Down
20 changes: 20 additions & 0 deletions test/spec.js
Expand Up @@ -208,6 +208,26 @@ describe("pretendr", function () {
expect(p.calls[0]).to.have.property("asConstructor", true);
});
});
describe("findCall method", function () {
it("finds the call with the matching number of arguments", function () {
p.mock(1, "a", true);
p.mock(1, "a");
p.mock();
p.mock(1);
expect(p.findCall(2)).to.equal(p.calls[1]);
expect(p.findCall(0)).to.equal(p.calls[2]);
});
it("finds the call with matching arguments", function () {
p.mock(1, "a", true);
p.mock(2, "b", false);
p.mock(2, "b", true);
expect(p.findCall([2, "b", false])).to.equal(p.calls[1]);
});
it("treats null as any value", function () {
p.mock(3, 4, 5);
expect(p.findCall([3, null, 5])).to.equal(p.calls[0]);
});
});
});
describe("with an object", function () {
var descriptor,
Expand Down

0 comments on commit 4e21532

Please sign in to comment.