1 describe("Ext.ClassManager", function() {
2 var manager = Ext.ClassManager,
3 cls, emptyFn = function(){};
7 beforeEach(function() {
8 manager.enableNamespaceParseCache = false;
11 Class: function(){console.log(11);},
12 Class1: function(){console.log(12);},
13 Class2: function(){console.log(13);}
16 AnotherClass: function(){console.log(21);},
17 AnotherClass1: function(){console.log(22);},
18 AnotherClass2: function(){console.log(23);}
23 afterEach(function() {
24 if (window.Something) {
25 window.Something = undefined;
29 window.My = undefined;
37 window.Test = undefined;
41 delete window.Something;
46 manager.enableNamespaceParseCache = true;
49 describe("parseNamespace", function() {
50 it("should return the broken-down namespace", function() {
51 var parts = manager.parseNamespace('Some.strange.alien.Namespace');
53 expect(parts).toEqual([Ext.global, 'Some', 'strange', 'alien', 'Namespace']);
56 it("should return the broken-down namespace with object rewrites", function() {
57 var parts = manager.parseNamespace('Ext.some.Namespace');
59 expect(parts).toEqual([Ext, 'some', 'Namespace']);
63 describe("exist", function() {
64 it("should return whether a single class exists", function() {
65 expect(manager.isCreated('My.notexisting.Class')).toBe(false);
66 expect(manager.isCreated('My.awesome.Class')).toBe(true);
70 describe("loader preprocessor", function() {
71 beforeEach(function() {
75 it("should load and replace string class names with objects", function() {
77 extend: 'My.awesome.Class',
79 name1: My.cool.AnotherClass,
80 name2: 'My.cool.AnotherClass1'
84 extend: My.awesome.Class,
86 name1: My.cool.AnotherClass,
87 name2: My.cool.AnotherClass1
93 spyOn(Ext.Loader, 'require').andCallFake(function(classes, fn) {
98 Ext.Class.getPreprocessor('loader').fn(cls, data, emptyFn, emptyFn);
100 expect(Ext.Loader.require).toHaveBeenCalled();
101 expect(classNames).toEqual(['My.awesome.Class', 'My.cool.AnotherClass1']);
102 expect(data).toEqual(expected);
106 describe("create", function() {
107 var subClass, parentClass, mixinClass1, mixinClass2, subSubClass;
109 beforeEach(function() {
110 mixinClass1 = manager.create('I.am.the.MixinClass1', {
112 mixinConfig: 'mixinConfig'
115 constructor: function() {
116 this.mixinConstructor1Called = true;
119 mixinProperty1: 'mixinProperty1',
121 mixinMethod1: function() {
122 this.mixinMethodCalled = true;
126 mixinClass2 = manager.create('I.am.the.MixinClass2', {
127 constructor: function() {
128 this.mixinConstructor2Called = true;
131 mixinProperty2: 'mixinProperty2',
133 mixinMethod2: function() {
134 this.mixinMethodCalled = true;
138 parentClass = manager.create('I.am.the.ParentClass', {
139 alias: ['parentclass', 'superclass'],
142 mixin1: 'I.am.the.MixinClass1'
149 abe: 'Abraham Elias',
152 hobbies: ['football', 'bowling']
155 onClassExtended: function(subClass, data) {
156 subClass.onClassExtendedCalled = true;
159 constructor: function(config) {
160 this.initConfig(config);
162 this.parentConstructorCalled = true;
164 this.mixins.mixin1.constructor.apply(this, arguments);
167 parentProperty: 'parentProperty',
169 parentMethod: function() {
170 this.parentMethodCalled = true;
174 subClass = manager.create('I.am.the.SubClass', {
177 extend: 'I.am.the.ParentClass',
180 mixin1: 'I.am.the.MixinClass1',
181 mixin2: 'I.am.the.MixinClass2'
187 jacky: 'Jacky Nguyen',
188 tommy: 'Tommy Maintz'
190 hobbies: ['sleeping', 'eating', 'movies'],
193 constructor: function() {
194 this.subConstrutorCalled = true;
196 this.superclass.constructor.apply(this, arguments);
198 this.mixins.mixin2.constructor.apply(this, arguments);
200 myOwnMethod: function() {
201 this.myOwnMethodCalled = true;
206 it("should create the namespace", function() {
207 expect(I).toBeDefined();
208 expect(I.am).toBeDefined();
209 expect(I.am.the).toBeDefined();
210 expect(I.am.the.SubClass).toBeDefined();
213 it("should get className", function() {
214 expect(Ext.getClassName(subClass)).toEqual('I.am.the.SubClass');
217 describe("addStatics", function() {
218 it("single with name - value arguments", function() {
221 subClass.addStatics({
222 staticMethod: function(){
227 expect(subClass.staticMethod).toBeDefined();
228 subClass.staticMethod();
230 expect(called).toBeTruthy();
233 it("multiple with object map argument", function() {
234 subClass.addStatics({
235 staticProperty: 'something',
236 staticMethod: function(){}
239 expect(subClass.staticProperty).toEqual('something');
240 expect(subClass.staticMethod).toBeDefined();
244 describe("mixin", function() {
245 it("should have all properties of mixins", function() {
246 var obj = new subClass();
247 expect(obj.mixinProperty1).toEqual('mixinProperty1');
248 expect(obj.mixinProperty2).toEqual('mixinProperty2');
249 expect(obj.mixinMethod1).toBeDefined();
250 expect(obj.mixinMethod2).toBeDefined();
251 expect(obj.config.mixinConfig).toEqual('mixinConfig');
255 describe("config", function() {
256 it("should merge properly", function() {
257 var obj = new subClass();
259 expect(obj.config).toEqual({
260 mixinConfig: 'mixinConfig',
264 abe: 'Abraham Elias',
266 jacky: 'Jacky Nguyen',
267 tommy: 'Tommy Maintz'
269 hobbies: ['sleeping', 'eating', 'movies'],
274 it("should apply default config", function() {
275 var obj = new subClass();
277 expect(obj.getName()).toEqual('subClass');
278 expect(obj.getIsCool()).toEqual(true);
279 expect(obj.getHobbies()).toEqual(['sleeping', 'eating', 'movies']);
282 it("should apply with supplied config", function() {
283 var obj = new subClass({
287 aaron: 'Aaron Conran'
291 expect(obj.getName()).toEqual('newName');
292 expect(obj.getIsCool()).toEqual(false);
293 expect(obj.getMembers().aaron).toEqual('Aaron Conran');
297 describe("overriden methods", function() {
298 it("should call self constructor", function() {
299 var obj = new subClass();
300 expect(obj.subConstrutorCalled).toBeTruthy();
303 it("should call parent constructor", function() {
304 var obj = new subClass();
305 expect(obj.parentConstructorCalled).toBeTruthy();
308 it("should call mixins constructors", function() {
309 var obj = new subClass();
310 expect(obj.mixinConstructor1Called).toBeTruthy();
311 expect(obj.mixinConstructor2Called).toBeTruthy();
315 describe("alias", function() {
316 it("should store alias", function() {
317 expect(manager.getByAlias('subclass')).toBe(subClass);
320 it("should store multiple aliases", function() {
321 expect(manager.getByAlias('parentclass')).toBe(parentClass);
322 expect(manager.getByAlias('superclass')).toBe(parentClass);
326 describe("onClassExtended", function() {
327 it("should store an internal reference", function() {
328 expect(parentClass.prototype.$onExtended).toBeDefined();
329 expect(subClass.prototype.$onExtended).toBeDefined();
332 it("should invoke the internal reference", function() {
333 expect(subClass.onClassExtendedCalled).toBe(true);
338 describe("instantiate", function() {
339 beforeEach(function() {
340 manager.create('Test.stuff.Person', {
343 constructor: function(name, age, sex) {
349 eat: function(food) {
350 this.eatenFood = food;
354 manager.create('Test.stuff.Developer', {
357 extend: 'Test.stuff.Person',
359 constructor: function(isGeek, name, age, sex) {
360 this.isGeek = isGeek;
362 return this.superclass.constructor.apply(this, arguments);
365 code: function(language) {
366 this.languageCoded = language;
373 it("should create the instance by full class name", function() {
374 var me = manager.instantiate('Test.stuff.Person', 'Jacky', 24, 'male');
375 expect(me instanceof Test.stuff.Person).toBe(true);
378 it("should create the instance by alias", function() {
379 var me = manager.instantiateByAlias('person', 'Jacky', 24, 'male');
380 expect(me instanceof Test.stuff.Person).toBe(true);
383 it("should pass all arguments to the constructor", function() {
384 var me = manager.instantiateByAlias('person', 'Jacky', 24, 'male');
385 expect(me.name).toBe('Jacky');
386 expect(me.age).toBe(24);
387 expect(me.sex).toBe('male');
390 it("should have all methods in prototype", function() {
391 var me = manager.instantiateByAlias('person', 'Jacky', 24, 'male');
394 expect(me.eatenFood).toBe('rice');
397 it("should works with inheritance", function() {
398 var me = manager.instantiateByAlias('developer', true, 'Jacky', 24, 'male');
399 me.code('javascript');
401 expect(me.languageCoded).toBe('javascript');
402 expect(me.eatenFood).toBe('bugs');
406 describe("post-processors", function() {
408 xdescribe("uses", function() {
409 //expect(Something.Cool).toBeDefined();
410 //expect(Something.Cool instanceof test).toBeTruthy();
413 describe("singleton", function() {
414 it("should create the instance namespace and return the class", function() {
415 var test = Ext.define('Something.Cool', {
417 someMethod: function() {
418 this.someMethodCalled = true;
420 someProperty: 'something'
423 expect(Something.Cool).toBeDefined();
424 expect(Something.Cool instanceof test).toBeTruthy();
428 describe("alias xtype", function() {
429 it("should set xtype as a static class property", function() {
430 var test = Ext.define('Something.Cool', {
434 expect(Something.Cool.xtype).toEqual('cool');
438 describe("alternate", function() {
439 it("should create the alternate", function() {
440 Ext.define('Something.Cool', {
441 alternateClassName: 'Something.CoolAsWell',
443 someMethod: function() {
444 this.someMethodCalled = true;
447 someProperty: 'something'
450 expect(Something.CoolAsWell).toBeDefined();
451 expect(Something.CoolAsWell).toBe(Something.Cool);
454 it("should create the alternate", function() {
455 Ext.define('Something.Cool', {
456 alternateClassName: ['Something.CoolAsWell', 'Something.AlsoCool']
459 expect(Something.CoolAsWell).toBe(Something.Cool);
460 expect(Something.AlsoCool).toBe(Something.Cool);