Skip to content

Commit

Permalink
feat(constructors): stub and spy constructors pass "instanceof" tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jan 2, 2017
1 parent 0d8d1b0 commit 745daad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ function getArrayFromArrayLikeObject (args) {
}

var isMethod = R.curry(function (object, propName) {
return !Object.getOwnPropertyDescriptor(object, propName).get && typeof object[ propName ] === 'function'
return !Object.getOwnPropertyDescriptor(object, propName).get &&
typeof object[ propName ] === 'function' && !(propName === 'constructor')
})

var applyToEachFunctionKeyInObject = function (appliedFunction, object) {
Expand Down Expand Up @@ -63,6 +64,8 @@ module.exports = function getStubOrSpyConstructor (getConstructorProperties) {
return instance
}

StubOrSpyConstructor.prototype = constructorProps.SourceConstructor.prototype

function configureMethods (methods) {
methodParams = methods
return this
Expand Down
10 changes: 10 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ describe('getSpyConstructor', function () {
expect(spiedObject.proto2()).to.equal('p2')
})

it('should create instances of the original constructor', function () {
var spiedObject = new SpyConstructor()
expect(spiedObject).to.be.an.instanceof(TestConstructor)
})

it('should put spies on all methods', function () {
var spiedObject = new SpyConstructor()

Expand Down Expand Up @@ -193,6 +198,11 @@ describe('getSpy- and getStubConstructor', function () {
NewConstructor = testData.getConstructor(TestConstructor)
})

it('should create instances of itself', function () {
var instance = new NewConstructor()
expect(instance).to.be.an.instanceof(NewConstructor)
})

describe('afterCreation', function () {
it('should allow for manual post-processing before an instance is created', function () {
NewConstructor.afterCreation(function (instance) {
Expand Down

0 comments on commit 745daad

Please sign in to comment.