3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
15 describe("Ext.Class", function() {
16 var cls, emptyFn = function(){};
17 beforeEach(function() {
20 Class: function(){console.log(11)},
21 Class1: function(){console.log(12)},
22 Class2: function(){console.log(13)}
25 AnotherClass: function(){console.log(21)},
26 AnotherClass1: function(){console.log(22)},
27 AnotherClass2: function(){console.log(23)}
32 afterEach(function() {
34 window.My = undefined;
42 // START PREPROCESSORS =================================================================== /
43 describe("preprocessors", function() {
45 beforeEach(function() {
47 cls.prototype.config = {};
48 cls.ownMethod = function(name, fn) {
49 this.prototype[name] = fn;
53 describe("extend", function() {
55 it("should extend from Base if no 'extend' property found", function() {
58 Ext.Class.preprocessors.extend.fn(cls, data, emptyFn, emptyFn);
60 expect((new cls) instanceof Ext.Base).toBeTruthy();
63 it("should extend from given parent class", function() {
65 extend: My.awesome.Class
68 Ext.Class.preprocessors.extend.fn(cls, data, emptyFn, emptyFn);
70 expect((new cls) instanceof My.awesome.Class).toBeTruthy();
73 it("should have superclass reference", function() {
75 extend: My.awesome.Class
78 var parentPrototype = My.awesome.Class.prototype;
80 Ext.Class.preprocessors.extend.fn(cls, data, emptyFn, emptyFn);
82 expect(cls.superclass).toEqual(parentPrototype);
83 expect((new cls).superclass).toEqual(parentPrototype);
87 describe("config", function() {
89 it("should create getter if not exists", function() {
96 Ext.Class.preprocessors.config.fn(cls, data, emptyFn, emptyFn);
98 expect(data.getSomeName).toBeDefined();
101 it("should NOT create getter if already exists", function() {
104 someName: 'someValue'
109 cls.prototype.getSomeName = function() {
113 Ext.Class.preprocessors.config.fn(cls, data, emptyFn, emptyFn);
115 expect(data.getSomeName).not.toBeDefined();
118 it("should create setter if not exists", function() {
121 someName: 'someValue'
125 Ext.Class.preprocessors.config.fn(cls, data, emptyFn, emptyFn);
127 expect(data.setSomeName).toBeDefined();
130 it("should NOT create setter if already exists", function() {
133 someName: 'someValue'
139 cls.prototype.setSomeName = function() {
143 Ext.Class.preprocessors.config.fn(cls, data, emptyFn, emptyFn);
145 expect(data.setSomeName).not.toBeDefined();
148 it("should create apply if not exists", function() {
151 someName: 'someValue'
155 Ext.Class.preprocessors.config.fn(cls, data, emptyFn, emptyFn);
157 expect(data.applySomeName).toBeDefined();
160 it("should NOT create apply if already exists", function() {
163 someName: 'someValue'
168 cls.prototype.applySomeName = function() {
172 Ext.Class.preprocessors.config.fn(cls, data, emptyFn, emptyFn);
174 expect(data.applySomeName).not.toBeDefined();
178 describe("statics", function() {
179 it("should copy static properties to the class", function() {
182 someName: 'someValue',
183 someMethod: Ext.emptyFn
187 Ext.Class.preprocessors.statics.fn(cls, data, emptyFn, emptyFn);
191 expect(obj.statics).not.toBeDefined();
192 expect(cls.someName).toBe('someValue');
193 expect(cls.someMethod).toBe(Ext.emptyFn);
197 describe("inheritableStatics", function() {
199 it("should store names of inheritable static properties", function() {
201 inheritableStatics: {
202 someName: 'someValue',
203 someMethod: Ext.emptyFn
207 Ext.Class.preprocessors.inheritableStatics.fn(cls, data, emptyFn, emptyFn);
211 expect(obj.inheritableStatics).not.toBeDefined();
212 expect(cls.someName).toBe('someValue');
213 expect(cls.prototype.$inheritableStatics).toEqual(['someName', 'someMethod']);
214 expect(cls.someMethod).toBe(Ext.emptyFn);
217 it("should inherit inheritable statics", function() {
219 inheritableStatics: {
220 someName: 'someValue',
221 someMethod: Ext.emptyFn
223 }, cls2 = function(){};
225 Ext.Class.preprocessors.inheritableStatics.fn(cls, data, emptyFn, emptyFn);
226 Ext.Class.preprocessors.extend.fn(cls2, { extend: cls }, emptyFn, emptyFn);
228 expect(cls2.someName).toEqual('someValue');
229 expect(cls2.someMethod).toBe(Ext.emptyFn);
232 it("should NOT inherit inheritable statics if the class already has it", function() {
234 inheritableStatics: {
235 someName: 'someValue',
236 someMethod: Ext.emptyFn
238 }, cls2 = function(){};
240 cls2.someName = 'someOtherValue';
241 cls2.someMethod = function(){};
243 Ext.Class.preprocessors.inheritableStatics.fn(cls, data, emptyFn, emptyFn);
244 Ext.Class.preprocessors.extend.fn(cls2, { extend: cls }, emptyFn, emptyFn);
246 expect(cls2.someName).toEqual('someOtherValue');
247 expect(cls2.someMethod).not.toBe(Ext.emptyFn);
252 // END PREPROCESSORS =================================================================== /
254 describe("Instantiation", function() {
255 var subClass, parentClass, mixinClass1, mixinClass2;
257 beforeEach(function() {
258 mixinClass1 = new Ext.Class({
260 mixinConfig: 'mixinConfig'
263 constructor: function(config) {
264 this.initConfig(config);
266 this.mixinConstructor1Called = true;
269 mixinProperty1: 'mixinProperty1',
271 mixinMethod1: function() {
272 this.mixinMethodCalled = true;
276 mixinClass2 = new Ext.Class({
277 constructor: function(config) {
278 this.initConfig(config);
280 this.mixinConstructor2Called = true;
283 mixinProperty2: 'mixinProperty2',
285 mixinMethod2: function() {
286 this.mixinMethodCalled = true;
290 parentClass = new Ext.Class({
298 abe: 'Abraham Elias',
301 hobbies: ['football', 'bowling']
303 constructor: function(config) {
304 this.initConfig(config);
306 this.parentConstructorCalled = true;
308 this.mixins.mixin1.constructor.apply(this, arguments);
311 parentProperty: 'parentProperty',
313 parentMethod: function() {
314 this.parentMethodCalled = true;
318 subClass = new Ext.Class({
328 jacky: 'Jacky Nguyen',
329 tommy: 'Tommy Maintz'
331 hobbies: ['sleeping', 'eating', 'movies'],
334 constructor: function(config) {
335 this.initConfig(config);
337 this.subConstrutorCalled = true;
339 subClass.superclass.constructor.apply(this, arguments);
341 this.mixins.mixin2.constructor.apply(this, arguments);
343 myOwnMethod: function() {
344 this.myOwnMethodCalled = true;
349 describe("addStatics", function() {
350 it("single with name - value arguments", function() {
353 subClass.addStatics({
354 staticMethod: function(){
359 expect(subClass.staticMethod).toBeDefined();
360 subClass.staticMethod();
362 expect(called).toBeTruthy();
365 it("multiple with object map argument", function() {
366 subClass.addStatics({
367 staticProperty: 'something',
368 staticMethod: function(){}
371 expect(subClass.staticProperty).toEqual('something');
372 expect(subClass.staticMethod).toBeDefined();
377 describe("override", function() {
378 it("should override", function() {
380 myOwnMethod: function(){
381 this.isOverridden = true;
383 this.callOverridden(arguments);
387 var obj = new subClass;
390 expect(obj.isOverridden).toBe(true);
391 expect(obj.myOwnMethodCalled).toBe(true);
395 describe("mixin", function() {
396 it("should have all properties of mixins", function() {
397 var obj = new subClass;
398 expect(obj.mixinProperty1).toEqual('mixinProperty1');
399 expect(obj.mixinProperty2).toEqual('mixinProperty2');
400 expect(obj.mixinMethod1).toBeDefined();
401 expect(obj.mixinMethod2).toBeDefined();
402 expect(obj.config.mixinConfig).toEqual('mixinConfig');
406 describe("config", function() {
407 it("should merge properly", function() {
408 var obj = new subClass;
409 expect(obj.config).toEqual({
410 mixinConfig: 'mixinConfig',
414 abe: 'Abraham Elias',
416 jacky: 'Jacky Nguyen',
417 tommy: 'Tommy Maintz'
419 hobbies: ['sleeping', 'eating', 'movies'],
424 it("should apply default config", function() {
425 var obj = new subClass;
426 expect(obj.getName()).toEqual('subClass');
427 expect(obj.getIsCool()).toEqual(true);
428 expect(obj.getHobbies()).toEqual(['sleeping', 'eating', 'movies']);
431 it("should apply with supplied config", function() {
432 var obj = new subClass({
436 aaron: 'Aaron Conran'
440 expect(obj.getName()).toEqual('newName');
441 expect(obj.getIsCool()).toEqual(false);
442 expect(obj.getMembers().aaron).toEqual('Aaron Conran');
445 it("should not share the same config", function() {
446 var obj1 = new subClass({
450 aaron: 'Aaron Conran'
454 var obj2 = new subClass();
456 expect(obj2.getName()).not.toEqual('newName');
460 describe("overriden methods", function() {
461 it("should call self constructor", function() {
462 var obj = new subClass;
463 expect(obj.subConstrutorCalled).toBeTruthy();
466 it("should call parent constructor", function() {
467 var obj = new subClass;
468 expect(obj.parentConstructorCalled).toBeTruthy();
471 it("should call mixins constructors", function() {
472 var obj = new subClass;
473 expect(obj.mixinConstructor1Called).toBeTruthy();
474 expect(obj.mixinConstructor2Called).toBeTruthy();