aboutsummaryrefslogtreecommitdiffstats
path: root/extlib/leaflet/spec/suites/core/ClassSpec.js
blob: 7a289154b85f77fc3aead4f5c1eb3276a61c7394 (plain)
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
describe("Class", function() {
	
	describe("#extend", function() {
		var Klass,
			constructor,
			method;
		
		beforeEach(function() {
			constructor = jasmine.createSpy(),
			method = jasmine.createSpy();

			Klass = L.Class.extend({
				statics: {bla: 1},
				includes: {mixin: true},
				
				initialize: constructor,
				foo: 5,
				bar: method
			});
		});
		
		it("should create a class with the given constructor & properties", function() {
			var a = new Klass();
			
			expect(constructor).toHaveBeenCalled();
			expect(a.foo).toEqual(5);
			
			a.bar();
			
			expect(method).toHaveBeenCalled();
		});
		
		it("should inherit parent classes' constructor & properties", function() {
			var Klass2 = Klass.extend({baz: 2});
			
			var b = new Klass2();
			
			expect(b instanceof Klass).toBeTruthy();
			expect(b instanceof Klass2).toBeTruthy();
			
			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();
			b.superclass.initialize.call(this);
			expect(constructor).toHaveBeenCalled();

			b.superclass.bar.call(this);
			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);
		});
		
		it("should include the given mixin", function() {
			var a = new Klass();
			expect(a.mixin).toBeTruthy();
		});
		
		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();
		});
		
		it("should grant the ability to include the given mixin", function() {
			Klass.include({mixin2: true});
			
			var a = new Klass();
			expect(a.mixin2).toBeTruthy();
		});
		
		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
			});
		});
	});
});