8
0

emittermixin.js 14 KB

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