PageRenderTime 32482ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/ext-4.1.0_b3/src/core/test/unit/spec/EventManager.js

https://bitbucket.org/srogerf/javascript
JavaScript | 641 lines | 524 code | 116 blank | 1 comment | 14 complexity | ea23b699462d6880b3063513ee0bc22a MD5 | raw file
  1. describe("Ext.EventManager", function() {
  2. var element,
  3. elementWithId,
  4. elementWithoutId;
  5. beforeEach(function() {
  6. // add global variable in whitelist
  7. addGlobal("id");
  8. addGlobal("ExtBox1");
  9. element = document.body;
  10. elementWithId = document.createElement("DIV");
  11. elementWithId.id = 'element-with-id';
  12. element.appendChild(elementWithId);
  13. elementWithoutId = document.createElement("DIV");
  14. element.appendChild(elementWithoutId);
  15. });
  16. afterEach(function() {
  17. var el = Ext.get(elementWithId);
  18. if (el) {
  19. el.remove();
  20. }
  21. el = Ext.get(elementWithoutId);
  22. if (el) {
  23. el.remove();
  24. }
  25. });
  26. describe("event binding", function() {
  27. describe("getId", function() {
  28. describe("if element has an id", function() {
  29. it("should return element id", function() {
  30. expect(elementWithId.id).toBe(Ext.EventManager.getId(elementWithId));
  31. });
  32. });
  33. describe("if element hasn't an id", function() {
  34. var id, result;
  35. beforeEach(function() {
  36. id = 'nf-42';
  37. spyOn(Ext, "id").andCallFake(function(dom) {
  38. return (dom.id = id);
  39. });
  40. result = Ext.EventManager.getId(elementWithoutId);
  41. });
  42. it("should add an id to element with Ext.id", function() {
  43. expect(elementWithoutId.id).toBe(id);
  44. });
  45. it("should return the element id", function() {
  46. expect(result).toBe(id);
  47. });
  48. it("should add element to Ext.cache", function() {
  49. expect(Ext.cache[id].el.dom).toBe(elementWithoutId);
  50. });
  51. });
  52. describe("document and window", function() {
  53. describe("document", function() {
  54. var result;
  55. beforeEach(function() {
  56. result = Ext.EventManager.getId(document);
  57. });
  58. it("should add document Ext.Element to cache", function() {
  59. expect(Ext.cache[Ext.documentId].el.dom).toBe(document);
  60. });
  61. it("should enable skipGarbageCollection flag", function() {
  62. expect(Ext.cache[Ext.documentId].skipGarbageCollection).toBe(true);
  63. });
  64. it("should return Ext.documentId", function() {
  65. expect(result).toBe(Ext.documentId);
  66. });
  67. });
  68. describe("window", function() {
  69. var result;
  70. beforeEach(function() {
  71. result = Ext.EventManager.getId(window);
  72. });
  73. it("should add window Ext.Element to cache", function() {
  74. expect(Ext.cache[Ext.windowId].el.dom).toBe(window);
  75. });
  76. it("should enable skipGarbageCollection flag", function() {
  77. expect(Ext.cache[Ext.windowId].skipGarbageCollection).toBe(true);
  78. });
  79. it("should return Ext.windowId", function() {
  80. expect(result).toBe(Ext.windowId);
  81. });
  82. });
  83. });
  84. });
  85. describe("prepareListenerConfig", function() {
  86. var config, configWithFn;
  87. beforeEach(function() {
  88. config = {
  89. click: Ext.emptyFn,
  90. scope: fakeScope
  91. };
  92. configWithFn = {
  93. click: {
  94. fn: Ext.emptyFn,
  95. scope: fakeScope
  96. }
  97. };
  98. spyOn(Ext.EventManager, "removeListener");
  99. spyOn(Ext.EventManager, "addListener");
  100. });
  101. describe("if remove", function() {
  102. describe("with an object like click: function(){}, scope: this", function() {
  103. it("should call removeListener", function() {
  104. Ext.EventManager.prepareListenerConfig(element, config, true);
  105. expect(Ext.EventManager.removeListener).toHaveBeenCalledWith(element, 'click', Ext.emptyFn, fakeScope, config);
  106. });
  107. });
  108. describe("with an object like click: {fn: function(){}, scope: this}", function() {
  109. it("should call removeListener", function() {
  110. Ext.EventManager.prepareListenerConfig(element, configWithFn, true);
  111. expect(Ext.EventManager.removeListener).toHaveBeenCalledWith(element, 'click', Ext.emptyFn, fakeScope, configWithFn.click);
  112. });
  113. });
  114. });
  115. describe("if add", function() {
  116. describe("with an object like click: function(){}, scope: this", function() {
  117. it("should call addListener", function() {
  118. Ext.EventManager.prepareListenerConfig(element, config);
  119. expect(Ext.EventManager.addListener).toHaveBeenCalledWith(element, 'click', Ext.emptyFn, fakeScope, config);
  120. });
  121. });
  122. describe("with an object like click: {fn: function(){}, scope: this}", function() {
  123. it("should call addListener", function() {
  124. Ext.EventManager.prepareListenerConfig(element, configWithFn);
  125. expect(Ext.EventManager.addListener).toHaveBeenCalledWith(element, 'click', Ext.emptyFn, fakeScope, configWithFn.click);
  126. });
  127. });
  128. });
  129. });
  130. describe("addListener", function() {
  131. describe("if eventName is an object", function() {
  132. var eventName;
  133. beforeEach(function() {
  134. eventName = {};
  135. });
  136. it("should call prepareListenerConfig", function() {
  137. spyOn(Ext.EventManager, "prepareListenerConfig");
  138. Ext.EventManager.addListener(element, eventName);
  139. expect(Ext.EventManager.prepareListenerConfig).toHaveBeenCalledWith(element, eventName);
  140. });
  141. });
  142. describe("event firing", function() {
  143. var config;
  144. beforeEach(function() {
  145. config = {
  146. click: {
  147. fn: jasmine.createSpy(),
  148. scope: fakeScope
  149. }
  150. };
  151. Ext.EventManager.addListener(element, config);
  152. });
  153. afterEach(function() {
  154. Ext.EventManager.removeListener(element, config);
  155. });
  156. it("should call the listener", function() {
  157. jasmine.fireMouseEvent(element, "click", 1, 1);
  158. expect(config.click.fn).toHaveBeenCalled();
  159. });
  160. });
  161. describe("options", function() {
  162. var config;
  163. afterEach(function() {
  164. Ext.EventManager.removeListener(element, config);
  165. });
  166. describe("scope", function() {
  167. it("should call the listener with the correct scope", function() {
  168. config = {
  169. click: {
  170. fn: jasmine.createSpy(),
  171. scope: fakeScope
  172. }
  173. };
  174. Ext.EventManager.addListener(element, config);
  175. jasmine.fireMouseEvent(element, "click", 1, 1);
  176. expect(config.click.fn.calls[0].object).toBe(fakeScope);
  177. });
  178. });
  179. describe("delegate", function() {
  180. var child;
  181. beforeEach(function() {
  182. config = {
  183. click: {
  184. fn: jasmine.createSpy(),
  185. delegate: ".something",
  186. scope: fakeScope
  187. }
  188. };
  189. child = Ext.get(element).createChild({
  190. cls: "child"
  191. }).dom;
  192. });
  193. afterEach(function() {
  194. var elChild = Ext.get(child);
  195. if (elChild) {
  196. elChild.remove();
  197. }
  198. });
  199. describe("if filter found nothing", function() {
  200. it("should not call the listener", function() {
  201. Ext.EventManager.addListener(element, config);
  202. jasmine.fireMouseEvent(child, "click", 1, 1);
  203. expect(config.click.fn).not.toHaveBeenCalled();
  204. });
  205. });
  206. describe("if filter found something", function() {
  207. it("should call the listener", function() {
  208. Ext.get(child).addCls("something");
  209. Ext.EventManager.addListener(element, config);
  210. jasmine.fireMouseEvent(child, "click", 1, 1);
  211. expect(config.click.fn).toHaveBeenCalled();
  212. });
  213. });
  214. describe("stopevent", function() {
  215. var checkbox;
  216. beforeEach(function() {
  217. config = {
  218. click: {
  219. fn: jasmine.createSpy(),
  220. stopEvent: true,
  221. scope: fakeScope
  222. }
  223. };
  224. checkbox = Ext.get(element).createChild({
  225. tag: "input",
  226. type: "checkbox"
  227. }).dom;
  228. Ext.EventManager.addListener(element, config);
  229. Ext.EventManager.addListener(checkbox, config);
  230. if (jasmine.browser.isIE) {
  231. checkbox.click();
  232. } else {
  233. jasmine.fireMouseEvent(checkbox, "click", 1, 1);
  234. }
  235. });
  236. afterEach(function() {
  237. var checkBoxEl = Ext.get(checkbox);
  238. if (checkBoxEl) {
  239. checkBoxEl.remove();
  240. }
  241. Ext.EventManager.removeListener(checkbox, config);
  242. });
  243. it("should stop propagation to parent elements", function() {
  244. expect(config.click.fn.calls.length).toEqual(1);
  245. });
  246. it("should prevent default browser handling of the event", function() {
  247. expect(checkbox.checked).toBe(false);
  248. });
  249. });
  250. describe("preventDefault", function() {
  251. var checkbox;
  252. beforeEach(function() {
  253. config = {
  254. click: {
  255. fn: jasmine.createSpy(),
  256. preventDefault: true,
  257. scope: fakeScope
  258. }
  259. };
  260. checkbox = Ext.get(element).createChild({
  261. tag: "input",
  262. type: "checkbox"
  263. }).dom;
  264. Ext.EventManager.addListener(element, config);
  265. Ext.EventManager.addListener(checkbox, config);
  266. if (jasmine.browser.isIE) {
  267. checkbox.click();
  268. } else {
  269. jasmine.fireMouseEvent(checkbox, "click", 1, 1);
  270. }
  271. });
  272. afterEach(function() {
  273. var checkBoxEl = Ext.get(checkbox);
  274. if (checkBoxEl) {
  275. checkBoxEl.remove();
  276. }
  277. Ext.EventManager.removeListener(checkbox, config);
  278. });
  279. it("should prevent default browser handling of the event", function() {
  280. expect(checkbox.checked).toBe(false);
  281. });
  282. it("should not stop the propagation of the event", function() {
  283. expect(config.click.fn.calls.length).toEqual(2);
  284. });
  285. });
  286. describe("stopPropagation", function() {
  287. var checkbox;
  288. beforeEach(function() {
  289. config = {
  290. click: {
  291. fn: jasmine.createSpy(),
  292. stopPropagation: true,
  293. scope: fakeScope
  294. }
  295. };
  296. checkbox = Ext.getBody().createChild({
  297. tag: "input",
  298. type: "checkbox"
  299. }).dom;
  300. Ext.EventManager.addListener(element, config);
  301. Ext.EventManager.addListener(checkbox, config);
  302. if (jasmine.browser.isIE) {
  303. checkbox.click();
  304. } else {
  305. jasmine.fireMouseEvent(checkbox, "click", 1, 1);
  306. }
  307. });
  308. afterEach(function() {
  309. var checkBoxEl = Ext.get(checkbox);
  310. if (checkBoxEl) {
  311. checkBoxEl.remove();
  312. }
  313. Ext.EventManager.removeListener(checkbox, config);
  314. });
  315. it("should stop propagation to parent elements", function() {
  316. expect(config.click.fn.calls.length).toEqual(1);
  317. });
  318. it("should not prevent default browser handling of the event", function() {
  319. expect(checkbox.checked).toBe(true);
  320. });
  321. });
  322. describe("normalized", function() {
  323. var event;
  324. beforeEach(function() {
  325. config = {
  326. click: {
  327. fn: jasmine.createSpy(),
  328. normalized: false,
  329. scope: fakeScope
  330. }
  331. };
  332. Ext.EventManager.addListener(element, config);
  333. event = jasmine.fireMouseEvent(element, "click", 1, 1);
  334. });
  335. it("should pass a browser event instead of an Ext.EventObject", function() {
  336. expect(config.click.fn).toHaveBeenCalledWith(event, element, config.click);
  337. });
  338. });
  339. describe("delay", function() {
  340. beforeEach(function() {
  341. config = {
  342. click: {
  343. fn: jasmine.createSpy(),
  344. delay: 1,
  345. scope: fakeScope
  346. }
  347. };
  348. Ext.EventManager.addListener(element, config);
  349. });
  350. it("should not call listener before 1 ms", function() {
  351. jasmine.fireMouseEvent(element, "click", 1, 1);
  352. expect(config.click.fn).not.toHaveBeenCalled();
  353. });
  354. it("should call listener after 1 ms", function() {
  355. runs(function() {
  356. jasmine.fireMouseEvent(element, "click", 1, 1);
  357. });
  358. waits(1);
  359. runs(function() {
  360. expect(config.click.fn).toHaveBeenCalled();
  361. });
  362. });
  363. });
  364. describe("single", function() {
  365. beforeEach(function() {
  366. config = {
  367. click: {
  368. fn: jasmine.createSpy(),
  369. single: true,
  370. scope: fakeScope
  371. }
  372. };
  373. Ext.EventManager.addListener(element, config);
  374. jasmine.fireMouseEvent(element, "click", 1, 1);
  375. jasmine.fireMouseEvent(element, "click", 1, 1);
  376. });
  377. it("should call listener only one time", function() {
  378. expect(config.click.fn.calls.length).toEqual(1);
  379. });
  380. });
  381. describe("buffer", function() {
  382. beforeEach(function() {
  383. config = {
  384. click: {
  385. fn: jasmine.createSpy(),
  386. buffer: 1,
  387. scope: fakeScope
  388. }
  389. };
  390. Ext.EventManager.addListener(element, config);
  391. jasmine.fireMouseEvent(element, "click", 1, 1);
  392. jasmine.fireMouseEvent(element, "click", 1, 1);
  393. });
  394. it("should call listener only one time", function() {
  395. waits(1);
  396. runs(function() {
  397. expect(config.click.fn.calls.length).toEqual(1);
  398. });
  399. });
  400. });
  401. describe("target", function() {
  402. var child;
  403. beforeEach(function() {
  404. child = Ext.get(element).createChild({
  405. }).dom;
  406. config = {
  407. click: {
  408. fn: jasmine.createSpy(),
  409. target: element,
  410. scope: fakeScope
  411. }
  412. };
  413. Ext.EventManager.addListener(element, config);
  414. });
  415. afterEach(function() {
  416. var childEl = Ext.get(child);
  417. if (childEl) {
  418. childEl.remove();
  419. }
  420. });
  421. it("should call listener if element event is fired", function() {
  422. jasmine.fireMouseEvent(element, "click", 1, 1);
  423. expect(config.click.fn).toHaveBeenCalled();
  424. });
  425. it("should not call listener if child event is fired (no bubbling)", function() {
  426. jasmine.fireMouseEvent(child, "click", 1, 1);
  427. expect(config.click.fn).not.toHaveBeenCalled();
  428. });
  429. });
  430. });
  431. });
  432. });
  433. describe("removeListener", function() {
  434. describe("if eventName is an object", function() {
  435. it("should call prepareListenerConfig", function() {
  436. var eventName = {};
  437. spyOn(Ext.EventManager, "prepareListenerConfig");
  438. Ext.EventManager.removeListener(element, eventName);
  439. expect(Ext.EventManager.prepareListenerConfig).toHaveBeenCalledWith(element, eventName, true);
  440. });
  441. });
  442. describe("event firing", function() {
  443. var config;
  444. beforeEach(function() {
  445. config = {
  446. click: {
  447. fn: jasmine.createSpy(),
  448. scope: fakeScope
  449. }
  450. };
  451. Ext.EventManager.addListener(element, config);
  452. Ext.EventManager.removeListener(element, config);
  453. });
  454. it("should not call the listener", function() {
  455. jasmine.fireMouseEvent(element, "click", 1, 1);
  456. expect(config.click.fn).not.toHaveBeenCalled();
  457. });
  458. });
  459. });
  460. describe("removeAll", function() {
  461. var config;
  462. beforeEach(function() {
  463. config = {
  464. click: {
  465. fn: jasmine.createSpy(),
  466. scope: fakeScope
  467. },
  468. mouseover: {
  469. fn: jasmine.createSpy(),
  470. scope: fakeScope
  471. }
  472. };
  473. Ext.EventManager.addListener(element, config);
  474. Ext.EventManager.removeAll(element, config);
  475. });
  476. it("should should not call click listener", function() {
  477. jasmine.fireMouseEvent(element, "click", 1, 1);
  478. expect(config.click.fn).not.toHaveBeenCalled();
  479. });
  480. it("should not call mouseover listener", function() {
  481. jasmine.fireMouseEvent(element, "mouseover", 1, 1);
  482. expect(config.mouseover.fn).not.toHaveBeenCalled();
  483. });
  484. });
  485. describe("purgeElement", function() {
  486. var child, config;
  487. beforeEach(function() {
  488. child = Ext.get(element).createChild({
  489. cls: "child"
  490. }).dom;
  491. config = {
  492. mouseover: {
  493. fn: jasmine.createSpy(),
  494. scope: fakeScope
  495. }
  496. };
  497. Ext.EventManager.addListener(element, config);
  498. Ext.EventManager.addListener(child, config);
  499. Ext.EventManager.purgeElement(element);
  500. });
  501. afterEach(function() {
  502. var childEl = Ext.get(child);
  503. if (childEl) {
  504. childEl.remove();
  505. }
  506. });
  507. it("should remove all listeners from element", function() {
  508. jasmine.fireMouseEvent(element, "mouseover", 1, 1);
  509. expect(config.mouseover.fn).not.toHaveBeenCalled();
  510. });
  511. it("should remove all listeners from element children", function() {
  512. jasmine.fireMouseEvent(child, "mouseover", 1, 1);
  513. expect(config.mouseover.fn).not.toHaveBeenCalled();
  514. });
  515. });
  516. describe("methods alias", function() {
  517. it("should alias addListener with on", function() {
  518. expect(Ext.EventManager.on).toBe(Ext.EventManager.addListener);
  519. });
  520. it("should alias removeListener with un", function() {
  521. expect(Ext.EventManager.un).toBe(Ext.EventManager.removeListener);
  522. });
  523. });
  524. });
  525. });