1 describe("Ext.Class", function() {
2 var cls, emptyFn = function(){};
3 beforeEach(function() {
6 Class: function(){console.log(11)},
7 Class1: function(){console.log(12)},
8 Class2: function(){console.log(13)}
11 AnotherClass: function(){console.log(21)},
12 AnotherClass1: function(){console.log(22)},
13 AnotherClass2: function(){console.log(23)}
18 afterEach(function() {
20 window.My = undefined;
28 // START PREPROCESSORS =================================================================== /
29 describe("preprocessors", function() {
31 beforeEach(function() {
33 cls.prototype.config = {};
34 cls.ownMethod = function(name, fn) {
35 this.prototype[name] = fn;
39 describe("extend", function() {
41 it("should extend from Base if no 'extend' property found", function() {
44 Ext.Class.preprocessors.extend.fn(cls, data, emptyFn, emptyFn);
46 expect((new cls) instanceof Ext.Base).toBeTruthy();
49 it("should extend from given parent class", function() {
51 extend: My.awesome.Class
54 Ext.Class.preprocessors.extend.fn(cls, data, emptyFn, emptyFn);
56 expect((new cls) instanceof My.awesome.Class).toBeTruthy();
59 it("should have superclass reference", function() {
61 extend: My.awesome.Class
64 var parentPrototype = My.awesome.Class.prototype;
66 Ext.Class.preprocessors.extend.fn(cls, data, emptyFn, emptyFn);
68 expect(cls.superclass).toEqual(parentPrototype);
69 expect((new cls).superclass).toEqual(parentPrototype);
73 describe("config", function() {
75 it("should create getter if not exists", function() {
82 Ext.Class.preprocessors.config.fn(cls, data, emptyFn, emptyFn);
84 expect(data.getSomeName).toBeDefined();
87 it("should NOT create getter if already exists", function() {
95 cls.prototype.getSomeName = function() {
99 Ext.Class.preprocessors.config.fn(cls, data, emptyFn, emptyFn);
101 expect(data.getSomeName).not.toBeDefined();
104 it("should create setter if not exists", function() {
107 someName: 'someValue'
111 Ext.Class.preprocessors.config.fn(cls, data, emptyFn, emptyFn);
113 expect(data.setSomeName).toBeDefined();
116 it("should NOT create setter if already exists", function() {
119 someName: 'someValue'
125 cls.prototype.setSomeName = function() {
129 Ext.Class.preprocessors.config.fn(cls, data, emptyFn, emptyFn);
131 expect(data.setSomeName).not.toBeDefined();
134 it("should create apply if not exists", function() {
137 someName: 'someValue'
141 Ext.Class.preprocessors.config.fn(cls, data, emptyFn, emptyFn);
143 expect(data.applySomeName).toBeDefined();
146 it("should NOT create apply if already exists", function() {
149 someName: 'someValue'
154 cls.prototype.applySomeName = function() {
158 Ext.Class.preprocessors.config.fn(cls, data, emptyFn, emptyFn);
160 expect(data.applySomeName).not.toBeDefined();
164 describe("statics", function() {
165 it("should copy static properties to the class", function() {
168 someName: 'someValue',
169 someMethod: Ext.emptyFn
173 Ext.Class.preprocessors.statics.fn(cls, data, emptyFn, emptyFn);
177 expect(obj.statics).not.toBeDefined();
178 expect(cls.someName).toBe('someValue');
179 expect(cls.someMethod).toBe(Ext.emptyFn);
183 describe("inheritableStatics", function() {
185 it("should store names of inheritable static properties", function() {
187 inheritableStatics: {
188 someName: 'someValue',
189 someMethod: Ext.emptyFn
193 Ext.Class.preprocessors.inheritableStatics.fn(cls, data, emptyFn, emptyFn);
197 expect(obj.inheritableStatics).not.toBeDefined();
198 expect(cls.someName).toBe('someValue');
199 expect(cls.prototype.$inheritableStatics).toEqual(['someName', 'someMethod']);
200 expect(cls.someMethod).toBe(Ext.emptyFn);
203 it("should inherit inheritable statics", function() {
205 inheritableStatics: {
206 someName: 'someValue',
207 someMethod: Ext.emptyFn
209 }, cls2 = function(){};
211 Ext.Class.preprocessors.inheritableStatics.fn(cls, data, emptyFn, emptyFn);
212 Ext.Class.preprocessors.extend.fn(cls2, { extend: cls }, emptyFn, emptyFn);
214 expect(cls2.someName).toEqual('someValue');
215 expect(cls2.someMethod).toBe(Ext.emptyFn);
218 it("should NOT inherit inheritable statics if the class already has it", function() {
220 inheritableStatics: {
221 someName: 'someValue',
222 someMethod: Ext.emptyFn
224 }, cls2 = function(){};
226 cls2.someName = 'someOtherValue';
227 cls2.someMethod = function(){};
229 Ext.Class.preprocessors.inheritableStatics.fn(cls, data, emptyFn, emptyFn);
230 Ext.Class.preprocessors.extend.fn(cls2, { extend: cls }, emptyFn, emptyFn);
232 expect(cls2.someName).toEqual('someOtherValue');
233 expect(cls2.someMethod).not.toBe(Ext.emptyFn);
238 // END PREPROCESSORS =================================================================== /
240 describe("Instantiation", function() {
241 var subClass, parentClass, mixinClass1, mixinClass2;
243 beforeEach(function() {
244 mixinClass1 = new Ext.Class({
246 mixinConfig: 'mixinConfig'
249 constructor: function(config) {
250 this.initConfig(config);
252 this.mixinConstructor1Called = true;
255 mixinProperty1: 'mixinProperty1',
257 mixinMethod1: function() {
258 this.mixinMethodCalled = true;
262 mixinClass2 = new Ext.Class({
263 constructor: function(config) {
264 this.initConfig(config);
266 this.mixinConstructor2Called = true;
269 mixinProperty2: 'mixinProperty2',
271 mixinMethod2: function() {
272 this.mixinMethodCalled = true;
276 parentClass = new Ext.Class({
284 abe: 'Abraham Elias',
287 hobbies: ['football', 'bowling']
289 constructor: function(config) {
290 this.initConfig(config);
292 this.parentConstructorCalled = true;
294 this.mixins.mixin1.constructor.apply(this, arguments);
297 parentProperty: 'parentProperty',
299 parentMethod: function() {
300 this.parentMethodCalled = true;
304 subClass = new Ext.Class({
314 jacky: 'Jacky Nguyen',
315 tommy: 'Tommy Maintz'
317 hobbies: ['sleeping', 'eating', 'movies'],
320 constructor: function(config) {
321 this.initConfig(config);
323 this.subConstrutorCalled = true;
325 subClass.superclass.constructor.apply(this, arguments);
327 this.mixins.mixin2.constructor.apply(this, arguments);
329 myOwnMethod: function() {
330 this.myOwnMethodCalled = true;
335 describe("addStatics", function() {
336 it("single with name - value arguments", function() {
339 subClass.addStatics({
340 staticMethod: function(){
345 expect(subClass.staticMethod).toBeDefined();
346 subClass.staticMethod();
348 expect(called).toBeTruthy();
351 it("multiple with object map argument", function() {
352 subClass.addStatics({
353 staticProperty: 'something',
354 staticMethod: function(){}
357 expect(subClass.staticProperty).toEqual('something');
358 expect(subClass.staticMethod).toBeDefined();
363 describe("override", function() {
364 it("should override", function() {
366 myOwnMethod: function(){
367 this.isOverridden = true;
369 this.callOverridden(arguments);
373 var obj = new subClass;
376 expect(obj.isOverridden).toBe(true);
377 expect(obj.myOwnMethodCalled).toBe(true);
381 describe("mixin", function() {
382 it("should have all properties of mixins", function() {
383 var obj = new subClass;
384 expect(obj.mixinProperty1).toEqual('mixinProperty1');
385 expect(obj.mixinProperty2).toEqual('mixinProperty2');
386 expect(obj.mixinMethod1).toBeDefined();
387 expect(obj.mixinMethod2).toBeDefined();
388 expect(obj.config.mixinConfig).toEqual('mixinConfig');
392 describe("config", function() {
393 it("should merge properly", function() {
394 var obj = new subClass;
395 expect(obj.config).toEqual({
396 mixinConfig: 'mixinConfig',
400 abe: 'Abraham Elias',
402 jacky: 'Jacky Nguyen',
403 tommy: 'Tommy Maintz'
405 hobbies: ['sleeping', 'eating', 'movies'],
410 it("should apply default config", function() {
411 var obj = new subClass;
412 expect(obj.getName()).toEqual('subClass');
413 expect(obj.getIsCool()).toEqual(true);
414 expect(obj.getHobbies()).toEqual(['sleeping', 'eating', 'movies']);
417 it("should apply with supplied config", function() {
418 var obj = new subClass({
422 aaron: 'Aaron Conran'
426 expect(obj.getName()).toEqual('newName');
427 expect(obj.getIsCool()).toEqual(false);
428 expect(obj.getMembers().aaron).toEqual('Aaron Conran');
431 it("should not share the same config", function() {
432 var obj1 = new subClass({
436 aaron: 'Aaron Conran'
440 var obj2 = new subClass();
442 expect(obj2.getName()).not.toEqual('newName');
446 describe("overriden methods", function() {
447 it("should call self constructor", function() {
448 var obj = new subClass;
449 expect(obj.subConstrutorCalled).toBeTruthy();
452 it("should call parent constructor", function() {
453 var obj = new subClass;
454 expect(obj.parentConstructorCalled).toBeTruthy();
457 it("should call mixins constructors", function() {
458 var obj = new subClass;
459 expect(obj.mixinConstructor1Called).toBeTruthy();
460 expect(obj.mixinConstructor2Called).toBeTruthy();