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.ClassManager", function() {
16 var manager = Ext.ClassManager,
17 cls, emptyFn = function(){};
21 beforeEach(function() {
22 manager.enableNamespaceParseCache = false;
25 Class: function(){console.log(11);},
26 Class1: function(){console.log(12);},
27 Class2: function(){console.log(13);}
30 AnotherClass: function(){console.log(21);},
31 AnotherClass1: function(){console.log(22);},
32 AnotherClass2: function(){console.log(23);}
37 afterEach(function() {
38 if (window.Something) {
39 window.Something = undefined;
43 window.My = undefined;
51 window.Test = undefined;
55 delete window.Something;
60 manager.enableNamespaceParseCache = true;
63 describe("parseNamespace", function() {
64 it("should return the broken-down namespace", function() {
65 var parts = manager.parseNamespace('Some.strange.alien.Namespace');
67 expect(parts).toEqual([Ext.global, 'Some', 'strange', 'alien', 'Namespace']);
70 it("should return the broken-down namespace with object rewrites", function() {
71 var parts = manager.parseNamespace('Ext.some.Namespace');
73 expect(parts).toEqual([Ext, 'some', 'Namespace']);
77 describe("exist", function() {
78 it("should return whether a single class exists", function() {
79 expect(manager.isCreated('My.notexisting.Class')).toBe(false);
80 expect(manager.isCreated('My.awesome.Class')).toBe(true);
84 describe("loader preprocessor", function() {
85 beforeEach(function() {
89 it("should load and replace string class names with objects", function() {
91 extend: 'My.awesome.Class',
93 name1: My.cool.AnotherClass,
94 name2: 'My.cool.AnotherClass1'
98 extend: My.awesome.Class,
100 name1: My.cool.AnotherClass,
101 name2: My.cool.AnotherClass1
107 spyOn(Ext.Loader, 'require').andCallFake(function(classes, fn) {
108 classNames = classes;
112 Ext.Class.getPreprocessor('loader').fn(cls, data, emptyFn, emptyFn);
114 expect(Ext.Loader.require).toHaveBeenCalled();
115 expect(classNames).toEqual(['My.awesome.Class', 'My.cool.AnotherClass1']);
116 expect(data).toEqual(expected);
120 describe("create", function() {
121 var subClass, parentClass, mixinClass1, mixinClass2, subSubClass;
123 beforeEach(function() {
124 mixinClass1 = manager.create('I.am.the.MixinClass1', {
126 mixinConfig: 'mixinConfig'
129 constructor: function() {
130 this.mixinConstructor1Called = true;
133 mixinProperty1: 'mixinProperty1',
135 mixinMethod1: function() {
136 this.mixinMethodCalled = true;
140 mixinClass2 = manager.create('I.am.the.MixinClass2', {
141 constructor: function() {
142 this.mixinConstructor2Called = true;
145 mixinProperty2: 'mixinProperty2',
147 mixinMethod2: function() {
148 this.mixinMethodCalled = true;
152 parentClass = manager.create('I.am.the.ParentClass', {
153 alias: ['parentclass', 'superclass'],
156 mixin1: 'I.am.the.MixinClass1'
163 abe: 'Abraham Elias',
166 hobbies: ['football', 'bowling']
169 onClassExtended: function(subClass, data) {
170 subClass.onClassExtendedCalled = true;
173 constructor: function(config) {
174 this.initConfig(config);
176 this.parentConstructorCalled = true;
178 this.mixins.mixin1.constructor.apply(this, arguments);
181 parentProperty: 'parentProperty',
183 parentMethod: function() {
184 this.parentMethodCalled = true;
188 subClass = manager.create('I.am.the.SubClass', {
191 extend: 'I.am.the.ParentClass',
194 mixin1: 'I.am.the.MixinClass1',
195 mixin2: 'I.am.the.MixinClass2'
201 jacky: 'Jacky Nguyen',
202 tommy: 'Tommy Maintz'
204 hobbies: ['sleeping', 'eating', 'movies'],
207 constructor: function() {
208 this.subConstrutorCalled = true;
210 this.superclass.constructor.apply(this, arguments);
212 this.mixins.mixin2.constructor.apply(this, arguments);
214 myOwnMethod: function() {
215 this.myOwnMethodCalled = true;
220 it("should create the namespace", function() {
221 expect(I).toBeDefined();
222 expect(I.am).toBeDefined();
223 expect(I.am.the).toBeDefined();
224 expect(I.am.the.SubClass).toBeDefined();
227 it("should get className", function() {
228 expect(Ext.getClassName(subClass)).toEqual('I.am.the.SubClass');
231 describe("addStatics", function() {
232 it("single with name - value arguments", function() {
235 subClass.addStatics({
236 staticMethod: function(){
241 expect(subClass.staticMethod).toBeDefined();
242 subClass.staticMethod();
244 expect(called).toBeTruthy();
247 it("multiple with object map argument", function() {
248 subClass.addStatics({
249 staticProperty: 'something',
250 staticMethod: function(){}
253 expect(subClass.staticProperty).toEqual('something');
254 expect(subClass.staticMethod).toBeDefined();
258 describe("mixin", function() {
259 it("should have all properties of mixins", function() {
260 var obj = new subClass();
261 expect(obj.mixinProperty1).toEqual('mixinProperty1');
262 expect(obj.mixinProperty2).toEqual('mixinProperty2');
263 expect(obj.mixinMethod1).toBeDefined();
264 expect(obj.mixinMethod2).toBeDefined();
265 expect(obj.config.mixinConfig).toEqual('mixinConfig');
269 describe("config", function() {
270 it("should merge properly", function() {
271 var obj = new subClass();
273 expect(obj.config).toEqual({
274 mixinConfig: 'mixinConfig',
278 abe: 'Abraham Elias',
280 jacky: 'Jacky Nguyen',
281 tommy: 'Tommy Maintz'
283 hobbies: ['sleeping', 'eating', 'movies'],
288 it("should apply default config", function() {
289 var obj = new subClass();
291 expect(obj.getName()).toEqual('subClass');
292 expect(obj.getIsCool()).toEqual(true);
293 expect(obj.getHobbies()).toEqual(['sleeping', 'eating', 'movies']);
296 it("should apply with supplied config", function() {
297 var obj = new subClass({
301 aaron: 'Aaron Conran'
305 expect(obj.getName()).toEqual('newName');
306 expect(obj.getIsCool()).toEqual(false);
307 expect(obj.getMembers().aaron).toEqual('Aaron Conran');
311 describe("overriden methods", function() {
312 it("should call self constructor", function() {
313 var obj = new subClass();
314 expect(obj.subConstrutorCalled).toBeTruthy();
317 it("should call parent constructor", function() {
318 var obj = new subClass();
319 expect(obj.parentConstructorCalled).toBeTruthy();
322 it("should call mixins constructors", function() {
323 var obj = new subClass();
324 expect(obj.mixinConstructor1Called).toBeTruthy();
325 expect(obj.mixinConstructor2Called).toBeTruthy();
329 describe("alias", function() {
330 it("should store alias", function() {
331 expect(manager.getByAlias('subclass')).toBe(subClass);
334 it("should store multiple aliases", function() {
335 expect(manager.getByAlias('parentclass')).toBe(parentClass);
336 expect(manager.getByAlias('superclass')).toBe(parentClass);
340 describe("onClassExtended", function() {
341 it("should store an internal reference", function() {
342 expect(parentClass.prototype.$onExtended).toBeDefined();
343 expect(subClass.prototype.$onExtended).toBeDefined();
346 it("should invoke the internal reference", function() {
347 expect(subClass.onClassExtendedCalled).toBe(true);
352 describe("instantiate", function() {
353 beforeEach(function() {
354 manager.create('Test.stuff.Person', {
357 constructor: function(name, age, sex) {
363 eat: function(food) {
364 this.eatenFood = food;
368 manager.create('Test.stuff.Developer', {
371 extend: 'Test.stuff.Person',
373 constructor: function(isGeek, name, age, sex) {
374 this.isGeek = isGeek;
376 return this.superclass.constructor.apply(this, arguments);
379 code: function(language) {
380 this.languageCoded = language;
387 it("should create the instance by full class name", function() {
388 var me = manager.instantiate('Test.stuff.Person', 'Jacky', 24, 'male');
389 expect(me instanceof Test.stuff.Person).toBe(true);
392 it("should create the instance by alias", function() {
393 var me = manager.instantiateByAlias('person', 'Jacky', 24, 'male');
394 expect(me instanceof Test.stuff.Person).toBe(true);
397 it("should pass all arguments to the constructor", function() {
398 var me = manager.instantiateByAlias('person', 'Jacky', 24, 'male');
399 expect(me.name).toBe('Jacky');
400 expect(me.age).toBe(24);
401 expect(me.sex).toBe('male');
404 it("should have all methods in prototype", function() {
405 var me = manager.instantiateByAlias('person', 'Jacky', 24, 'male');
408 expect(me.eatenFood).toBe('rice');
411 it("should works with inheritance", function() {
412 var me = manager.instantiateByAlias('developer', true, 'Jacky', 24, 'male');
413 me.code('javascript');
415 expect(me.languageCoded).toBe('javascript');
416 expect(me.eatenFood).toBe('bugs');
420 describe("post-processors", function() {
422 xdescribe("uses", function() {
423 //expect(Something.Cool).toBeDefined();
424 //expect(Something.Cool instanceof test).toBeTruthy();
427 describe("singleton", function() {
428 it("should create the instance namespace and return the class", function() {
429 var test = Ext.define('Something.Cool', {
431 someMethod: function() {
432 this.someMethodCalled = true;
434 someProperty: 'something'
437 expect(Something.Cool).toBeDefined();
438 expect(Something.Cool instanceof test).toBeTruthy();
442 describe("alias xtype", function() {
443 it("should set xtype as a static class property", function() {
444 var test = Ext.define('Something.Cool', {
448 expect(Something.Cool.xtype).toEqual('cool');
452 describe("alternate", function() {
453 it("should create the alternate", function() {
454 Ext.define('Something.Cool', {
455 alternateClassName: 'Something.CoolAsWell',
457 someMethod: function() {
458 this.someMethodCalled = true;
461 someProperty: 'something'
464 expect(Something.CoolAsWell).toBeDefined();
465 expect(Something.CoolAsWell).toBe(Something.Cool);
468 it("should create the alternate", function() {
469 Ext.define('Something.Cool', {
470 alternateClassName: ['Something.CoolAsWell', 'Something.AlsoCool']
473 expect(Something.CoolAsWell).toBe(Something.Cool);
474 expect(Something.AlsoCool).toBe(Something.Cool);
479 describe("createNamespaces", function() {
482 it("should have an alias Ext.namespace", function() {
483 spyOn(Ext.ClassManager, 'createNamespaces');
484 Ext.namespace('a', 'b', 'c');
485 expect(Ext.ClassManager.createNamespaces).toHaveBeenCalledWith('a', 'b', 'c');
488 it("should create a single top level namespace", function() {
489 Ext.ClassManager.createNamespaces('FooTest1');
491 expect(w.FooTest1).toBeDefined();
493 if (jasmine.browser.isIE6 || jasmine.browser.isIE7 || jasmine.browser.isIE8) {
494 w.FooTest1 = undefined;
500 it("should create multiple top level namespace", function() {
501 Ext.ClassManager.createNamespaces('FooTest2', 'FooTest3', 'FooTest4');
503 expect(w.FooTest2).toBeDefined();
504 expect(w.FooTest3).toBeDefined();
505 expect(w.FooTest4).toBeDefined();
507 if (jasmine.browser.isIE6 || jasmine.browser.isIE7 || jasmine.browser.isIE8) {
508 w.FooTest2 = undefined;
509 w.FooTest3 = undefined;
510 w.FooTest4 = undefined;
518 it("should create a chain of namespaces, starting from a top level", function() {
519 Ext.ClassManager.createNamespaces('FooTest5', 'FooTest5.ns1', 'FooTest5.ns1.ns2', 'FooTest5.ns1.ns2.ns3');
521 expect(w.FooTest5).toBeDefined();
522 expect(w.FooTest5.ns1).toBeDefined();
523 expect(w.FooTest5.ns1.ns2).toBeDefined();
524 expect(w.FooTest5.ns1.ns2.ns3).toBeDefined();
526 if (jasmine.browser.isIE6 || jasmine.browser.isIE7 || jasmine.browser.isIE8) {
527 w.FooTest5 = undefined;
533 it("should create lower level namespaces without first defining the top level", function() {
534 Ext.ClassManager.createNamespaces('FooTest6.ns1', 'FooTest7.ns2');
536 expect(w.FooTest6).toBeDefined();
537 expect(w.FooTest6.ns1).toBeDefined();
538 expect(w.FooTest7).toBeDefined();
539 expect(w.FooTest7.ns2).toBeDefined();
541 if (jasmine.browser.isIE6 || jasmine.browser.isIE7 || jasmine.browser.isIE8) {
542 w.FooTest6 = undefined;
543 w.FooTest7 = undefined;
550 it("should create a lower level namespace without defining the middle level", function() {
551 Ext.ClassManager.createNamespaces('FooTest8', 'FooTest8.ns1.ns2');
553 expect(w.FooTest8).toBeDefined();
554 expect(w.FooTest8.ns1).toBeDefined();
555 expect(w.FooTest8.ns1.ns2).toBeDefined();
557 if (jasmine.browser.isIE6 || jasmine.browser.isIE7 || jasmine.browser.isIE8) {
558 w.FooTest8 = undefined;
564 it ("should not overwritte existing namespace", function() {
565 Ext.ClassManager.createNamespaces('FooTest9');
567 FooTest9.prop1 = 'foo';
569 Ext.ClassManager.createNamespaces('FooTest9');
571 expect(FooTest9.prop1).toEqual("foo");
573 if (jasmine.browser.isIE6 || jasmine.browser.isIE7 || jasmine.browser.isIE8) {
574 w.FooTest9 = undefined;