]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | describe("Ext.ClassManager", function() {\r |
2 | var manager = Ext.ClassManager,\r | |
3 | cls, emptyFn = function(){};\r | |
4 | \r | |
5 | \r | |
6 | \r | |
7 | beforeEach(function() {\r | |
8 | manager.enableNamespaceParseCache = false;\r | |
9 | window.My = {\r | |
10 | awesome: {\r | |
11 | Class: function(){console.log(11);},\r | |
12 | Class1: function(){console.log(12);},\r | |
13 | Class2: function(){console.log(13);}\r | |
14 | },\r | |
15 | cool: {\r | |
16 | AnotherClass: function(){console.log(21);},\r | |
17 | AnotherClass1: function(){console.log(22);},\r | |
18 | AnotherClass2: function(){console.log(23);}\r | |
19 | }\r | |
20 | };\r | |
21 | });\r | |
22 | \r | |
23 | afterEach(function() {\r | |
24 | try {\r | |
25 | delete window.Something;\r | |
26 | delete window.My;\r | |
27 | delete window.I;\r | |
28 | delete window.Test;\r | |
29 | } catch (e) {\r | |
30 | window.Something = undefined;\r | |
31 | window.My = undefined;\r | |
32 | window.I = undefined;\r | |
33 | window.Test = undefined;\r | |
34 | }\r | |
35 | manager.enableNamespaceParseCache = true;\r | |
36 | });\r | |
37 | \r | |
38 | describe("exist", function() {\r | |
39 | it("should return whether a single class exists", function() {\r | |
40 | expect(manager.isCreated('My.notexisting.Class')).toBe(false);\r | |
41 | expect(manager.isCreated('My.awesome.Class')).toBe(true);\r | |
42 | });\r | |
43 | });\r | |
44 | \r | |
45 | describe("loader preprocessor", function() {\r | |
46 | beforeEach(function() {\r | |
47 | cls = function(){};\r | |
48 | });\r | |
49 | \r | |
50 | it("should load and replace string class names with objects", function() {\r | |
51 | var data = {\r | |
52 | extend: 'My.awesome.Class',\r | |
53 | mixins: {\r | |
54 | name1: My.cool.AnotherClass,\r | |
55 | name2: 'My.cool.AnotherClass1'\r | |
56 | }\r | |
57 | },\r | |
58 | expected = {\r | |
59 | extend: My.awesome.Class,\r | |
60 | mixins: {\r | |
61 | name1: My.cool.AnotherClass,\r | |
62 | name2: My.cool.AnotherClass1\r | |
63 | }\r | |
64 | },\r | |
65 | classNames;\r | |
66 | \r | |
67 | \r | |
68 | spyOn(Ext.Loader, 'require').andCallFake(function(classes, fn) {\r | |
69 | classNames = classes;\r | |
70 | fn();\r | |
71 | });\r | |
72 | \r | |
73 | Ext.Class.getPreprocessor('loader').fn(cls, data, emptyFn, emptyFn);\r | |
74 | \r | |
75 | expect(Ext.Loader.require).toHaveBeenCalled();\r | |
76 | expect(classNames).toEqual(['My.awesome.Class', 'My.cool.AnotherClass1']);\r | |
77 | expect(data).toEqual(expected);\r | |
78 | });\r | |
79 | });\r | |
80 | \r | |
81 | describe("create", function() {\r | |
82 | var subClass, parentClass, mixinClass1, mixinClass2, subSubClass;\r | |
83 | \r | |
84 | beforeEach(function() {\r | |
85 | mixinClass1 = manager.create('I.am.the.MixinClass1', {\r | |
86 | config: {\r | |
87 | mixinConfig: 'mixinConfig'\r | |
88 | },\r | |
89 | \r | |
90 | constructor: function() {\r | |
91 | this.mixinConstructor1Called = true;\r | |
92 | },\r | |
93 | \r | |
94 | mixinProperty1: 'mixinProperty1',\r | |
95 | \r | |
96 | mixinMethod1: function() {\r | |
97 | this.mixinMethodCalled = true;\r | |
98 | }\r | |
99 | });\r | |
100 | \r | |
101 | mixinClass2 = manager.create('I.am.the.MixinClass2', {\r | |
102 | constructor: function() {\r | |
103 | this.mixinConstructor2Called = true;\r | |
104 | },\r | |
105 | \r | |
106 | mixinProperty2: 'mixinProperty2',\r | |
107 | \r | |
108 | mixinMethod2: function() {\r | |
109 | this.mixinMethodCalled = true;\r | |
110 | }\r | |
111 | });\r | |
112 | \r | |
113 | parentClass = manager.create('I.am.the.ParentClass', {\r | |
114 | alias: ['parentclass', 'superclass'],\r | |
115 | \r | |
116 | mixins: {\r | |
117 | mixin1: 'I.am.the.MixinClass1'\r | |
118 | },\r | |
119 | \r | |
120 | config: {\r | |
121 | name: 'parentClass',\r | |
122 | isCool: false,\r | |
123 | members: {\r | |
124 | abe: 'Abraham Elias',\r | |
125 | ed: 'Ed Spencer'\r | |
126 | },\r | |
127 | hobbies: ['football', 'bowling']\r | |
128 | },\r | |
129 | \r | |
130 | onClassExtended: function(subClass, data) {\r | |
131 | subClass.onClassExtendedCalled = true;\r | |
132 | },\r | |
133 | \r | |
134 | constructor: function(config) {\r | |
135 | this.initConfig(config);\r | |
136 | \r | |
137 | this.parentConstructorCalled = true;\r | |
138 | \r | |
139 | this.mixins.mixin1.constructor.apply(this, arguments);\r | |
140 | },\r | |
141 | \r | |
142 | parentProperty: 'parentProperty',\r | |
143 | \r | |
144 | parentMethod: function() {\r | |
145 | this.parentMethodCalled = true;\r | |
146 | }\r | |
147 | });\r | |
148 | \r | |
149 | subClass = manager.create('I.am.the.SubClass', {\r | |
150 | alias: 'subclass',\r | |
151 | \r | |
152 | extend: 'I.am.the.ParentClass',\r | |
153 | \r | |
154 | mixins: {\r | |
155 | mixin1: 'I.am.the.MixinClass1',\r | |
156 | mixin2: 'I.am.the.MixinClass2'\r | |
157 | },\r | |
158 | config: {\r | |
159 | name: 'subClass',\r | |
160 | isCool: true,\r | |
161 | members: {\r | |
162 | jacky: 'Jacky Nguyen',\r | |
163 | tommy: 'Tommy Maintz'\r | |
164 | },\r | |
165 | hobbies: ['sleeping', 'eating', 'movies'],\r | |
166 | isSpecial: true\r | |
167 | },\r | |
168 | constructor: function() {\r | |
169 | this.subConstrutorCalled = true;\r | |
170 | \r | |
171 | this.superclass.constructor.apply(this, arguments);\r | |
172 | \r | |
173 | this.mixins.mixin2.constructor.apply(this, arguments);\r | |
174 | },\r | |
175 | myOwnMethod: function() {\r | |
176 | this.myOwnMethodCalled = true;\r | |
177 | }\r | |
178 | });\r | |
179 | });\r | |
180 | \r | |
181 | afterEach(function() {\r | |
182 | Ext.undefine('I.am.the.MixinClass1');\r | |
183 | Ext.undefine('I.am.the.MixinClass2');\r | |
184 | Ext.undefine('I.am.the.ParentClass');\r | |
185 | Ext.undefine('I.am.the.SubClass');\r | |
186 | });\r | |
187 | \r | |
188 | it("should create the namespace", function() {\r | |
189 | expect(I).toBeDefined();\r | |
190 | expect(I.am).toBeDefined();\r | |
191 | expect(I.am.the).toBeDefined();\r | |
192 | expect(I.am.the.SubClass).toBeDefined();\r | |
193 | });\r | |
194 | \r | |
195 | it("should get className", function() {\r | |
196 | expect(Ext.getClassName(subClass)).toEqual('I.am.the.SubClass');\r | |
197 | });\r | |
198 | \r | |
199 | describe("addStatics", function() {\r | |
200 | it("single with name - value arguments", function() {\r | |
201 | var called = false;\r | |
202 | \r | |
203 | subClass.addStatics({\r | |
204 | staticMethod: function(){\r | |
205 | called = true;\r | |
206 | }\r | |
207 | });\r | |
208 | \r | |
209 | expect(subClass.staticMethod).toBeDefined();\r | |
210 | subClass.staticMethod();\r | |
211 | \r | |
212 | expect(called).toBeTruthy();\r | |
213 | });\r | |
214 | \r | |
215 | it("multiple with object map argument", function() {\r | |
216 | subClass.addStatics({\r | |
217 | staticProperty: 'something',\r | |
218 | staticMethod: function(){}\r | |
219 | });\r | |
220 | \r | |
221 | expect(subClass.staticProperty).toEqual('something');\r | |
222 | expect(subClass.staticMethod).toBeDefined();\r | |
223 | });\r | |
224 | });\r | |
225 | \r | |
226 | describe("mixin", function() {\r | |
227 | it("should have all properties of mixins", function() {\r | |
228 | var obj = new subClass();\r | |
229 | expect(obj.mixinProperty1).toEqual('mixinProperty1');\r | |
230 | expect(obj.mixinProperty2).toEqual('mixinProperty2');\r | |
231 | expect(obj.mixinMethod1).toBeDefined();\r | |
232 | expect(obj.mixinMethod2).toBeDefined();\r | |
233 | expect(obj.config.mixinConfig).toEqual('mixinConfig');\r | |
234 | });\r | |
235 | });\r | |
236 | \r | |
237 | describe("config", function() {\r | |
238 | it("should merge properly", function() {\r | |
239 | var obj = new subClass();\r | |
240 | \r | |
241 | expect(obj.config).toEqual({\r | |
242 | mixinConfig: 'mixinConfig',\r | |
243 | name: 'subClass',\r | |
244 | isCool: true,\r | |
245 | members: {\r | |
246 | abe: 'Abraham Elias',\r | |
247 | ed: 'Ed Spencer',\r | |
248 | jacky: 'Jacky Nguyen',\r | |
249 | tommy: 'Tommy Maintz'\r | |
250 | },\r | |
251 | hobbies: ['sleeping', 'eating', 'movies'],\r | |
252 | isSpecial: true\r | |
253 | });\r | |
254 | });\r | |
255 | \r | |
256 | it("should apply default config", function() {\r | |
257 | var obj = new subClass();\r | |
258 | \r | |
259 | expect(obj.getName()).toEqual('subClass');\r | |
260 | expect(obj.getIsCool()).toEqual(true);\r | |
261 | expect(obj.getHobbies()).toEqual(['sleeping', 'eating', 'movies']);\r | |
262 | });\r | |
263 | \r | |
264 | it("should apply with supplied config", function() {\r | |
265 | var obj = new subClass({\r | |
266 | name: 'newName',\r | |
267 | isCool: false,\r | |
268 | members: {\r | |
269 | aaron: 'Aaron Conran'\r | |
270 | }\r | |
271 | });\r | |
272 | \r | |
273 | expect(obj.getName()).toEqual('newName');\r | |
274 | expect(obj.getIsCool()).toEqual(false);\r | |
275 | expect(obj.getMembers().aaron).toEqual('Aaron Conran');\r | |
276 | });\r | |
277 | });\r | |
278 | \r | |
279 | describe("overriden methods", function() {\r | |
280 | it("should call self constructor", function() {\r | |
281 | var obj = new subClass();\r | |
282 | expect(obj.subConstrutorCalled).toBeTruthy();\r | |
283 | });\r | |
284 | \r | |
285 | it("should call parent constructor", function() {\r | |
286 | var obj = new subClass();\r | |
287 | expect(obj.parentConstructorCalled).toBeTruthy();\r | |
288 | });\r | |
289 | \r | |
290 | it("should call mixins constructors", function() {\r | |
291 | var obj = new subClass();\r | |
292 | expect(obj.mixinConstructor1Called).toBeTruthy();\r | |
293 | expect(obj.mixinConstructor2Called).toBeTruthy();\r | |
294 | });\r | |
295 | });\r | |
296 | \r | |
297 | describe("alias", function() {\r | |
298 | it("should store alias", function() {\r | |
299 | expect(manager.getByAlias('subclass')).toBe(subClass);\r | |
300 | });\r | |
301 | \r | |
302 | it("should store multiple aliases", function() {\r | |
303 | expect(manager.getByAlias('parentclass')).toBe(parentClass);\r | |
304 | expect(manager.getByAlias('superclass')).toBe(parentClass);\r | |
305 | });\r | |
306 | });\r | |
307 | });\r | |
308 | \r | |
309 | describe('define', function () {\r | |
310 | it('should allow anonymous classes', function () {\r | |
311 | var T = Ext.define(null, function (Self) {\r | |
312 | return {\r | |
313 | constructor: function () {\r | |
314 | this.foo = 1;\r | |
315 | this.T = Self;\r | |
316 | }\r | |
317 | }\r | |
318 | });\r | |
319 | \r | |
320 | var obj = new T();\r | |
321 | \r | |
322 | expect(obj.foo).toBe(1);\r | |
323 | expect(T).toBe(obj.self);\r | |
324 | expect(obj.T).toBe(T);\r | |
325 | expect(obj.$className).toBeNull();\r | |
326 | })\r | |
327 | });\r | |
328 | \r | |
329 | describe("instantiate", function() {\r | |
330 | beforeEach(function() {\r | |
331 | manager.create('Test.stuff.Person', {\r | |
332 | alias: 'person',\r | |
333 | \r | |
334 | constructor: function(name, age, sex) {\r | |
335 | this.name = name;\r | |
336 | this.age = age;\r | |
337 | this.sex = sex;\r | |
338 | },\r | |
339 | \r | |
340 | eat: function(food) {\r | |
341 | this.eatenFood = food;\r | |
342 | }\r | |
343 | });\r | |
344 | \r | |
345 | manager.create('Test.stuff.Developer', {\r | |
346 | alias: 'developer',\r | |
347 | \r | |
348 | extend: 'Test.stuff.Person',\r | |
349 | \r | |
350 | constructor: function(isGeek, name, age, sex) {\r | |
351 | this.isGeek = isGeek;\r | |
352 | \r | |
353 | return this.superclass.constructor.apply(this, arguments);\r | |
354 | },\r | |
355 | \r | |
356 | code: function(language) {\r | |
357 | this.languageCoded = language;\r | |
358 | this.eat('bugs');\r | |
359 | }\r | |
360 | });\r | |
361 | });\r | |
362 | \r | |
363 | afterEach(function() {\r | |
364 | Ext.undefine('Test.stuff.Person');\r | |
365 | Ext.undefine('Test.stuff.Developer');\r | |
366 | });\r | |
367 | \r | |
368 | it("should create the instance by full class name", function() {\r | |
369 | var me = Ext.create('Test.stuff.Person', 'Jacky', 24, 'male');\r | |
370 | expect(me instanceof Test.stuff.Person).toBe(true);\r | |
371 | });\r | |
372 | \r | |
373 | it("should create the instance by alias", function() {\r | |
374 | var me = manager.instantiateByAlias('person', 'Jacky', 24, 'male');\r | |
375 | expect(me instanceof Test.stuff.Person).toBe(true);\r | |
376 | });\r | |
377 | \r | |
378 | it("should pass all arguments to the constructor", function() {\r | |
379 | var me = manager.instantiateByAlias('person', 'Jacky', 24, 'male');\r | |
380 | expect(me.name).toBe('Jacky');\r | |
381 | expect(me.age).toBe(24);\r | |
382 | expect(me.sex).toBe('male');\r | |
383 | });\r | |
384 | \r | |
385 | it("should have all methods in prototype", function() {\r | |
386 | var me = manager.instantiateByAlias('person', 'Jacky', 24, 'male');\r | |
387 | me.eat('rice');\r | |
388 | \r | |
389 | expect(me.eatenFood).toBe('rice');\r | |
390 | });\r | |
391 | \r | |
392 | it("should works with inheritance", function() {\r | |
393 | var me = manager.instantiateByAlias('developer', true, 'Jacky', 24, 'male');\r | |
394 | me.code('javascript');\r | |
395 | \r | |
396 | expect(me.languageCoded).toBe('javascript');\r | |
397 | expect(me.eatenFood).toBe('bugs');\r | |
398 | });\r | |
399 | });\r | |
400 | \r | |
401 | describe("post-processors", function() {\r | |
402 | afterEach(function() {\r | |
403 | Ext.undefine('Something.Cool');\r | |
404 | });\r | |
405 | \r | |
406 | xdescribe("uses", function() {\r | |
407 | //expect(Something.Cool).toBeDefined();\r | |
408 | //expect(Something.Cool instanceof test).toBeTruthy();\r | |
409 | });\r | |
410 | \r | |
411 | describe("singleton", function() {\r | |
412 | it("should create the instance namespace and return the class", function() {\r | |
413 | var test = Ext.define('Something.Cool', {\r | |
414 | singleton: true,\r | |
415 | someMethod: function() {\r | |
416 | this.someMethodCalled = true;\r | |
417 | },\r | |
418 | someProperty: 'something'\r | |
419 | });\r | |
420 | \r | |
421 | expect(Something.Cool).toBeDefined();\r | |
422 | expect(Something.Cool instanceof test).toBeTruthy();\r | |
423 | });\r | |
424 | });\r | |
425 | \r | |
426 | describe("alias xtype", function() {\r | |
427 | it("should set xtype as a static class property", function() {\r | |
428 | var test = Ext.define('Something.Cool', {\r | |
429 | alias: 'widget.cool'\r | |
430 | });\r | |
431 | \r | |
432 | expect(Something.Cool.xtype).toEqual('cool');\r | |
433 | });\r | |
434 | });\r | |
435 | \r | |
436 | describe("alternate", function() {\r | |
437 | it("should create the alternate with a string for alternateClassName property", function() {\r | |
438 | Ext.define('Something.Cool', {\r | |
439 | alternateClassName: 'Something.CoolAsWell',\r | |
440 | \r | |
441 | someMethod: function() {\r | |
442 | this.someMethodCalled = true;\r | |
443 | },\r | |
444 | \r | |
445 | someProperty: 'something'\r | |
446 | });\r | |
447 | \r | |
448 | expect(Something.CoolAsWell).toBeDefined();\r | |
449 | expect(Something.CoolAsWell).toBe(Something.Cool);\r | |
450 | });\r | |
451 | \r | |
452 | it("should create the alternate with an array for alternateClassName property", function() {\r | |
453 | Ext.define('Something.Cool', {\r | |
454 | alternateClassName: ['Something.CoolAsWell', 'Something.AlsoCool']\r | |
455 | });\r | |
456 | \r | |
457 | expect(Something.CoolAsWell).toBe(Something.Cool);\r | |
458 | expect(Something.AlsoCool).toBe(Something.Cool);\r | |
459 | });\r | |
460 | });\r | |
461 | });\r | |
462 | \r | |
463 | describe("Ext.namespace", function() {\r | |
464 | var w = window;\r | |
465 | \r | |
466 | function undo (name) {\r | |
467 | if (Ext.isIE8) {\r | |
468 | w[name] = undefined;\r | |
469 | } else {\r | |
470 | delete w[name];\r | |
471 | }\r | |
472 | }\r | |
473 | \r | |
474 | it("should create a single top level namespace", function() {\r | |
475 | Ext.namespace('FooTest1');\r | |
476 | \r | |
477 | expect(w.FooTest1).toBeDefined();\r | |
478 | \r | |
479 | undo('FooTest1');\r | |
480 | });\r | |
481 | \r | |
482 | it("should create multiple top level namespace", function() {\r | |
483 | Ext.namespace('FooTest2', 'FooTest3', 'FooTest4');\r | |
484 | \r | |
485 | expect(w.FooTest2).toBeDefined();\r | |
486 | expect(w.FooTest3).toBeDefined();\r | |
487 | expect(w.FooTest4).toBeDefined();\r | |
488 | \r | |
489 | undo('FooTest2');\r | |
490 | undo('FooTest3');\r | |
491 | undo('FooTest4');\r | |
492 | });\r | |
493 | \r | |
494 | it("should create a chain of namespaces, starting from a top level", function() {\r | |
495 | Ext.namespace('FooTest5', 'FooTest5.ns1', 'FooTest5.ns1.ns2', 'FooTest5.ns1.ns2.ns3');\r | |
496 | \r | |
497 | expect(w.FooTest5).toBeDefined();\r | |
498 | expect(w.FooTest5.ns1).toBeDefined();\r | |
499 | expect(w.FooTest5.ns1.ns2).toBeDefined();\r | |
500 | expect(w.FooTest5.ns1.ns2.ns3).toBeDefined();\r | |
501 | \r | |
502 | undo('FooTest5');\r | |
503 | });\r | |
504 | \r | |
505 | it("should create lower level namespaces without first defining the top level", function() {\r | |
506 | Ext.namespace('FooTest6.ns1', 'FooTest7.ns2');\r | |
507 | \r | |
508 | expect(w.FooTest6).toBeDefined();\r | |
509 | expect(w.FooTest6.ns1).toBeDefined();\r | |
510 | expect(w.FooTest7).toBeDefined();\r | |
511 | expect(w.FooTest7.ns2).toBeDefined();\r | |
512 | \r | |
513 | undo('FooTest6');\r | |
514 | undo('FooTest7');\r | |
515 | });\r | |
516 | \r | |
517 | it("should create a lower level namespace without defining the middle level", function() {\r | |
518 | Ext.namespace('FooTest8', 'FooTest8.ns1.ns2');\r | |
519 | \r | |
520 | expect(w.FooTest8).toBeDefined();\r | |
521 | expect(w.FooTest8.ns1).toBeDefined();\r | |
522 | expect(w.FooTest8.ns1.ns2).toBeDefined();\r | |
523 | \r | |
524 | undo('FooTest8');\r | |
525 | });\r | |
526 | \r | |
527 | it ("should not overwritte existing namespace", function() {\r | |
528 | Ext.namespace('FooTest9');\r | |
529 | \r | |
530 | FooTest9.prop1 = 'foo';\r | |
531 | \r | |
532 | Ext.namespace('FooTest9');\r | |
533 | \r | |
534 | expect(FooTest9.prop1).toEqual("foo");\r | |
535 | \r | |
536 | undo('FooTest9');\r | |
537 | });\r | |
538 | });\r | |
539 | });\r |