|
define(function(){ |
|
|
|
/** |
|
* Emulating how Object.create works. |
|
* Is the implementation below equivalent with Object.create? |
|
* just a test |
|
*/ |
|
|
|
// Constructor emulating inheritance |
|
var Constructor = function (parameterMap, parentObject) { |
|
for(var key in parameterMap) { |
|
if(parameterMap.hasOwnProperty(key)) { |
|
this[key] = parameterMap[key]; |
|
} |
|
} |
|
if( typeof parentObject !== 'undefined' ) { |
|
this.prototype = parentObject; |
|
for(var fn in parentObject) { |
|
if(!this.hasOwnProperty(fn)) { |
|
this[fn] = parentObject[fn]; |
|
} |
|
} |
|
} |
|
} |
|
|
|
var testParent = new Constructor( { |
|
'name' : 'parent', |
|
'parentProperty' : 'test', |
|
'parentFn' : function () { console.log('parent fn'); } |
|
}); |
|
|
|
describe('The factory (the Constructor)', function() { |
|
it('can create an object', function() { |
|
expect(testParent).toBeDefined(); |
|
}); |
|
it('can set up the properties', function() { |
|
expect(testParent.name).toEqual('parent'); |
|
expect(testParent.parentProperty).toEqual('test'); |
|
expect(testParent.parentFn).toBeDefined(); |
|
}); |
|
it('can create an object without a parent object', function() { |
|
expect(testParent.prototype).toBeUndefined(); |
|
}); |
|
}); |
|
|
|
|
|
describe('An instance of a parent created by the constructor', function() { |
|
|
|
var testInstance = new Constructor({ 'name' : 'child1'}, testParent); |
|
|
|
it('has a prototype', function() { |
|
expect(testInstance.prototype).toBeDefined(); |
|
}); |
|
|
|
it('has the prototype same as the parent', function() { |
|
expect(testInstance.prototype).toEqual(testParent); |
|
}); |
|
|
|
it('can have its own properties', function() { |
|
expect(testInstance.name).toBeDefined(); |
|
}); |
|
|
|
it('has the parents functions', function() { |
|
expect(testInstance.parentFn).toBeDefined(); |
|
}); |
|
|
|
it('has the parents properties', function() { |
|
expect(testInstance.parentProperty).toBeDefined(); |
|
}); |
|
|
|
}); |
|
|
|
describe('An instance of a parent created by Object.create', function() { |
|
var testInstance = Object.create(testParent); |
|
|
|
it('has the parents properties', function() { |
|
expect(testInstance.parentProperty).toBeDefined(); |
|
}); |
|
|
|
it('has the parents functions', function() { |
|
expect(testInstance.parentFn).toBeDefined(); |
|
}); |
|
}); |
|
}) |