-
-
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.
internal: extract tests into own module
- Loading branch information
Showing
1 changed file
with
158 additions
and
0 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,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!" } | ||
); | ||
}); | ||
}) |