domemittermixin.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, window, Event, MouseEvent */
  6. /* bender-tags: ui */
  7. import testUtils from 'tests/core/_utils/utils.js';
  8. import DOMEmitterMixin from 'ckeditor5/ui/domemittermixin.js';
  9. import EmitterMixin from 'ckeditor5/utils/emittermixin.js';
  10. testUtils.createSinonSandbox();
  11. describe( 'DOMEmitterMixin', () => {
  12. let emitter, domEmitter, node;
  13. beforeEach( () => {
  14. emitter = Object.create( EmitterMixin );
  15. domEmitter = Object.create( DOMEmitterMixin );
  16. node = document.createElement( 'div' );
  17. } );
  18. afterEach( () => {
  19. domEmitter.stopListening();
  20. } );
  21. describe( 'listenTo', () => {
  22. it( 'should listen to EmitterMixin events', () => {
  23. const spy = testUtils.sinon.spy();
  24. domEmitter.listenTo( emitter, 'test', spy );
  25. emitter.fire( 'test' );
  26. sinon.assert.calledOnce( spy );
  27. } );
  28. it( 'should listen to native DOM events', () => {
  29. const spy = testUtils.sinon.spy();
  30. domEmitter.listenTo( node, 'test', spy );
  31. node.dispatchEvent( new Event( 'test' ) );
  32. sinon.assert.calledOnce( spy );
  33. } );
  34. it( 'should listen to native DOM events - window as source', () => {
  35. const spy = testUtils.sinon.spy();
  36. domEmitter.listenTo( window, 'test', spy );
  37. window.dispatchEvent( new Event( 'test' ) );
  38. sinon.assert.calledOnce( spy );
  39. } );
  40. it( 'should listen to native DOM events - document as source', () => {
  41. const spy = testUtils.sinon.spy();
  42. domEmitter.listenTo( document, 'test', spy );
  43. document.dispatchEvent( new Event( 'test' ) );
  44. sinon.assert.calledOnce( spy );
  45. } );
  46. // #187
  47. it( 'should work for DOM Nodes belonging to another window', () => {
  48. const spy = testUtils.sinon.spy();
  49. const iframe = document.createElement( 'iframe' );
  50. document.body.appendChild( iframe );
  51. const iframeNode = iframe.contentWindow.document.createElement( 'div' );
  52. domEmitter.listenTo( iframeNode, 'test', spy );
  53. iframeNode.dispatchEvent( new Event( 'test' ) );
  54. sinon.assert.calledOnce( spy );
  55. } );
  56. describe( 'event capturing', () => {
  57. beforeEach( () => {
  58. document.body.appendChild( node );
  59. } );
  60. afterEach( () => {
  61. document.body.removeChild( node );
  62. } );
  63. it( 'should not use capturing at default', () => {
  64. const spy = testUtils.sinon.spy();
  65. domEmitter.listenTo( document, 'test', spy );
  66. node.dispatchEvent( new Event( 'test', { bubbles: false } ) );
  67. sinon.assert.notCalled( spy );
  68. } );
  69. it( 'should optionally use capturing', () => {
  70. const spy = testUtils.sinon.spy();
  71. domEmitter.listenTo( document, 'test', spy, { useCapture: true } );
  72. node.dispatchEvent( new Event( 'test', { bubbles: false } ) );
  73. sinon.assert.calledOnce( spy );
  74. } );
  75. } );
  76. } );
  77. describe( 'stopListening', () => {
  78. it( 'should stop listening to a specific event callback', () => {
  79. const spy1 = testUtils.sinon.spy();
  80. const spy2 = testUtils.sinon.spy();
  81. domEmitter.listenTo( node, 'event1', spy1 );
  82. domEmitter.listenTo( node, 'event2', spy2 );
  83. node.dispatchEvent( new Event( 'event1' ) );
  84. node.dispatchEvent( new Event( 'event2' ) );
  85. domEmitter.stopListening( node, 'event1', spy1 );
  86. node.dispatchEvent( new Event( 'event1' ) );
  87. node.dispatchEvent( new Event( 'event2' ) );
  88. sinon.assert.calledOnce( spy1 );
  89. sinon.assert.calledTwice( spy2 );
  90. } );
  91. it( 'should stop listening to an specific event', () => {
  92. const spy1a = testUtils.sinon.spy();
  93. const spy1b = testUtils.sinon.spy();
  94. const spy2 = testUtils.sinon.spy();
  95. domEmitter.listenTo( node, 'event1', spy1a );
  96. domEmitter.listenTo( node, 'event1', spy1b );
  97. domEmitter.listenTo( node, 'event2', spy2 );
  98. node.dispatchEvent( new Event( 'event1' ) );
  99. node.dispatchEvent( new Event( 'event2' ) );
  100. sinon.assert.calledOnce( spy1a );
  101. sinon.assert.calledOnce( spy1b );
  102. sinon.assert.calledOnce( spy2 );
  103. domEmitter.stopListening( node, 'event1' );
  104. node.dispatchEvent( new Event( 'event1' ) );
  105. node.dispatchEvent( new Event( 'event2' ) );
  106. sinon.assert.calledOnce( spy1a );
  107. sinon.assert.calledOnce( spy1b );
  108. sinon.assert.calledTwice( spy2 );
  109. } );
  110. it( 'should stop listening to all events from a specific node', () => {
  111. const spy1 = testUtils.sinon.spy();
  112. const spy2 = testUtils.sinon.spy();
  113. domEmitter.listenTo( node, 'event1', spy1 );
  114. domEmitter.listenTo( node, 'event2', spy2 );
  115. node.dispatchEvent( new Event( 'event1' ) );
  116. node.dispatchEvent( new Event( 'event2' ) );
  117. domEmitter.stopListening( node );
  118. node.dispatchEvent( new Event( 'event1' ) );
  119. node.dispatchEvent( new Event( 'event2' ) );
  120. sinon.assert.calledOnce( spy1 );
  121. sinon.assert.calledOnce( spy2 );
  122. } );
  123. it( 'should stop listening to everything', () => {
  124. const spy1 = testUtils.sinon.spy();
  125. const spy2 = testUtils.sinon.spy();
  126. const node1 = document.createElement( 'div' );
  127. const node2 = document.createElement( 'div' );
  128. domEmitter.listenTo( node1, 'event1', spy1 );
  129. domEmitter.listenTo( node2, 'event2', spy2 );
  130. expect( domEmitter ).to.have.property( '_listeningTo' );
  131. node1.dispatchEvent( new Event( 'event1' ) );
  132. node2.dispatchEvent( new Event( 'event2' ) );
  133. domEmitter.stopListening();
  134. node1.dispatchEvent( new Event( 'event1' ) );
  135. node2.dispatchEvent( new Event( 'event2' ) );
  136. sinon.assert.calledOnce( spy1 );
  137. sinon.assert.calledOnce( spy2 );
  138. expect( domEmitter ).to.not.have.property( '_listeningTo' );
  139. } );
  140. it( 'should stop listening to everything what left', () => {
  141. const spy1 = testUtils.sinon.spy();
  142. const spy2 = testUtils.sinon.spy();
  143. const spy3 = testUtils.sinon.spy();
  144. const spy4 = testUtils.sinon.spy();
  145. const node1 = document.createElement( 'div' );
  146. const node2 = document.createElement( 'div' );
  147. domEmitter.listenTo( node1, 'event1', spy1 );
  148. domEmitter.listenTo( node1, 'event2', spy2 );
  149. domEmitter.listenTo( node2, 'event1', spy3 );
  150. domEmitter.listenTo( node2, 'event2', spy4 );
  151. expect( domEmitter ).to.have.property( '_listeningTo' );
  152. domEmitter.stopListening( node1, 'event1', spy1 );
  153. domEmitter.stopListening( node2, 'event1' );
  154. node1.dispatchEvent( new Event( 'event1' ) );
  155. node1.dispatchEvent( new Event( 'event2' ) );
  156. node2.dispatchEvent( new Event( 'event1' ) );
  157. node2.dispatchEvent( new Event( 'event2' ) );
  158. sinon.assert.notCalled( spy1 );
  159. sinon.assert.calledOnce( spy2 );
  160. sinon.assert.notCalled( spy3 );
  161. sinon.assert.calledOnce( spy4 );
  162. domEmitter.stopListening();
  163. node1.dispatchEvent( new Event( 'event1' ) );
  164. node1.dispatchEvent( new Event( 'event2' ) );
  165. node2.dispatchEvent( new Event( 'event1' ) );
  166. node2.dispatchEvent( new Event( 'event2' ) );
  167. sinon.assert.notCalled( spy1 );
  168. sinon.assert.calledOnce( spy2 );
  169. sinon.assert.notCalled( spy3 );
  170. sinon.assert.calledOnce( spy4 );
  171. expect( domEmitter ).to.not.have.property( '_listeningTo' );
  172. } );
  173. it( 'should not stop other nodes when a non-listened node is provided', () => {
  174. const spy = testUtils.sinon.spy();
  175. const node1 = document.createElement( 'div' );
  176. const node2 = document.createElement( 'div' );
  177. domEmitter.listenTo( node1, 'test', spy );
  178. domEmitter.stopListening( node2 );
  179. node1.dispatchEvent( new Event( 'test' ) );
  180. sinon.assert.called( spy );
  181. } );
  182. it( 'should pass DOM Event data to the listener', () => {
  183. const spy = testUtils.sinon.spy();
  184. domEmitter.listenTo( node, 'click', spy );
  185. const mouseEvent = new MouseEvent( 'click', {
  186. screenX: 10,
  187. screenY: 20
  188. } );
  189. node.dispatchEvent( mouseEvent );
  190. sinon.assert.calledOnce( spy );
  191. expect( spy.args[ 0 ][ 1 ] ).to.be.equal( mouseEvent );
  192. } );
  193. it( 'should detach native DOM event listener proxy, specific event', () => {
  194. const spy1a = testUtils.sinon.spy();
  195. const spy1b = testUtils.sinon.spy();
  196. domEmitter.listenTo( node, 'test', spy1a );
  197. const proxyEmitter = domEmitter._getProxyEmitter( node );
  198. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  199. node.dispatchEvent( new Event( 'test' ) );
  200. sinon.assert.calledOnce( spy1a );
  201. sinon.assert.calledOnce( spy2 );
  202. domEmitter.stopListening( node, 'test' );
  203. node.dispatchEvent( new Event( 'test' ) );
  204. sinon.assert.calledOnce( spy1a );
  205. sinon.assert.calledOnce( spy2 );
  206. // Attach same event again.
  207. domEmitter.listenTo( node, 'test', spy1b );
  208. node.dispatchEvent( new Event( 'test' ) );
  209. expect( proxyEmitter ).to.be.equal( domEmitter._getProxyEmitter( node ) );
  210. sinon.assert.calledOnce( spy1a );
  211. sinon.assert.calledOnce( spy1b );
  212. sinon.assert.calledTwice( spy2 );
  213. } );
  214. it( 'should detach native DOM event listener proxy, specific callback', () => {
  215. const spy1a = testUtils.sinon.spy();
  216. const spy1b = testUtils.sinon.spy();
  217. const spy1c = testUtils.sinon.spy();
  218. domEmitter.listenTo( node, 'test', spy1a );
  219. domEmitter.listenTo( node, 'test', spy1b );
  220. const proxyEmitter = domEmitter._getProxyEmitter( node );
  221. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  222. node.dispatchEvent( new Event( 'test' ) );
  223. sinon.assert.calledOnce( spy1a );
  224. sinon.assert.calledOnce( spy1b );
  225. sinon.assert.calledOnce( spy2 );
  226. domEmitter.stopListening( node, 'test', spy1a );
  227. node.dispatchEvent( new Event( 'test' ) );
  228. sinon.assert.calledOnce( spy1a );
  229. sinon.assert.calledTwice( spy1b );
  230. sinon.assert.calledTwice( spy2 );
  231. domEmitter.stopListening( node, 'test', spy1b );
  232. node.dispatchEvent( new Event( 'test' ) );
  233. sinon.assert.calledOnce( spy1a );
  234. sinon.assert.calledTwice( spy1b );
  235. sinon.assert.calledTwice( spy2 );
  236. // Attach same event again.
  237. domEmitter.listenTo( node, 'test', spy1c );
  238. node.dispatchEvent( new Event( 'test' ) );
  239. expect( proxyEmitter ).to.be.equal( domEmitter._getProxyEmitter( node ) );
  240. sinon.assert.calledOnce( spy1a );
  241. sinon.assert.calledTwice( spy1b );
  242. sinon.assert.calledOnce( spy1c );
  243. sinon.assert.calledThrice( spy2 );
  244. } );
  245. it( 'should detach native DOM event listener proxy, specific emitter', () => {
  246. const spy1a = testUtils.sinon.spy();
  247. const spy1b = testUtils.sinon.spy();
  248. const spy1c = testUtils.sinon.spy();
  249. const spy1d = testUtils.sinon.spy();
  250. domEmitter.listenTo( node, 'test1', spy1a );
  251. domEmitter.listenTo( node, 'test2', spy1b );
  252. const proxyEmitter = domEmitter._getProxyEmitter( node );
  253. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  254. node.dispatchEvent( new Event( 'test1' ) );
  255. node.dispatchEvent( new Event( 'test2' ) );
  256. sinon.assert.calledOnce( spy1a );
  257. sinon.assert.calledOnce( spy1b );
  258. sinon.assert.calledTwice( spy2 );
  259. domEmitter.stopListening( node );
  260. node.dispatchEvent( new Event( 'test1' ) );
  261. node.dispatchEvent( new Event( 'test2' ) );
  262. sinon.assert.calledOnce( spy1a );
  263. sinon.assert.calledOnce( spy1b );
  264. sinon.assert.calledTwice( spy2 );
  265. // Attach same event again.
  266. domEmitter.listenTo( node, 'test1', spy1c );
  267. domEmitter.listenTo( node, 'test2', spy1d );
  268. // Old proxy emitter died when stopped listening to the node.
  269. const proxyEmitter2 = domEmitter._getProxyEmitter( node );
  270. const spy3 = testUtils.sinon.spy( proxyEmitter2, 'fire' );
  271. node.dispatchEvent( new Event( 'test1' ) );
  272. node.dispatchEvent( new Event( 'test2' ) );
  273. expect( proxyEmitter ).to.not.be.equal( proxyEmitter2 );
  274. sinon.assert.calledOnce( spy1a );
  275. sinon.assert.calledOnce( spy1b );
  276. sinon.assert.calledOnce( spy1c );
  277. sinon.assert.calledOnce( spy1d );
  278. sinon.assert.calledTwice( spy2 );
  279. sinon.assert.calledTwice( spy3 );
  280. } );
  281. it( 'should detach native DOM event listener proxy, everything', () => {
  282. const spy1a = testUtils.sinon.spy();
  283. const spy1b = testUtils.sinon.spy();
  284. const spy1c = testUtils.sinon.spy();
  285. const spy1d = testUtils.sinon.spy();
  286. domEmitter.listenTo( node, 'test1', spy1a );
  287. domEmitter.listenTo( node, 'test2', spy1b );
  288. const proxyEmitter = domEmitter._getProxyEmitter( node );
  289. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  290. node.dispatchEvent( new Event( 'test1' ) );
  291. node.dispatchEvent( new Event( 'test2' ) );
  292. sinon.assert.calledOnce( spy1a );
  293. sinon.assert.calledOnce( spy1b );
  294. sinon.assert.calledTwice( spy2 );
  295. domEmitter.stopListening();
  296. node.dispatchEvent( new Event( 'test1' ) );
  297. node.dispatchEvent( new Event( 'test2' ) );
  298. sinon.assert.calledOnce( spy1a );
  299. sinon.assert.calledOnce( spy1b );
  300. sinon.assert.calledTwice( spy2 );
  301. // Attach same event again.
  302. domEmitter.listenTo( node, 'test1', spy1c );
  303. domEmitter.listenTo( node, 'test2', spy1d );
  304. // Old proxy emitter died when stopped listening to the node.
  305. const proxyEmitter2 = domEmitter._getProxyEmitter( node );
  306. const spy3 = testUtils.sinon.spy( proxyEmitter2, 'fire' );
  307. node.dispatchEvent( new Event( 'test1' ) );
  308. node.dispatchEvent( new Event( 'test2' ) );
  309. expect( proxyEmitter ).to.not.be.equal( proxyEmitter2 );
  310. sinon.assert.calledOnce( spy1a );
  311. sinon.assert.calledOnce( spy1b );
  312. sinon.assert.calledOnce( spy1c );
  313. sinon.assert.calledOnce( spy1d );
  314. sinon.assert.calledTwice( spy2 );
  315. sinon.assert.calledTwice( spy3 );
  316. } );
  317. // #187
  318. it( 'should work for DOM Nodes belonging to another window', () => {
  319. const spy = testUtils.sinon.spy();
  320. const iframe = document.createElement( 'iframe' );
  321. document.body.appendChild( iframe );
  322. const iframeNode = iframe.contentWindow.document.createElement( 'div' );
  323. domEmitter.listenTo( iframeNode, 'test', spy );
  324. iframeNode.dispatchEvent( new Event( 'test' ) );
  325. domEmitter.stopListening( iframeNode );
  326. iframeNode.dispatchEvent( new Event( 'test' ) );
  327. sinon.assert.calledOnce( spy );
  328. } );
  329. } );
  330. } );