1 describe("Ext-mess", function() {
2 describe("Ext.id", function(){
4 describe("if element passed as first argument is different of document or window", function() {
5 beforeEach(function() {
6 el = document.createElement("div"); // do not use the Ext.getBody().createChild() method because it call Ext.id
7 Ext.getBody().appendChild(el);
10 it("should generate an unique id for the element with default prefix ext-gen", function() {
11 expect(Ext.id(el)).toEqual("ext-gen" + Ext.idSeed);
14 it("should generate an unique id for the element with custom prefix", function() {
15 var prefix = "nico-yhwh";
16 expect(Ext.id(el, prefix)).toEqual(prefix + Ext.idSeed);
19 it("should not override existing id", function() {
22 expect(Ext.id(el)).toEqual(id);
26 describe("if element passed as first argument is document", function() {
27 it("should return Ext.documentId", function() {
28 expect(Ext.id(document)).toEqual(Ext.documentId);
32 describe("if element passed as first argument is window", function() {
33 it("should return Ext.windowId", function() {
34 expect(Ext.id(window)).toEqual(Ext.windowId);
39 xdescribe("Ext.repaint", function() {
40 it("should create a mask in the body", function(){
41 var body = Ext.getBody();
43 spyOn(Ext, "getBody").andCallThrough();
44 spyOn(body, "createChild").andCallThrough();
48 expect(Ext.getBody).toHaveBeenCalled();
49 expect(body.createChild).toHaveBeenCalledWith({cls: "x-mask x-mask-transparent", tag: "div"});
53 describe("Ext.destroy", function() {
56 beforeEach(function() {
57 o1 = jasmine.createSpyObj("o1", ["destroy"]);
59 o2 = jasmine.createSpyObj("o2", ["destroy"]);
61 o3 = jasmine.createSpyObj("o3", ["dest"]);
65 it("should destroy an object", function() {
68 expect(o1.destroy).toHaveBeenCalled();
71 it("should no destroy an object without a destroy method", function() {
74 expect(o3.dest).not.toHaveBeenCalled();
77 it("should destroy an array of objects", function() {
78 Ext.destroy([o1, o2, o3]);
80 expect(o1.destroy).toHaveBeenCalled();
81 expect(o2.destroy).toHaveBeenCalled();
82 expect(o3.dest).not.toHaveBeenCalled();
85 it("should destroy multiple objects", function() {
86 Ext.destroy(o1, o2, o3);
88 expect(o1.destroy).toHaveBeenCalled();
89 expect(o2.destroy).toHaveBeenCalled();
90 expect(o3.dest).not.toHaveBeenCalled();
93 it("should remove dom if object is an Ext.element", function() {
94 var el = Ext.getBody().createChild({id: "to_destroy"});
98 expect(Ext.fly("to_destroy")).toBeNull();
102 describe("Ext.each", function() {
105 beforeEach(function() {
106 itFn = jasmine.createSpy("on itFn");
109 describe("each object", function() {
112 beforeEach(function() {
120 describe("if itFn does not return false", function() {
121 beforeEach(function() {
125 it("should call the each function 3 times", function () {
126 expect(itFn.callCount).toEqual(3);
129 it("should call the each function with correct arguments", function () {
130 expect(itFn.calls[0].args).toEqual(["n1", 11, o]);
131 expect(itFn.calls[1].args).toEqual(["n2", 13, o]);
132 expect(itFn.calls[2].args).toEqual(["n3", 18, o]);
136 describe("if itFn return false", function() {
137 beforeEach(function() {
138 itFn.andReturn(false);
142 it("should stop iteration if function return false", function() {
143 itFn.andReturn(false);
145 expect(itFn.calls.length).toEqual(1);
150 describe("do nothing on an empty object", function() {
153 beforeEach(function() {
158 it("should not call the each function", function () {
159 expect(itFn).not.toHaveBeenCalled();
164 describe("each array", function() {
167 beforeEach(function() {
171 describe("if itFn does not return false", function() {
172 beforeEach(function() {
176 it("should call the each function 4 times", function () {
177 expect(itFn.callCount).toEqual(4);
180 it("should call the each function with correct arguments", function () {
181 expect(itFn.calls[0].args).toEqual([6, 0, arr]);
182 expect(itFn.calls[1].args).toEqual([7, 1, arr]);
183 expect(itFn.calls[2].args).toEqual([8, 2, arr]);
184 expect(itFn.calls[3].args).toEqual([9, 3, arr]);
188 describe("if itFn return false", function() {
189 beforeEach(function() {
190 itFn.andReturn(false);
194 it("should stop iteration if function return false", function() {
195 itFn.andReturn(false);
197 expect(itFn.calls.length).toEqual(1);
202 describe("do nothing on an empty array", function() {
205 beforeEach(function() {
210 it("should not call the each function", function () {
211 expect(itFn).not.toHaveBeenCalled();
216 describe("simple cases", function() {
217 it("should work with an array of numbers", function() {
220 Ext.each([1, 2, 3, 4], function(val){
224 expect(sum).toEqual(10);
227 it("should work with an array of strings", function() {
230 Ext.each(["S", "e", "n", "c", "h", "a"], function(s){
234 expect(str).toEqual("Sencha");
237 it("should pass index correctly", function() {
239 Ext.each([1, 2, 3, 4, 5, 6], function(val, idx){
242 expect(arr).toEqual([0, 1, 2, 3, 4, 5]);
245 it("should work with a non array parameter", function() {
248 Ext.each(5, function(num){
252 expect(sum).toEqual(5);
256 describe("more complex cases that need a spy", function() {
259 beforeEach(function() {
260 eachFn = jasmine.createSpy();
263 it("should not run eachFn with an empty array", function() {
264 Ext.each([], eachFn);
266 expect(eachFn).not.toHaveBeenCalled();
269 it("should not run eachFn with null as first param", function() {
270 Ext.each(null, eachFn);
272 expect(eachFn).not.toHaveBeenCalled();
275 it("should iterate over NodeLists", function() {
276 Ext.each(document.getElementsByTagName('body'), eachFn);
278 expect(eachFn).toHaveBeenCalled();
281 it("should stop when function called with each item return false", function() {
282 eachFn.andCallFake(function(v) {
288 Ext.each([1, 2, 3, 4, 5, 6], eachFn);
290 expect(eachFn.callCount).toEqual(5);
293 it("should runfunction called with each item with correct scope", function() {
295 Ext.each([1, 2], eachFn, fakeScope);
297 expect(eachFn.calls[0].object).toBe(fakeScope);
298 expect(eachFn.calls[1].object).toBe(fakeScope);
305 describe("Ext.iterate", function() {
308 beforeEach(function() {
309 itFn = jasmine.createSpy();
312 describe("iterate object", function() {
315 beforeEach(function() {
323 describe("if itFn does not return false", function() {
324 beforeEach(function() {
325 Ext.iterate(o, itFn);
328 it("should call the iterate function 3 times", function () {
329 expect(itFn.callCount).toEqual(3);
332 it("should call the iterate function with correct arguments", function () {
333 expect(itFn.calls[0].args).toEqual(["n1", 11, o]);
334 expect(itFn.calls[1].args).toEqual(["n2", 13, o]);
335 expect(itFn.calls[2].args).toEqual(["n3", 18, o]);
339 describe("if itFn return false", function() {
340 beforeEach(function() {
341 itFn.andReturn(false);
342 Ext.iterate(o, itFn);
345 it("should stop iteration if function return false", function() {
346 itFn.andReturn(false);
348 expect(itFn.calls.length).toEqual(1);
353 describe("do nothing on an empty object", function() {
356 beforeEach(function() {
358 Ext.iterate(o, itFn);
361 it("should not call the iterate function", function () {
362 expect(itFn).not.toHaveBeenCalled();
367 describe("iterate array", function() {
370 beforeEach(function() {
374 describe("if itFn does not return false", function() {
375 beforeEach(function() {
376 Ext.iterate(arr, itFn);
379 it("should call the iterate function 4 times", function () {
380 expect(itFn.callCount).toEqual(4);
383 it("should call the iterate function with correct arguments", function () {
384 expect(itFn.calls[0].args).toEqual([6, 0, arr]);
385 expect(itFn.calls[1].args).toEqual([7, 1, arr]);
386 expect(itFn.calls[2].args).toEqual([8, 2, arr]);
387 expect(itFn.calls[3].args).toEqual([9, 3, arr]);
391 describe("if itFn return false", function() {
392 beforeEach(function() {
393 itFn.andReturn(false);
394 Ext.iterate(arr, itFn);
397 it("should stop iteration if function return false", function() {
398 itFn.andReturn(false);
400 expect(itFn.calls.length).toEqual(1);
405 describe("do nothing on an empty array", function() {
408 beforeEach(function() {
410 Ext.iterate(arr, itFn);
413 it("should not call the iterate function", function () {
414 expect(itFn).not.toHaveBeenCalled();
421 describe("Ext.num", function() {
422 it("should work with an integer", function() {
423 expect(Ext.num(3)).toEqual(3);
426 it("should work with a negative integer", function() {
427 expect(Ext.num(-7)).toEqual(-7);
430 it("should work with a float", function() {
431 expect(Ext.num(5.43)).toEqual(5.43);
434 it("should work with a negative float", function() {
435 expect(Ext.num(-9.8)).toEqual(-9.8);
438 it("should work with Math.PI", function() {
439 expect(Ext.num(Math.PI)).toEqual(Math.PI);
442 it("should return undefined with null", function() {
443 expect(Ext.num(null)).toBeUndefined();
446 it("should work with null, with defaults", function() {
447 expect(Ext.num(null, 4)).toEqual(4);
450 it("should return undefined with undefined", function() {
451 expect(Ext.num(undefined)).toBeUndefined();
454 it("should work with undefined, with defaults", function() {
455 expect(Ext.num(undefined, 42)).toEqual(42);
458 it("should return undefined with boolean", function() {
459 expect(Ext.num(true)).toBeUndefined();
462 it("should work with boolean, with defaults", function() {
463 expect(Ext.num(true, 12)).toEqual(12);
466 it("should return undefined with empty string", function() {
467 expect(Ext.num("")).toBeUndefined();
470 it("should work with string argument in the form of a number", function() {
471 expect(Ext.num('666')).toEqual(666);
474 it("should return undefined with a string containing only spaces", function() {
475 expect(Ext.num(" ")).toBeUndefined();
478 it("should return undefined with non empty string", function() {
479 expect(Ext.num("foo")).toBeUndefined();
482 it("should return undefined with empty array", function() {
483 expect(Ext.num([])).toBeUndefined();
486 it("should return undefined with non empty array", function() {
487 expect(Ext.num([1, 2, 3])).toBeUndefined();
490 it("should return undefined with array with a single item", function() {
491 expect(Ext.num([3])).toBeUndefined();
495 describe("Ext.pluck", function() {
496 it("should return results", function() {
497 var results = Ext.pluck([{
508 expect(results).toEqual([11, 13, 18]);
512 describe("Ext.toArray", function() {
520 beforeEach(function() {
521 div = Ext.getBody().createChild({tag: "div"});
522 span1 = div.createChild({tag: "span"});
523 span2 = div.createChild({tag: "span"});
524 span3 = div.createChild({tag: "span"});
525 span4 = div.createChild({tag: "span"});
526 htmlCollection = div.dom.getElementsByTagName("span");
529 it("should convert iterable to an array", function() {
530 expect(Ext.toArray(htmlCollection)).toEqual([span1.dom, span2.dom, span3.dom, span4.dom]);
533 it("should convert a part of an iterable to an array", function() {
534 expect(Ext.toArray(htmlCollection, 1, 3)).toEqual([span2.dom, span3.dom]);
538 xdescribe("Ext.urlAppend", function() {
539 var url = "http://example.com/";
541 it("should manage question mark", function() {
542 expect(Ext.urlAppend(url, "test=1")).toEqual("http://example.com/?test=1");
545 it("should manage ampersand", function() {
546 expect(Ext.urlAppend(url+"?test=1","foo=2")).toEqual("http://example.com/?test=1&foo=2");
549 it("should return directly url if content is empty", function() {
550 expect(Ext.urlAppend(url)).toEqual(url);
554 xdescribe("Ext.urlDecode", function() {
555 it ("should return an empty object if string is empty", function (){
556 expect(Ext.urlDecode("")).toEqual({});
559 it("should decode 2 keys", function(){
560 expect(Ext.urlDecode("foo=1&bar=2")).toEqual({
566 it("should decode 2 keys, one of them an array (overwrite off)", function() {
567 expect(Ext.urlDecode("foo=1&bar=2&bar=3&bar=4", false)).toEqual({
573 it("should decode 2 keys, one of them an array (overwrite on)", function() {
574 expect(Ext.urlDecode("foo=1&bar=2&bar=3&bar=4", true)).toEqual({
581 xdescribe("Ext.urlEncode", function() {
582 it("should encode 2 keys", function() {
583 expect(Ext.urlEncode({
586 })).toEqual("foo=1&bar=2");
589 it("should encode 2 keys, one of them an array", function() {
590 expect(Ext.urlEncode({
593 })).toEqual("foo=1&bar=2&bar=3&bar=4");
596 it("should encode 2 keys, one of them an array, with pre: test=1", function() {
597 expect(Ext.urlEncode({
600 }, "test=1")).toEqual("test=1&foo=1&bar=2&bar=3&bar=4");
604 xdescribe("Ext.htmlEncode", function() {
605 it("should call Ext.String.htmlEncode", function() {
607 spyOn(Ext.util.Format, "htmlEncode");
609 expect(Ext.String.htmlEncode).toHaveBeenCalledWith(val);
613 xdescribe("Ext.htmlEncode", function() {
614 it("should call Ext.String.htmlDecode", function() {
616 spyOn(Ext.util.Format, "htmlDecode");
619 expect(Ext.String.htmlDecode).toHaveBeenCalledWith(val);
623 describe("Ext.getBody", function() {
624 it("should return current document body as an Ext.core.Element", function() {
625 expect(Ext.getBody()).toEqual(Ext.get(document.body)); // see initSandbox in DomSandBox.js for more info
629 describe("Ext.getHead", function() {
630 it("should return current document head as an Ext.core.Element", function() {
631 expect(Ext.getHead()).toEqual(Ext.get(document.getElementsByTagName("head")[0]));
635 describe("Ext.getDoc", function() {
636 it("should return the current HTML document object as an Ext.element", function() {
637 expect(Ext.getDoc()).toEqual(Ext.get(document));
641 describe("Ext.getOrientation", function() {
642 it("should return the current orientation of the mobile device", function() {
643 if (window.innerHeight > window.innerWidth) {
644 expect(Ext.getOrientation()).toEqual("portrait");
646 expect(Ext.getOrientation()).toEqual("landscape");
651 describe("Ext.getDom", function() {
654 beforeEach(function() {
655 el1 = Ext.getBody().createChild({id: "elone"});
658 it("should return a dom element if an Ext.element is passed as first argument", function() {
659 expect(Ext.getDom(el1)).toEqual(el1.dom);
662 it("should return a dom element if the string (id) passed as first argument", function() {
663 expect(Ext.getDom("elone")).toEqual(el1.dom);