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;
51 cls.addStatics = Ext.Base.addStatics;
52 cls.addInheritableStatics = Ext.Base.addInheritableStatics;
55 describe("extend", function() {
57 it("should extend from Base if no 'extend' property found", function() {
60 Ext.Class.preprocessors.extend.fn(cls, data, emptyFn, emptyFn);
62 expect((new cls) instanceof Ext.Base).toBeTruthy();
65 it("should extend from given parent class", function() {
67 extend: My.awesome.Class
70 Ext.Class.preprocessors.extend.fn(cls, data, emptyFn, emptyFn);
72 expect((new cls) instanceof My.awesome.Class).toBeTruthy();
75 it("should have superclass reference", function() {
77 extend: My.awesome.Class
80 var parentPrototype = My.awesome.Class.prototype;
82 Ext.Class.preprocessors.extend.fn(cls, data, emptyFn, emptyFn);
84 expect(cls.superclass).toEqual(parentPrototype);
85 expect((new cls).superclass).toEqual(parentPrototype);
89 describe("config", function() {
91 it("should create getter if not exists", function() {
98 Ext.Class.preprocessors.config.fn(cls, data, emptyFn, emptyFn);
100 expect(data.getSomeName).toBeDefined();
103 it("should NOT create getter if already exists", function() {
106 someName: 'someValue'
111 cls.prototype.getSomeName = function() {
115 Ext.Class.preprocessors.config.fn(cls, data, emptyFn, emptyFn);
117 expect(data.getSomeName).not.toBeDefined();
120 it("should create setter if not exists", function() {
123 someName: 'someValue'
127 Ext.Class.preprocessors.config.fn(cls, data, emptyFn, emptyFn);
129 expect(data.setSomeName).toBeDefined();
132 it("should NOT create setter if already exists", function() {
135 someName: 'someValue'
141 cls.prototype.setSomeName = function() {
145 Ext.Class.preprocessors.config.fn(cls, data, emptyFn, emptyFn);
147 expect(data.setSomeName).not.toBeDefined();
150 it("should create apply if not exists", function() {
153 someName: 'someValue'
157 Ext.Class.preprocessors.config.fn(cls, data, emptyFn, emptyFn);
159 expect(data.applySomeName).toBeDefined();
162 it("should NOT create apply if already exists", function() {
165 someName: 'someValue'
170 cls.prototype.applySomeName = function() {
174 Ext.Class.preprocessors.config.fn(cls, data, emptyFn, emptyFn);
176 expect(data.applySomeName).not.toBeDefined();
180 describe("statics", function() {
181 it("should copy static properties to the class", function() {
184 someName: 'someValue',
185 someMethod: Ext.emptyFn
189 Ext.Class.preprocessors.statics.fn(cls, data, emptyFn, emptyFn);
193 expect(obj.statics).not.toBeDefined();
194 expect(cls.someName).toBe('someValue');
195 expect(cls.someMethod).toBe(Ext.emptyFn);
199 describe("inheritableStatics", function() {
201 it("should store names of inheritable static properties", function() {
203 inheritableStatics: {
204 someName: 'someValue',
205 someMethod: Ext.emptyFn
209 Ext.Class.preprocessors.inheritableStatics.fn(cls, data, emptyFn, emptyFn);
213 expect(obj.inheritableStatics).not.toBeDefined();
214 expect(cls.someName).toBe('someValue');
215 expect(cls.prototype.$inheritableStatics).toEqual(['someName', 'someMethod']);
216 expect(cls.someMethod).toBe(Ext.emptyFn);
219 it("should inherit inheritable statics", function() {
221 inheritableStatics: {
222 someName: 'someValue',
223 someMethod: Ext.emptyFn
225 }, cls2 = function(){};
227 Ext.Class.preprocessors.inheritableStatics.fn(cls, data, emptyFn, emptyFn);
228 Ext.Class.preprocessors.extend.fn(cls2, { extend: cls }, emptyFn, emptyFn);
230 expect(cls2.someName).toEqual('someValue');
231 expect(cls2.someMethod).toBe(Ext.emptyFn);
234 it("should NOT inherit inheritable statics if the class already has it", function() {
236 inheritableStatics: {
237 someName: 'someValue',
238 someMethod: Ext.emptyFn
240 }, cls2 = function(){};
242 cls2.someName = 'someOtherValue';
243 cls2.someMethod = function(){};
245 Ext.Class.preprocessors.inheritableStatics.fn(cls, data, emptyFn, emptyFn);
246 Ext.Class.preprocessors.extend.fn(cls2, { extend: cls }, emptyFn, emptyFn);
248 expect(cls2.someName).toEqual('someOtherValue');
249 expect(cls2.someMethod).not.toBe(Ext.emptyFn);
254 // END PREPROCESSORS =================================================================== /
256 describe("Instantiation", function() {
257 var subClass, parentClass, mixinClass1, mixinClass2;
259 beforeEach(function() {
260 mixinClass1 = new Ext.Class({
262 mixinConfig: 'mixinConfig'
265 constructor: function(config) {
266 this.initConfig(config);
268 this.mixinConstructor1Called = true;
271 mixinProperty1: 'mixinProperty1',
273 mixinMethod1: function() {
274 this.mixinMethodCalled = true;
278 mixinClass2 = new Ext.Class({
279 constructor: function(config) {
280 this.initConfig(config);
282 this.mixinConstructor2Called = true;
285 mixinProperty2: 'mixinProperty2',
287 mixinMethod2: function() {
288 this.mixinMethodCalled = true;
292 parentClass = new Ext.Class({
300 abe: 'Abraham Elias',
303 hobbies: ['football', 'bowling']
305 constructor: function(config) {
306 this.initConfig(config);
308 this.parentConstructorCalled = true;
310 this.mixins.mixin1.constructor.apply(this, arguments);
313 parentProperty: 'parentProperty',
315 parentMethod: function() {
316 this.parentMethodCalled = true;
320 subClass = new Ext.Class({
330 jacky: 'Jacky Nguyen',
331 tommy: 'Tommy Maintz'
333 hobbies: ['sleeping', 'eating', 'movies'],
336 constructor: function(config) {
337 this.initConfig(config);
339 this.subConstrutorCalled = true;
341 subClass.superclass.constructor.apply(this, arguments);
343 this.mixins.mixin2.constructor.apply(this, arguments);
345 myOwnMethod: function() {
346 this.myOwnMethodCalled = true;
351 describe("addStatics", function() {
352 it("single with name - value arguments", function() {
355 subClass.addStatics({
356 staticMethod: function(){
361 expect(subClass.staticMethod).toBeDefined();
362 subClass.staticMethod();
364 expect(called).toBeTruthy();
367 it("multiple with object map argument", function() {
368 subClass.addStatics({
369 staticProperty: 'something',
370 staticMethod: function(){}
373 expect(subClass.staticProperty).toEqual('something');
374 expect(subClass.staticMethod).toBeDefined();
379 describe("override", function() {
380 it("should override", function() {
382 myOwnMethod: function(){
383 this.isOverridden = true;
385 this.callOverridden(arguments);
389 var obj = new subClass;
392 expect(obj.isOverridden).toBe(true);
393 expect(obj.myOwnMethodCalled).toBe(true);
397 describe("mixin", function() {
398 it("should have all properties of mixins", function() {
399 var obj = new subClass;
400 expect(obj.mixinProperty1).toEqual('mixinProperty1');
401 expect(obj.mixinProperty2).toEqual('mixinProperty2');
402 expect(obj.mixinMethod1).toBeDefined();
403 expect(obj.mixinMethod2).toBeDefined();
404 expect(obj.config.mixinConfig).toEqual('mixinConfig');
408 describe("config", function() {
409 it("should merge properly", function() {
410 var obj = new subClass;
411 expect(obj.config).toEqual({
412 mixinConfig: 'mixinConfig',
416 abe: 'Abraham Elias',
418 jacky: 'Jacky Nguyen',
419 tommy: 'Tommy Maintz'
421 hobbies: ['sleeping', 'eating', 'movies'],
426 it("should apply default config", function() {
427 var obj = new subClass;
428 expect(obj.getName()).toEqual('subClass');
429 expect(obj.getIsCool()).toEqual(true);
430 expect(obj.getHobbies()).toEqual(['sleeping', 'eating', 'movies']);
433 it("should apply with supplied config", function() {
434 var obj = new subClass({
438 aaron: 'Aaron Conran'
442 expect(obj.getName()).toEqual('newName');
443 expect(obj.getIsCool()).toEqual(false);
444 expect(obj.getMembers().aaron).toEqual('Aaron Conran');
447 it("should not share the same config", function() {
448 var obj1 = new subClass({
452 aaron: 'Aaron Conran'
456 var obj2 = new subClass();
458 expect(obj2.getName()).not.toEqual('newName');
462 describe("overriden methods", function() {
463 it("should call self constructor", function() {
464 var obj = new subClass;
465 expect(obj.subConstrutorCalled).toBeTruthy();
468 it("should call parent constructor", function() {
469 var obj = new subClass;
470 expect(obj.parentConstructorCalled).toBeTruthy();
473 it("should call mixins constructors", function() {
474 var obj = new subClass;
475 expect(obj.mixinConstructor1Called).toBeTruthy();
476 expect(obj.mixinConstructor2Called).toBeTruthy();