Leaflet/spec/suites/core/ClassSpec.js

201 lines
4.3 KiB
JavaScript
Raw Permalink Normal View History

describe("Class", function () {
describe("#extend", function () {
2010-09-02 22:26:54 +08:00
var Klass,
2015-09-25 18:55:37 +08:00
constructor,
method;
beforeEach(function () {
2013-03-02 05:49:20 +08:00
constructor = sinon.spy();
method = sinon.spy();
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
});
it("creates a class with the given constructor & properties", function () {
2010-09-02 21:14:25 +08:00
var a = new Klass();
2013-03-02 05:49:20 +08:00
expect(constructor.called).to.be.ok();
expect(a.foo).to.eql(5);
2010-09-02 21:14:25 +08:00
a.bar();
2013-03-02 05:49:20 +08:00
expect(method.called).to.be.ok();
2010-09-02 21:14:25 +08:00
});
it("inherits parent classes' constructor & properties", function () {
2010-09-02 22:26:54 +08:00
var Klass2 = Klass.extend({baz: 2});
2010-09-02 22:26:54 +08:00
var b = new Klass2();
2013-03-02 05:49:20 +08:00
expect(b instanceof Klass).to.be.ok();
expect(b instanceof Klass2).to.be.ok();
2013-03-02 05:49:20 +08:00
expect(constructor.called).to.be.ok();
expect(b.baz).to.eql(2);
2010-09-02 22:26:54 +08:00
b.bar();
2013-03-02 05:49:20 +08:00
expect(method.called).to.be.ok();
2010-09-02 22:26:54 +08:00
});
it("supports static properties", function () {
2013-03-02 05:49:20 +08:00
expect(Klass.bla).to.eql(1);
2010-09-02 22:26:54 +08:00
});
it("inherits parent static properties", function () {
2010-09-02 22:26:54 +08:00
var Klass2 = Klass.extend({});
2013-03-02 05:49:20 +08:00
expect(Klass2.bla).to.eql(1);
2010-09-02 22:26:54 +08:00
});
it("overrides parent static properties", function () {
2011-06-21 17:09:49 +08:00
var Klass2 = Klass.extend({statics: {bla: 2}});
2013-03-02 05:49:20 +08:00
expect(Klass2.bla).to.eql(2);
2011-06-21 17:09:49 +08:00
});
it("includes the given mixin", function () {
2010-09-02 22:26:54 +08:00
var a = new Klass();
2013-03-02 05:49:20 +08:00
expect(a.mixin).to.be.ok();
2010-09-02 22:26:54 +08:00
});
it("includes multiple mixins", function () {
2010-09-04 00:27:26 +08:00
var Klass2 = L.Class.extend({
includes: [{mixin: true}, {mixin2: true}]
});
var a = new Klass2();
2013-03-02 05:49:20 +08:00
expect(a.mixin).to.be.ok();
expect(a.mixin2).to.be.ok();
2010-09-04 00:27:26 +08:00
});
it("grants the ability to include the given mixin", function () {
2010-09-02 22:26:54 +08:00
Klass.include({mixin2: true});
2010-09-02 22:26:54 +08:00
var a = new Klass();
2013-03-02 05:49:20 +08:00
expect(a.mixin2).to.be.ok();
2010-09-02 22:26:54 +08:00
});
it("merges options instead of replacing them", function () {
2010-09-06 23:26:20 +08:00
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();
expect(a.options.foo1).to.eql(1);
expect(a.options.foo2).to.eql(3);
expect(a.options.foo3).to.eql(4);
});
it("gives new classes a distinct options object", function () {
var K1 = L.Class.extend({options: {}});
var K2 = K1.extend({});
expect(K2.prototype.options).not.to.equal(K1.prototype.options);
});
it("inherits options prototypally", function () {
var K1 = L.Class.extend({options: {}});
var K2 = K1.extend({options: {}});
K1.prototype.options.foo = 'bar';
expect(K2.prototype.options.foo).to.eql('bar');
2010-09-06 23:26:20 +08:00
});
it("adds constructor hooks correctly", function () {
2013-03-02 05:49:20 +08:00
var spy1 = sinon.spy();
Klass.addInitHook(spy1);
Klass.addInitHook('bar', 1, 2, 3);
var a = new Klass();
2013-03-02 05:49:20 +08:00
expect(spy1.called).to.be.ok();
expect(method.calledWith(1, 2, 3));
});
it("inherits constructor hooks", function () {
2013-03-02 05:49:20 +08:00
var spy1 = sinon.spy(),
2015-09-25 18:55:37 +08:00
spy2 = sinon.spy();
var Klass2 = Klass.extend({});
Klass.addInitHook(spy1);
Klass2.addInitHook(spy2);
var a = new Klass2();
2013-03-02 05:49:20 +08:00
expect(spy1.called).to.be.ok();
expect(spy2.called).to.be.ok();
});
it("does not call child constructor hooks", function () {
2013-03-02 05:49:20 +08:00
var spy1 = sinon.spy(),
2015-09-25 18:55:37 +08:00
spy2 = sinon.spy();
var Klass2 = Klass.extend({});
Klass.addInitHook(spy1);
Klass2.addInitHook(spy2);
var a = new Klass();
2013-03-02 05:49:20 +08:00
expect(spy1.called).to.be.ok();
expect(spy2.called).to.eql(false);
});
it("calls parent constructor hooks when child has none", function () {
var spy1 = sinon.spy();
Klass.addInitHook(spy1);
var Klass2 = Klass.extend({});
var a = new Klass2();
expect(spy1.called).to.be.ok();
});
2010-09-02 21:14:25 +08:00
});
2012-07-05 17:54:40 +08:00
describe("#include", function () {
var Klass;
beforeEach(function () {
Klass = L.Class.extend({});
});
it("returns the class with the extra methods", function () {
var q = sinon.spy();
var Qlass = Klass.include({quux: q});
var a = new Klass();
var b = new Qlass();
a.quux();
expect(q.called).to.be.ok();
b.quux();
expect(q.called).to.be.ok();
});
});
2012-07-05 17:54:40 +08:00
// TODO Class.mergeOptions
});