emittermixin.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, window, Event, MouseEvent */
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import DomEmitterMixin from '../../src/dom/emittermixin';
  8. import EmitterMixin from '../../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. node1.dispatchEvent( new Event( 'event1' ) );
  130. node2.dispatchEvent( new Event( 'event2' ) );
  131. domEmitter.stopListening();
  132. node1.dispatchEvent( new Event( 'event1' ) );
  133. node2.dispatchEvent( new Event( 'event2' ) );
  134. sinon.assert.calledOnce( spy1 );
  135. sinon.assert.calledOnce( spy2 );
  136. } );
  137. it( 'should stop listening to everything what left', () => {
  138. const spy1 = testUtils.sinon.spy();
  139. const spy2 = testUtils.sinon.spy();
  140. const spy3 = testUtils.sinon.spy();
  141. const spy4 = testUtils.sinon.spy();
  142. const node1 = document.createElement( 'div' );
  143. const node2 = document.createElement( 'div' );
  144. domEmitter.listenTo( node1, 'event1', spy1 );
  145. domEmitter.listenTo( node1, 'event2', spy2 );
  146. domEmitter.listenTo( node2, 'event1', spy3 );
  147. domEmitter.listenTo( node2, 'event2', spy4 );
  148. domEmitter.stopListening( node1, 'event1', spy1 );
  149. domEmitter.stopListening( node2, 'event1' );
  150. node1.dispatchEvent( new Event( 'event1' ) );
  151. node1.dispatchEvent( new Event( 'event2' ) );
  152. node2.dispatchEvent( new Event( 'event1' ) );
  153. node2.dispatchEvent( new Event( 'event2' ) );
  154. sinon.assert.notCalled( spy1 );
  155. sinon.assert.calledOnce( spy2 );
  156. sinon.assert.notCalled( spy3 );
  157. sinon.assert.calledOnce( spy4 );
  158. domEmitter.stopListening();
  159. node1.dispatchEvent( new Event( 'event1' ) );
  160. node1.dispatchEvent( new Event( 'event2' ) );
  161. node2.dispatchEvent( new Event( 'event1' ) );
  162. node2.dispatchEvent( new Event( 'event2' ) );
  163. sinon.assert.notCalled( spy1 );
  164. sinon.assert.calledOnce( spy2 );
  165. sinon.assert.notCalled( spy3 );
  166. sinon.assert.calledOnce( spy4 );
  167. } );
  168. it( 'should not stop other nodes when a non-listened node is provided', () => {
  169. const spy = testUtils.sinon.spy();
  170. const node1 = document.createElement( 'div' );
  171. const node2 = document.createElement( 'div' );
  172. domEmitter.listenTo( node1, 'test', spy );
  173. domEmitter.stopListening( node2 );
  174. node1.dispatchEvent( new Event( 'test' ) );
  175. sinon.assert.called( spy );
  176. } );
  177. it( 'should pass DOM Event data to the listener', () => {
  178. const spy = testUtils.sinon.spy();
  179. domEmitter.listenTo( node, 'click', spy );
  180. const mouseEvent = new MouseEvent( 'click', {
  181. screenX: 10,
  182. screenY: 20
  183. } );
  184. node.dispatchEvent( mouseEvent );
  185. sinon.assert.calledOnce( spy );
  186. expect( spy.args[ 0 ][ 1 ] ).to.be.equal( mouseEvent );
  187. } );
  188. it( 'should detach native DOM event listener proxy, specific event', () => {
  189. const spy1a = testUtils.sinon.spy();
  190. const spy1b = testUtils.sinon.spy();
  191. domEmitter.listenTo( node, 'test', spy1a );
  192. const proxyEmitter = domEmitter._getProxyEmitter( node );
  193. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  194. node.dispatchEvent( new Event( 'test' ) );
  195. sinon.assert.calledOnce( spy1a );
  196. sinon.assert.calledOnce( spy2 );
  197. domEmitter.stopListening( node, 'test' );
  198. node.dispatchEvent( new Event( 'test' ) );
  199. sinon.assert.calledOnce( spy1a );
  200. sinon.assert.calledOnce( spy2 );
  201. // Attach same event again.
  202. domEmitter.listenTo( node, 'test', spy1b );
  203. node.dispatchEvent( new Event( 'test' ) );
  204. expect( proxyEmitter ).to.be.equal( domEmitter._getProxyEmitter( node ) );
  205. sinon.assert.calledOnce( spy1a );
  206. sinon.assert.calledOnce( spy1b );
  207. sinon.assert.calledTwice( spy2 );
  208. } );
  209. it( 'should detach native DOM event listener proxy, specific callback', () => {
  210. const spy1a = testUtils.sinon.spy();
  211. const spy1b = testUtils.sinon.spy();
  212. const spy1c = testUtils.sinon.spy();
  213. domEmitter.listenTo( node, 'test', spy1a );
  214. domEmitter.listenTo( node, 'test', spy1b );
  215. const proxyEmitter = domEmitter._getProxyEmitter( node );
  216. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  217. node.dispatchEvent( new Event( 'test' ) );
  218. sinon.assert.calledOnce( spy1a );
  219. sinon.assert.calledOnce( spy1b );
  220. sinon.assert.calledOnce( spy2 );
  221. domEmitter.stopListening( node, 'test', spy1a );
  222. node.dispatchEvent( new Event( 'test' ) );
  223. sinon.assert.calledOnce( spy1a );
  224. sinon.assert.calledTwice( spy1b );
  225. sinon.assert.calledTwice( spy2 );
  226. domEmitter.stopListening( node, 'test', spy1b );
  227. node.dispatchEvent( new Event( 'test' ) );
  228. sinon.assert.calledOnce( spy1a );
  229. sinon.assert.calledTwice( spy1b );
  230. sinon.assert.calledTwice( spy2 );
  231. // Attach same event again.
  232. domEmitter.listenTo( node, 'test', spy1c );
  233. node.dispatchEvent( new Event( 'test' ) );
  234. expect( proxyEmitter ).to.be.equal( domEmitter._getProxyEmitter( node ) );
  235. sinon.assert.calledOnce( spy1a );
  236. sinon.assert.calledTwice( spy1b );
  237. sinon.assert.calledOnce( spy1c );
  238. sinon.assert.calledThrice( spy2 );
  239. } );
  240. it( 'should detach native DOM event listener proxy, specific emitter', () => {
  241. const spy1a = testUtils.sinon.spy();
  242. const spy1b = testUtils.sinon.spy();
  243. const spy1c = testUtils.sinon.spy();
  244. const spy1d = testUtils.sinon.spy();
  245. domEmitter.listenTo( node, 'test1', spy1a );
  246. domEmitter.listenTo( node, 'test2', spy1b );
  247. const proxyEmitter = domEmitter._getProxyEmitter( node );
  248. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  249. node.dispatchEvent( new Event( 'test1' ) );
  250. node.dispatchEvent( new Event( 'test2' ) );
  251. sinon.assert.calledOnce( spy1a );
  252. sinon.assert.calledOnce( spy1b );
  253. sinon.assert.calledTwice( spy2 );
  254. domEmitter.stopListening( node );
  255. node.dispatchEvent( new Event( 'test1' ) );
  256. node.dispatchEvent( new Event( 'test2' ) );
  257. sinon.assert.calledOnce( spy1a );
  258. sinon.assert.calledOnce( spy1b );
  259. sinon.assert.calledTwice( spy2 );
  260. // Attach same event again.
  261. domEmitter.listenTo( node, 'test1', spy1c );
  262. domEmitter.listenTo( node, 'test2', spy1d );
  263. // Old proxy emitter died when stopped listening to the node.
  264. const proxyEmitter2 = domEmitter._getProxyEmitter( node );
  265. const spy3 = testUtils.sinon.spy( proxyEmitter2, 'fire' );
  266. node.dispatchEvent( new Event( 'test1' ) );
  267. node.dispatchEvent( new Event( 'test2' ) );
  268. expect( proxyEmitter ).to.not.be.equal( proxyEmitter2 );
  269. sinon.assert.calledOnce( spy1a );
  270. sinon.assert.calledOnce( spy1b );
  271. sinon.assert.calledOnce( spy1c );
  272. sinon.assert.calledOnce( spy1d );
  273. sinon.assert.calledTwice( spy2 );
  274. sinon.assert.calledTwice( spy3 );
  275. } );
  276. it( 'should detach native DOM event listener proxy, everything', () => {
  277. const spy1a = testUtils.sinon.spy();
  278. const spy1b = testUtils.sinon.spy();
  279. const spy1c = testUtils.sinon.spy();
  280. const spy1d = testUtils.sinon.spy();
  281. domEmitter.listenTo( node, 'test1', spy1a );
  282. domEmitter.listenTo( node, 'test2', spy1b );
  283. const proxyEmitter = domEmitter._getProxyEmitter( node );
  284. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  285. node.dispatchEvent( new Event( 'test1' ) );
  286. node.dispatchEvent( new Event( 'test2' ) );
  287. sinon.assert.calledOnce( spy1a );
  288. sinon.assert.calledOnce( spy1b );
  289. sinon.assert.calledTwice( spy2 );
  290. domEmitter.stopListening();
  291. node.dispatchEvent( new Event( 'test1' ) );
  292. node.dispatchEvent( new Event( 'test2' ) );
  293. sinon.assert.calledOnce( spy1a );
  294. sinon.assert.calledOnce( spy1b );
  295. sinon.assert.calledTwice( spy2 );
  296. // Attach same event again.
  297. domEmitter.listenTo( node, 'test1', spy1c );
  298. domEmitter.listenTo( node, 'test2', spy1d );
  299. // Old proxy emitter died when stopped listening to the node.
  300. const proxyEmitter2 = domEmitter._getProxyEmitter( node );
  301. const spy3 = testUtils.sinon.spy( proxyEmitter2, 'fire' );
  302. node.dispatchEvent( new Event( 'test1' ) );
  303. node.dispatchEvent( new Event( 'test2' ) );
  304. expect( proxyEmitter ).to.not.be.equal( proxyEmitter2 );
  305. sinon.assert.calledOnce( spy1a );
  306. sinon.assert.calledOnce( spy1b );
  307. sinon.assert.calledOnce( spy1c );
  308. sinon.assert.calledOnce( spy1d );
  309. sinon.assert.calledTwice( spy2 );
  310. sinon.assert.calledTwice( spy3 );
  311. } );
  312. // #187
  313. it( 'should work for DOM Nodes belonging to another window', () => {
  314. const spy = testUtils.sinon.spy();
  315. const iframe = document.createElement( 'iframe' );
  316. document.body.appendChild( iframe );
  317. const iframeNode = iframe.contentWindow.document.createElement( 'div' );
  318. domEmitter.listenTo( iframeNode, 'test', spy );
  319. iframeNode.dispatchEvent( new Event( 'test' ) );
  320. domEmitter.stopListening( iframeNode );
  321. iframeNode.dispatchEvent( new Event( 'test' ) );
  322. sinon.assert.calledOnce( spy );
  323. } );
  324. } );
  325. } );