2010-09-02 21:14:25 +08:00
|
|
|
describe("Class", function() {
|
|
|
|
|
|
|
|
describe("#extend", function() {
|
2010-09-02 22:26:54 +08:00
|
|
|
var Klass,
|
|
|
|
constructor,
|
|
|
|
method;
|
2010-09-02 21:14:25 +08:00
|
|
|
|
2010-09-02 22:26:54 +08:00
|
|
|
beforeEach(function() {
|
|
|
|
constructor = jasmine.createSpy(),
|
|
|
|
method = jasmine.createSpy();
|
|
|
|
|
2010-09-02 21:14:25 +08:00
|
|
|
Klass = L.Class.extend({
|
2010-09-02 22:26:54 +08:00
|
|
|
statics: {bla: 1},
|
|
|
|
includes: {mixin: true},
|
|
|
|
|
2010-09-02 21:14:25 +08:00
|
|
|
initialize: constructor,
|
|
|
|
foo: 5,
|
|
|
|
bar: method
|
|
|
|
});
|
2010-09-02 22:26:54 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should create a class with the given constructor & properties", function() {
|
2010-09-02 21:14:25 +08:00
|
|
|
var a = new Klass();
|
|
|
|
|
|
|
|
expect(constructor).toHaveBeenCalled();
|
|
|
|
expect(a.foo).toEqual(5);
|
|
|
|
|
|
|
|
a.bar();
|
|
|
|
|
|
|
|
expect(method).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2010-09-02 22:26:54 +08:00
|
|
|
it("should inherit parent classes' constructor & properties", function() {
|
|
|
|
var Klass2 = Klass.extend({baz: 2});
|
|
|
|
|
|
|
|
var b = new Klass2();
|
|
|
|
|
|
|
|
expect(b instanceof Klass).toBeTruthy();
|
2010-09-06 23:38:14 +08:00
|
|
|
expect(b instanceof Klass2).toBeTruthy();
|
2010-09-02 22:26:54 +08:00
|
|
|
|
|
|
|
expect(constructor).toHaveBeenCalled();
|
|
|
|
expect(b.baz).toEqual(2);
|
|
|
|
|
|
|
|
b.bar();
|
|
|
|
|
|
|
|
expect(method).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should grant the ability to call parent methods, including constructor", function() {
|
|
|
|
var Klass2 = Klass.extend({
|
|
|
|
initialize: function() {},
|
|
|
|
bar: function() {}
|
|
|
|
});
|
|
|
|
|
|
|
|
var b = new Klass2();
|
|
|
|
|
|
|
|
expect(constructor).not.toHaveBeenCalled();
|
2010-09-06 23:26:20 +08:00
|
|
|
b.superclass.initialize.call(this);
|
2010-09-02 22:26:54 +08:00
|
|
|
expect(constructor).toHaveBeenCalled();
|
|
|
|
|
2010-09-06 23:26:20 +08:00
|
|
|
b.superclass.bar.call(this);
|
2010-09-02 22:26:54 +08:00
|
|
|
expect(method).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should support static properties", function() {
|
|
|
|
expect(Klass.bla).toEqual(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should inherit parent static properties", function() {
|
|
|
|
var Klass2 = Klass.extend({});
|
|
|
|
|
|
|
|
expect(Klass2.bla).toEqual(1);
|
|
|
|
});
|
|
|
|
|
2010-09-04 00:27:26 +08:00
|
|
|
it("should include the given mixin", function() {
|
2010-09-02 22:26:54 +08:00
|
|
|
var a = new Klass();
|
|
|
|
expect(a.mixin).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2010-09-04 00:27:26 +08:00
|
|
|
it("should be able to include multiple mixins", function() {
|
|
|
|
var Klass2 = L.Class.extend({
|
|
|
|
includes: [{mixin: true}, {mixin2: true}]
|
|
|
|
});
|
|
|
|
var a = new Klass2();
|
|
|
|
|
|
|
|
expect(a.mixin).toBeTruthy();
|
|
|
|
expect(a.mixin2).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2010-09-02 22:26:54 +08:00
|
|
|
it("should grant the ability to include the given mixin", function() {
|
|
|
|
Klass.include({mixin2: true});
|
|
|
|
|
|
|
|
var a = new Klass();
|
|
|
|
expect(a.mixin2).toBeTruthy();
|
|
|
|
});
|
2010-09-06 23:26:20 +08:00
|
|
|
|
|
|
|
it("should merge options instead of replacing them", function() {
|
|
|
|
var KlassWithOptions1 = L.Class.extend({
|
|
|
|
options: {
|
|
|
|
foo1: 1,
|
|
|
|
foo2: 2
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var KlassWithOptions2 = KlassWithOptions1.extend({
|
|
|
|
options: {
|
|
|
|
foo2: 3,
|
|
|
|
foo3: 4
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var a = new KlassWithOptions2();
|
|
|
|
|
|
|
|
expect(a.options).toEqual({
|
|
|
|
foo1: 1,
|
|
|
|
foo2: 3,
|
|
|
|
foo3: 4
|
|
|
|
});
|
|
|
|
});
|
2010-09-02 21:14:25 +08:00
|
|
|
});
|
|
|
|
});
|