| 1 |
'use strict'; |
| 2 |
|
| 3 |
require('mocha'); |
| 4 |
var App = require('./fixtures/app'); |
| 5 |
var assert = require('assert'); |
| 6 |
var extend = require('./'); |
| 7 |
|
| 8 |
describe('extend', function() { |
| 9 |
var Parent; |
| 10 |
var Ctor; |
| 11 |
var proto; |
| 12 |
|
| 13 |
beforeEach(function() { |
| 14 |
Parent = function() {} |
| 15 |
Parent.foo = 'bar'; |
| 16 |
Parent.prototype.a = function() {}; |
| 17 |
Parent.prototype.b = function() {}; |
| 18 |
Parent.prototype.c = function() {}; |
| 19 |
Object.defineProperty(Parent.prototype, 'count', { |
| 20 |
get: function() { |
| 21 |
return Object.keys(this).length; |
| 22 |
}, |
| 23 |
set: function() {} |
| 24 |
}); |
| 25 |
Ctor = function() { |
| 26 |
Parent.call(this); |
| 27 |
}; |
| 28 |
proto = App.prototype; |
| 29 |
}); |
| 30 |
|
| 31 |
it('should add `Parent.prototype` to `Ctor` instances as `_parent_`', function() { |
| 32 |
var ext = extend(Parent); |
| 33 |
var instance1 = new Ctor(); |
| 34 |
assert.equal(typeof instance1._parent_, 'undefined'); |
| 35 |
ext(Ctor); |
| 36 |
var instance2 = new Ctor(); |
| 37 |
assert.equal(typeof instance2._parent_, 'object'); |
| 38 |
assert.deepEqual(instance2._parent_, Parent.prototype); |
| 39 |
}); |
| 40 |
|
| 41 |
it('should access `Parent` methods through `_parent_`', function() { |
| 42 |
Parent.prototype.upper = function(str) { |
| 43 |
return str.toUpperCase(); |
| 44 |
}; |
| 45 |
|
| 46 |
var ext = extend(Parent); |
| 47 |
ext(Ctor); |
| 48 |
|
| 49 |
var instance = new Ctor(); |
| 50 |
assert.equal(instance.upper('foo'), 'FOO'); |
| 51 |
|
| 52 |
instance.upper = function(str) { |
| 53 |
return str; |
| 54 |
}; |
| 55 |
assert.equal(instance.upper('foo'), 'foo'); |
| 56 |
|
| 57 |
instance.upper = function(str) { |
| 58 |
return this._parent_.upper(str) + ' ' + str; |
| 59 |
}; |
| 60 |
assert.equal(instance.upper('foo'), 'FOO foo'); |
| 61 |
}); |
| 62 |
|
| 63 |
it('should add static methods to Ctor:', function() { |
| 64 |
var ext = extend(Parent); |
| 65 |
ext(Ctor); |
| 66 |
assert(typeof Ctor.extend === 'function'); |
| 67 |
assert(Ctor.foo === 'bar'); |
| 68 |
}); |
| 69 |
|
| 70 |
it('should add an extend method to Parent to add static methods to Ctor:', function() { |
| 71 |
Parent.extend = extend(Parent); |
| 72 |
Parent.extend(Ctor); |
| 73 |
assert(typeof Ctor.extend === 'function'); |
| 74 |
assert(Ctor.foo === 'bar'); |
| 75 |
}); |
| 76 |
|
| 77 |
it('should add prototype methods to Ctor:', function() { |
| 78 |
var ext = extend(Parent); |
| 79 |
ext(Ctor); |
| 80 |
assert(typeof Ctor.prototype.a === 'function'); |
| 81 |
assert(typeof Ctor.prototype.b === 'function'); |
| 82 |
assert(typeof Ctor.prototype.c === 'function'); |
| 83 |
}); |
| 84 |
|
| 85 |
it('should add descriptors to Ctor:', function() { |
| 86 |
var ext = extend(Parent); |
| 87 |
ext(Ctor); |
| 88 |
}); |
| 89 |
|
| 90 |
it('should copy prototype properties to Ctor:', function() { |
| 91 |
var ext = extend(Parent); |
| 92 |
ext(Ctor, App.prototype); |
| 93 |
assert(typeof Ctor.prototype.get === 'function'); |
| 94 |
assert(typeof Ctor.prototype.set === 'function'); |
| 95 |
assert(typeof Ctor.prototype.del === 'function'); |
| 96 |
}); |
| 97 |
|
| 98 |
it('should add a mixin method to the prototype of Ctor using `extend` function:', function() { |
| 99 |
var ext = extend(Parent, function(Child) { |
| 100 |
Child.prototype.mixin = function(key, val) { |
| 101 |
Child.prototype[key] = val; |
| 102 |
}; |
| 103 |
}); |
| 104 |
ext(Ctor, App.prototype); |
| 105 |
assert(typeof Ctor.prototype.mixin === 'function'); |
| 106 |
assert(typeof Ctor.prototype.get === 'function'); |
| 107 |
assert(typeof Ctor.prototype.set === 'function'); |
| 108 |
assert(typeof Ctor.prototype.del === 'function'); |
| 109 |
}); |
| 110 |
|
| 111 |
it('should mixin methods to the Ctor.prototype using `extend` function:', function() { |
| 112 |
var ext = extend(Parent, function(Child) { |
| 113 |
Child.prototype.mixin = function(key, val) { |
| 114 |
Child.prototype[key] = val; |
| 115 |
}; |
| 116 |
}); |
| 117 |
ext(Ctor, App.prototype); |
| 118 |
var app = new Ctor(); |
| 119 |
app.mixin('foo', function() {}); |
| 120 |
assert.equal(typeof Ctor.prototype.foo, 'function'); |
| 121 |
}); |
| 122 |
|
| 123 |
it('should throw an error when Parent is not a function:', function(cb) { |
| 124 |
try { |
| 125 |
extend('foo'); |
| 126 |
cb(new Error('expected an error')); |
| 127 |
cb(); |
| 128 |
} catch (err) { |
| 129 |
assert.equal(err.message, 'expected Parent to be a function.'); |
| 130 |
cb(); |
| 131 |
} |
| 132 |
}); |
| 133 |
|
| 134 |
it('should throw an error when Ctor is not a function:', function(cb) { |
| 135 |
try { |
| 136 |
extend(function Foo() {})('bar') |
| 137 |
cb(new Error('expected an error')); |
| 138 |
cb(); |
| 139 |
} catch (err) { |
| 140 |
assert.equal(err.message, 'expected Ctor to be a function.'); |
| 141 |
cb(); |
| 142 |
} |
| 143 |
}); |
| 144 |
}); |
| 145 |
|