Leaflet/spec/suites/core/ClassSpec.js

147 lines
3.2 KiB
JavaScript
Raw Normal View History

2010-09-02 21:14:25 +08:00
describe("Class", function() {
2010-09-02 21:14:25 +08:00
describe("#extend", function() {
2010-09-02 22:26:54 +08:00
var Klass,
constructor,
method;
2010-09-02 22:26:54 +08:00
beforeEach(function() {
2011-07-12 20:32:40 +08:00
constructor = jasmine.createSpy("Klass constructor");
method = jasmine.createSpy("Klass#bar method");
2010-09-02 22:26:54 +08:00
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
});
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();
2010-09-02 21:14:25 +08:00
expect(constructor).toHaveBeenCalled();
expect(a.foo).toEqual(5);
2010-09-02 21:14:25 +08:00
a.bar();
2010-09-02 21:14:25 +08:00
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});
2010-09-02 22:26:54 +08:00
var b = new Klass2();
2010-09-02 22:26:54 +08:00
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);
2010-09-02 22:26:54 +08:00
b.bar();
2010-09-02 22:26:54 +08:00
expect(method).toHaveBeenCalled();
});
2010-09-02 22:26:54 +08:00
it("should support static properties", function() {
expect(Klass.bla).toEqual(1);
});
2010-09-02 22:26:54 +08:00
it("should inherit parent static properties", function() {
var Klass2 = Klass.extend({});
2010-09-02 22:26:54 +08:00
expect(Klass2.bla).toEqual(1);
});
2011-06-21 17:09:49 +08:00
it("should override parent static properties", function() {
var Klass2 = Klass.extend({statics: {bla: 2}});
2011-06-21 17:09:49 +08:00
expect(Klass2.bla).toEqual(2);
});
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();
2010-09-04 00:27:26 +08:00
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});
2010-09-02 22:26:54 +08:00
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
}
});
2010-09-06 23:26:20 +08:00
var a = new KlassWithOptions2();
2010-09-06 23:26:20 +08:00
expect(a.options).toEqual({
foo1: 1,
foo2: 3,
foo3: 4
});
});
it("should add constructor hooks correctly", function () {
var spy1 = jasmine.createSpy("init hook 1");
Klass.addInitHook(spy1);
Klass.addInitHook('bar', 1, 2, 3);
var a = new Klass();
expect(spy1).toHaveBeenCalled();
expect(method).toHaveBeenCalledWith(1, 2, 3);
});
it("should inherit constructor hooks", function () {
var spy1 = jasmine.createSpy("init hook 1"),
spy2 = jasmine.createSpy("init hook 2");
Klass.addInitHook(spy1);
var Klass2 = Klass.extend({});
Klass2.addInitHook(spy2);
var a = new Klass();
expect(spy1).toHaveBeenCalled();
expect(spy2).not.toHaveBeenCalled();
var b = new Klass2();
expect(spy2).toHaveBeenCalled();
expect(spy1.argsForCall.length).toBe(2);
});
2010-09-02 21:14:25 +08:00
});
2012-07-05 17:54:40 +08:00
// TODO Class.include
// TODO Class.mergeOptions
});