-
-
Notifications
You must be signed in to change notification settings - Fork 771
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make it possible to call through to underlying stub in stub inst…
…ance (#2503) * fix: make it possible to call through to underlying stub in stub instances refs #2477 refs #2501 * internal: Extract underlying createStubInstance * internal: extract tests into own module * internal: extract sinon type checking into own module closes #2501
- Loading branch information
Showing
8 changed files
with
246 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"use strict"; | ||
|
||
const stub = require("./stub"); | ||
const sinonType = require("./util/core/sinon-type"); | ||
const forEach = require("@sinonjs/commons").prototypes.array.forEach; | ||
|
||
function isStub(value) { | ||
return sinonType.get(value) === "stub"; | ||
} | ||
|
||
module.exports = function createStubInstance(constructor, overrides) { | ||
if (typeof constructor !== "function") { | ||
throw new TypeError("The constructor should be a function."); | ||
} | ||
|
||
const stubInstance = Object.create(constructor.prototype); | ||
sinonType.set(stubInstance, "stub-instance"); | ||
|
||
const stubbedObject = stub(stubInstance); | ||
|
||
forEach(Object.keys(overrides || {}), function (propertyName) { | ||
if (propertyName in stubbedObject) { | ||
var value = overrides[propertyName]; | ||
if (isStub(value)) { | ||
stubbedObject[propertyName] = value; | ||
} else { | ||
stubbedObject[propertyName].returns(value); | ||
} | ||
} else { | ||
throw new Error( | ||
`Cannot stub ${propertyName}. Property does not exist!` | ||
); | ||
} | ||
}); | ||
return stubbedObject; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"use strict"; | ||
|
||
const sinonTypeSymbolProperty = Symbol("SinonType"); | ||
|
||
module.exports = { | ||
/** | ||
* Set the type of a Sinon object to make it possible to identify it later at runtime | ||
* | ||
* @param {object|Function} object object/function to set the type on | ||
* @param {string} type the named type of the object/function | ||
*/ | ||
set(object, type) { | ||
Object.defineProperty(object, sinonTypeSymbolProperty, { | ||
value: type, | ||
configurable: false, | ||
enumerable: false, | ||
}); | ||
}, | ||
get(object) { | ||
return object && object[sinonTypeSymbolProperty]; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
"use strict"; | ||
|
||
var referee = require("@sinonjs/referee"); | ||
var createStub = require("../lib/sinon/stub"); | ||
var createStubInstance = require("../lib/sinon/create-stub-instance"); | ||
var assert = referee.assert; | ||
var refute = referee.refute; | ||
|
||
describe("createStubInstance", function () { | ||
it("stubs existing methods", function () { | ||
var Class = function () { | ||
return; | ||
}; | ||
Class.prototype.method = function () { | ||
return; | ||
}; | ||
|
||
var stub = createStubInstance(Class); | ||
stub.method.returns(3); | ||
assert.equals(3, stub.method()); | ||
}); | ||
|
||
it("throws with no methods to stub", function () { | ||
var Class = function () { | ||
return; | ||
}; | ||
|
||
assert.exception( | ||
function () { | ||
createStubInstance(Class); | ||
}, | ||
{ | ||
message: | ||
"Found no methods on object to which we could apply mutations", | ||
} | ||
); | ||
}); | ||
|
||
it("doesn't call the constructor", function () { | ||
var Class = function (a, b) { | ||
var c = a + b; | ||
throw c; | ||
}; | ||
Class.prototype.method = function () { | ||
return; | ||
}; | ||
|
||
var stub = createStubInstance(Class); | ||
refute.exception(function () { | ||
stub.method(3); | ||
}); | ||
}); | ||
|
||
it("retains non function values", function () { | ||
var TYPE = "some-value"; | ||
var Class = function () { | ||
return; | ||
}; | ||
Class.prototype.method = function () { | ||
return; | ||
}; | ||
Class.prototype.type = TYPE; | ||
|
||
var stub = createStubInstance(Class); | ||
assert.equals(TYPE, stub.type); | ||
}); | ||
|
||
it("has no side effects on the prototype", function () { | ||
var proto = { | ||
method: function () { | ||
throw new Error("error"); | ||
}, | ||
}; | ||
var Class = function () { | ||
return; | ||
}; | ||
Class.prototype = proto; | ||
|
||
var stub = createStubInstance(Class); | ||
refute.exception(stub.method); | ||
assert.exception(proto.method); | ||
}); | ||
|
||
it("throws exception for non function params", function () { | ||
var types = [{}, 3, "hi!"]; | ||
|
||
for (var i = 0; i < types.length; i++) { | ||
// yes, it's silly to create functions in a loop, it's also a test | ||
// eslint-disable-next-line no-loop-func | ||
assert.exception(function () { | ||
createStubInstance(types[i]); | ||
}); | ||
} | ||
}); | ||
|
||
it("allows providing optional overrides", function () { | ||
var Class = function () { | ||
return; | ||
}; | ||
Class.prototype.method = function () { | ||
return; | ||
}; | ||
|
||
var stub = createStubInstance(Class, { | ||
method: createStub().returns(3), | ||
}); | ||
|
||
assert.equals(3, stub.method()); | ||
}); | ||
|
||
it("allows providing optional returned values", function () { | ||
var Class = function () { | ||
return; | ||
}; | ||
Class.prototype.method = function () { | ||
return; | ||
}; | ||
|
||
var stub = createStubInstance(Class, { | ||
method: 3, | ||
}); | ||
|
||
assert.equals(3, stub.method()); | ||
}); | ||
|
||
it("allows providing null as a return value", function () { | ||
var Class = function () { | ||
return; | ||
}; | ||
Class.prototype.method = function () { | ||
return; | ||
}; | ||
|
||
var stub = createStubInstance(Class, { | ||
method: null, | ||
}); | ||
|
||
assert.equals(null, stub.method()); | ||
}); | ||
|
||
it("throws an exception when trying to override non-existing property", function () { | ||
var Class = function () { | ||
return; | ||
}; | ||
Class.prototype.method = function () { | ||
return; | ||
}; | ||
|
||
assert.exception( | ||
function () { | ||
createStubInstance(Class, { | ||
foo: createStub().returns(3), | ||
}); | ||
}, | ||
{ message: "Cannot stub foo. Property does not exist!" } | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.