# Code coverage report for ./suites/core/ClassSpec.js

## Statements: 100% (70 / 70) Branches: 100% (0 / 0) Functions: 100% (15 / 15) Lines: 100% (70 / 70)

All files » ./suites/core/ » ClassSpec.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    1571

    1
    1

    1
    12
    12

    12

    1
    1

    1
    1

    1

    1

    1
    1

    1

    1
    1

    1
    1

    1

    1

    1
    1

    1
    1

    1

    1
    1

    1

    1
    1
    1

    1
    1

    1

    1
    1

    1
    1

    1
    1

    1
    1

    1

    1

    1

    1
    1

    1
    1

    1

    1
    1

    1
    1

    1

    1
    1

    1

    1
    1

    1
    1

    1

    1
    1

    1

    1
    1

     describe("Class", function() {

    	describe("#extend", function() {
    		var Klass,
    			constructor,
    			method;

    		beforeEach(function() {
    			constructor = sinon.spy();
    			method = sinon.spy();

    			Klass = L.Class.extend({
    				statics: {bla: 1},
    				includes: {mixin: true},

    				initialize: constructor,
    				foo: 5,
    				bar: method
    			});
    		});

    		it("creates a class with the given constructor & properties", function() {
    			var a = new Klass();

    			expect(constructor.called).to.be.ok();
    			expect(a.foo).to.eql(5);

    			a.bar();

    			expect(method.called).to.be.ok();
    		});

    		it("inherits parent classes' constructor & properties", function() {
    			var Klass2 = Klass.extend({baz: 2});

    			var b = new Klass2();

    			expect(b instanceof Klass).to.be.ok();
    			expect(b instanceof Klass2).to.be.ok();

    			expect(constructor.called).to.be.ok();
    			expect(b.baz).to.eql(2);

    			b.bar();

    			expect(method.called).to.be.ok();
    		});

    		it("supports static properties", function() {
    			expect(Klass.bla).to.eql(1);
    		});

    		it("inherits parent static properties", function() {
    			var Klass2 = Klass.extend({});

    			expect(Klass2.bla).to.eql(1);
    		});

    		it("overrides parent static properties", function() {
    			var Klass2 = Klass.extend({statics: {bla: 2}});

    			expect(Klass2.bla).to.eql(2);
    		});

    		it("includes the given mixin", function() {
    			var a = new Klass();
    			expect(a.mixin).to.be.ok();
    		});

    		it("includes multiple mixins", function() {
    			var Klass2 = L.Class.extend({
    				includes: [{mixin: true}, {mixin2: true}]
    			});
    			var a = new Klass2();

    			expect(a.mixin).to.be.ok();
    			expect(a.mixin2).to.be.ok();
    		});

    		it("grants the ability to include the given mixin", function() {
    			Klass.include({mixin2: true});

    			var a = new Klass();
    			expect(a.mixin2).to.be.ok();
    		});

    		it("merges 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).to.eql({
    				foo1: 1,
    				foo2: 3,
    				foo3: 4
    			});
    		});

    		it("adds constructor hooks correctly", function () {
    			var spy1 = sinon.spy();

    			Klass.addInitHook(spy1);
    			Klass.addInitHook('bar', 1, 2, 3);

    			var a = new Klass();

    			expect(spy1.called).to.be.ok();
    			expect(method.calledWith(1, 2, 3));
    		});

    		it("inherits constructor hooks", function () {
    			var spy1 = sinon.spy(),
    				spy2 = sinon.spy();

    			var Klass2 = Klass.extend({});

    			Klass.addInitHook(spy1);
    			Klass2.addInitHook(spy2);

    			var a = new Klass2();

    			expect(spy1.called).to.be.ok();
    			expect(spy2.called).to.be.ok();
    		});

    		it("does not call child constructor hooks", function () {
    			var spy1 = sinon.spy(),
    				spy2 = sinon.spy();

    			var Klass2 = Klass.extend({});

    			Klass.addInitHook(spy1);
    			Klass2.addInitHook(spy2);

    			var a = new Klass();

    			expect(spy1.called).to.be.ok();
    			expect(spy2.called).to.eql(false);
    		});
    	});

    	// TODO Class.include

    	// TODO Class.mergeOptions
    });