Skip to content

Commit

Permalink
Merge 5204f46 into 67b8c0c
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinfarris committed Jul 19, 2019
2 parents 67b8c0c + 5204f46 commit 63bbe69
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/sinon/behavior.js
Expand Up @@ -135,6 +135,7 @@ var proto = {
);
},

/*eslint complexity: ["error", 20]*/
invoke: function invoke(context, args) {
/*
* callCallback (conditionally) calls ensureArgs
Expand Down Expand Up @@ -171,7 +172,11 @@ var proto = {
return (this.promiseLibrary || Promise).reject(this.returnValue);
} else if (this.callsThrough) {
var wrappedMethod = this.effectiveWrappedMethod();

return wrappedMethod.apply(context, args);
} else if (this.callsThroughWithNew) {
var WrappedClass = this.effectiveWrappedMethod();
return new WrappedClass(args);
} else if (typeof this.returnValue !== "undefined") {
return this.returnValue;
} else if (typeof this.callArgAt === "number") {
Expand Down
4 changes: 4 additions & 0 deletions lib/sinon/default-behaviors.js
Expand Up @@ -221,6 +221,10 @@ var defaultBehaviors = {
fake.callsThrough = true;
},

callThroughWithNew: function callThroughWithNew(fake) {
fake.callsThroughWithNew = true;
},

get: function get(fake, getterFunction) {
var rootStub = fake.stub || fake;

Expand Down
67 changes: 67 additions & 0 deletions test/stub-test.js
Expand Up @@ -3136,6 +3136,73 @@ describe("stub", function() {
});
});

describe(".callThroughWithNew", function() {
it("does not call original function with new when arguments match conditional stub", function() {
// We need a function here because we can't wrap properties that are already stubs
var callCount = 0;
var OriginalClass = function SomeClass() {
this.foo = "bar";
callCount++;
};

var myObj = {
MyClass: OriginalClass
};

var propStub = createStub(myObj, "MyClass");
propStub.withArgs("foo").returns({ foo: "bar" });
propStub.callThroughWithNew(propStub);

var result = new myObj.MyClass("foo");

assert.equals(result.foo, "bar");
assert.equals(callCount, 0);
});

it("calls original function with new when arguments do not match conditional stub", function() {
var callCount = 0;
function OriginalClass() {
this.foo = "baz";
callCount++;
}

var myObj = {
MyClass: OriginalClass
};

var propStub = createStub(myObj, "MyClass");
propStub.withArgs("foo").returns({ foo: "bar" });
propStub.callThroughWithNew(propStub);

var result = new myObj.MyClass("not foo");
assert.equals(result.foo, "baz");
assert.equals(callCount, 1);
});

it("calls original function with new with same arguments when call does not match conditional stub", function() {
// We need a function here because we can't wrap properties that are already stubs
var callArgs = [];

function OriginalClass() {
callArgs = arguments;
this.foo = "baz";
}

var myObj = {
MyClass: OriginalClass
};

var propStub = createStub(myObj, "MyClass");
propStub.withArgs("foo").returns({ foo: "bar" });
propStub.callThroughWithNew(propStub);

var result = new myObj.MyClass("not foo");
assert.equals(callArgs[0].length, 1);
assert.equals(callArgs[0][0], "not foo");
assert.equals(result.foo, "baz");
});
});

describe(".get", function() {
it("allows users to stub getter functions for properties", function() {
var myObj = {
Expand Down

0 comments on commit 63bbe69

Please sign in to comment.