emittermixin.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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', done => {
  47. const spy = testUtils.sinon.spy();
  48. const iframe = document.createElement( 'iframe' );
  49. iframe.addEventListener( 'load', () => {
  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. iframe.remove();
  55. done();
  56. } );
  57. document.body.appendChild( iframe );
  58. } );
  59. describe( 'event capturing', () => {
  60. beforeEach( () => {
  61. document.body.appendChild( node );
  62. } );
  63. afterEach( () => {
  64. document.body.removeChild( node );
  65. } );
  66. it( 'should not use capturing at default', () => {
  67. const spy = testUtils.sinon.spy();
  68. domEmitter.listenTo( document, 'test', spy );
  69. node.dispatchEvent( new Event( 'test', { bubbles: false } ) );
  70. sinon.assert.notCalled( spy );
  71. } );
  72. it( 'should optionally use capturing', () => {
  73. const spy = testUtils.sinon.spy();
  74. domEmitter.listenTo( document, 'test', spy, { useCapture: true } );
  75. node.dispatchEvent( new Event( 'test', { bubbles: false } ) );
  76. sinon.assert.calledOnce( spy );
  77. } );
  78. } );
  79. } );
  80. describe( 'stopListening', () => {
  81. it( 'should stop listening to a specific event callback', () => {
  82. const spy1 = testUtils.sinon.spy();
  83. const spy2 = testUtils.sinon.spy();
  84. domEmitter.listenTo( node, 'event1', spy1 );
  85. domEmitter.listenTo( node, 'event2', spy2 );
  86. node.dispatchEvent( new Event( 'event1' ) );
  87. node.dispatchEvent( new Event( 'event2' ) );
  88. domEmitter.stopListening( node, 'event1', spy1 );
  89. node.dispatchEvent( new Event( 'event1' ) );
  90. node.dispatchEvent( new Event( 'event2' ) );
  91. sinon.assert.calledOnce( spy1 );
  92. sinon.assert.calledTwice( spy2 );
  93. } );
  94. it( 'should stop listening to a specific event callback (re–listen)', () => {
  95. const spy = testUtils.sinon.spy();
  96. domEmitter.listenTo( node, 'event', spy );
  97. node.dispatchEvent( new Event( 'event' ) );
  98. domEmitter.stopListening( node, 'event', spy );
  99. domEmitter.listenTo( node, 'event', spy );
  100. node.dispatchEvent( new Event( 'event' ) );
  101. sinon.assert.calledTwice( spy );
  102. } );
  103. it( 'should stop listening to an specific event', () => {
  104. const spy1a = testUtils.sinon.spy();
  105. const spy1b = testUtils.sinon.spy();
  106. const spy2 = testUtils.sinon.spy();
  107. domEmitter.listenTo( node, 'event1', spy1a );
  108. domEmitter.listenTo( node, 'event1', spy1b );
  109. domEmitter.listenTo( node, 'event2', spy2 );
  110. node.dispatchEvent( new Event( 'event1' ) );
  111. node.dispatchEvent( new Event( 'event2' ) );
  112. sinon.assert.calledOnce( spy1a );
  113. sinon.assert.calledOnce( spy1b );
  114. sinon.assert.calledOnce( spy2 );
  115. domEmitter.stopListening( node, 'event1' );
  116. node.dispatchEvent( new Event( 'event1' ) );
  117. node.dispatchEvent( new Event( 'event2' ) );
  118. sinon.assert.calledOnce( spy1a );
  119. sinon.assert.calledOnce( spy1b );
  120. sinon.assert.calledTwice( spy2 );
  121. } );
  122. it( 'should stop listening to all events from a specific node', () => {
  123. const spy1 = testUtils.sinon.spy();
  124. const spy2 = testUtils.sinon.spy();
  125. domEmitter.listenTo( node, 'event1', spy1 );
  126. domEmitter.listenTo( node, 'event2', spy2 );
  127. node.dispatchEvent( new Event( 'event1' ) );
  128. node.dispatchEvent( new Event( 'event2' ) );
  129. domEmitter.stopListening( node );
  130. node.dispatchEvent( new Event( 'event1' ) );
  131. node.dispatchEvent( new Event( 'event2' ) );
  132. sinon.assert.calledOnce( spy1 );
  133. sinon.assert.calledOnce( spy2 );
  134. } );
  135. it( 'should stop listening to everything', () => {
  136. const spy1 = testUtils.sinon.spy();
  137. const spy2 = testUtils.sinon.spy();
  138. const node1 = document.createElement( 'div' );
  139. const node2 = document.createElement( 'div' );
  140. domEmitter.listenTo( node1, 'event1', spy1 );
  141. domEmitter.listenTo( node2, 'event2', spy2 );
  142. node1.dispatchEvent( new Event( 'event1' ) );
  143. node2.dispatchEvent( new Event( 'event2' ) );
  144. domEmitter.stopListening();
  145. node1.dispatchEvent( new Event( 'event1' ) );
  146. node2.dispatchEvent( new Event( 'event2' ) );
  147. sinon.assert.calledOnce( spy1 );
  148. sinon.assert.calledOnce( spy2 );
  149. } );
  150. it( 'should stop listening to everything what left', () => {
  151. const spy1 = testUtils.sinon.spy();
  152. const spy2 = testUtils.sinon.spy();
  153. const spy3 = testUtils.sinon.spy();
  154. const spy4 = testUtils.sinon.spy();
  155. const node1 = document.createElement( 'div' );
  156. const node2 = document.createElement( 'div' );
  157. domEmitter.listenTo( node1, 'event1', spy1 );
  158. domEmitter.listenTo( node1, 'event2', spy2 );
  159. domEmitter.listenTo( node2, 'event1', spy3 );
  160. domEmitter.listenTo( node2, 'event2', spy4 );
  161. domEmitter.stopListening( node1, 'event1', spy1 );
  162. domEmitter.stopListening( node2, 'event1' );
  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. domEmitter.stopListening();
  172. node1.dispatchEvent( new Event( 'event1' ) );
  173. node1.dispatchEvent( new Event( 'event2' ) );
  174. node2.dispatchEvent( new Event( 'event1' ) );
  175. node2.dispatchEvent( new Event( 'event2' ) );
  176. sinon.assert.notCalled( spy1 );
  177. sinon.assert.calledOnce( spy2 );
  178. sinon.assert.notCalled( spy3 );
  179. sinon.assert.calledOnce( spy4 );
  180. } );
  181. it( 'should not stop other nodes when a non-listened node is provided', () => {
  182. const spy = testUtils.sinon.spy();
  183. const node1 = document.createElement( 'div' );
  184. const node2 = document.createElement( 'div' );
  185. domEmitter.listenTo( node1, 'test', spy );
  186. domEmitter.stopListening( node2 );
  187. node1.dispatchEvent( new Event( 'test' ) );
  188. sinon.assert.called( spy );
  189. } );
  190. it( 'should pass DOM Event data to the listener', () => {
  191. const spy = testUtils.sinon.spy();
  192. domEmitter.listenTo( node, 'click', spy );
  193. const mouseEvent = new MouseEvent( 'click', {
  194. screenX: 10,
  195. screenY: 20
  196. } );
  197. node.dispatchEvent( mouseEvent );
  198. sinon.assert.calledOnce( spy );
  199. expect( spy.args[ 0 ][ 1 ] ).to.be.equal( mouseEvent );
  200. } );
  201. it( 'should detach native DOM event listener proxy, specific event', () => {
  202. const spy1a = testUtils.sinon.spy();
  203. const spy1b = testUtils.sinon.spy();
  204. domEmitter.listenTo( node, 'test', spy1a );
  205. const proxyEmitter = domEmitter._getProxyEmitter( node );
  206. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  207. node.dispatchEvent( new Event( 'test' ) );
  208. sinon.assert.calledOnce( spy1a );
  209. sinon.assert.calledOnce( spy2 );
  210. domEmitter.stopListening( node, 'test' );
  211. node.dispatchEvent( new Event( 'test' ) );
  212. sinon.assert.calledOnce( spy1a );
  213. sinon.assert.calledOnce( spy2 );
  214. // Attach same event again.
  215. domEmitter.listenTo( node, 'test', spy1b );
  216. node.dispatchEvent( new Event( 'test' ) );
  217. expect( proxyEmitter ).to.be.equal( domEmitter._getProxyEmitter( node ) );
  218. sinon.assert.calledOnce( spy1a );
  219. sinon.assert.calledOnce( spy1b );
  220. sinon.assert.calledTwice( spy2 );
  221. } );
  222. it( 'should detach native DOM event listener proxy, specific callback', () => {
  223. const spy1a = testUtils.sinon.spy();
  224. const spy1b = testUtils.sinon.spy();
  225. const spy1c = testUtils.sinon.spy();
  226. domEmitter.listenTo( node, 'test', spy1a );
  227. domEmitter.listenTo( node, 'test', spy1b );
  228. const proxyEmitter = domEmitter._getProxyEmitter( node );
  229. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  230. node.dispatchEvent( new Event( 'test' ) );
  231. sinon.assert.calledOnce( spy1a );
  232. sinon.assert.calledOnce( spy1b );
  233. sinon.assert.calledOnce( spy2 );
  234. domEmitter.stopListening( node, 'test', spy1a );
  235. node.dispatchEvent( new Event( 'test' ) );
  236. sinon.assert.calledOnce( spy1a );
  237. sinon.assert.calledTwice( spy1b );
  238. sinon.assert.calledTwice( spy2 );
  239. domEmitter.stopListening( node, 'test', spy1b );
  240. node.dispatchEvent( new Event( 'test' ) );
  241. sinon.assert.calledOnce( spy1a );
  242. sinon.assert.calledTwice( spy1b );
  243. sinon.assert.calledTwice( spy2 );
  244. // Attach same event again.
  245. domEmitter.listenTo( node, 'test', spy1c );
  246. node.dispatchEvent( new Event( 'test' ) );
  247. expect( proxyEmitter ).to.be.equal( domEmitter._getProxyEmitter( node ) );
  248. sinon.assert.calledOnce( spy1a );
  249. sinon.assert.calledTwice( spy1b );
  250. sinon.assert.calledOnce( spy1c );
  251. sinon.assert.calledThrice( spy2 );
  252. } );
  253. it( 'should detach native DOM event listener proxy, specific emitter', () => {
  254. const spy1a = testUtils.sinon.spy();
  255. const spy1b = testUtils.sinon.spy();
  256. const spy1c = testUtils.sinon.spy();
  257. const spy1d = testUtils.sinon.spy();
  258. domEmitter.listenTo( node, 'test1', spy1a );
  259. domEmitter.listenTo( node, 'test2', spy1b );
  260. const proxyEmitter = domEmitter._getProxyEmitter( node );
  261. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  262. node.dispatchEvent( new Event( 'test1' ) );
  263. node.dispatchEvent( new Event( 'test2' ) );
  264. sinon.assert.calledOnce( spy1a );
  265. sinon.assert.calledOnce( spy1b );
  266. sinon.assert.calledTwice( spy2 );
  267. domEmitter.stopListening( node );
  268. node.dispatchEvent( new Event( 'test1' ) );
  269. node.dispatchEvent( new Event( 'test2' ) );
  270. sinon.assert.calledOnce( spy1a );
  271. sinon.assert.calledOnce( spy1b );
  272. sinon.assert.calledTwice( spy2 );
  273. // Attach same event again.
  274. domEmitter.listenTo( node, 'test1', spy1c );
  275. domEmitter.listenTo( node, 'test2', spy1d );
  276. // Old proxy emitter died when stopped listening to the node.
  277. const proxyEmitter2 = domEmitter._getProxyEmitter( node );
  278. const spy3 = testUtils.sinon.spy( proxyEmitter2, 'fire' );
  279. node.dispatchEvent( new Event( 'test1' ) );
  280. node.dispatchEvent( new Event( 'test2' ) );
  281. expect( proxyEmitter ).to.not.be.equal( proxyEmitter2 );
  282. sinon.assert.calledOnce( spy1a );
  283. sinon.assert.calledOnce( spy1b );
  284. sinon.assert.calledOnce( spy1c );
  285. sinon.assert.calledOnce( spy1d );
  286. sinon.assert.calledTwice( spy2 );
  287. sinon.assert.calledTwice( spy3 );
  288. } );
  289. it( 'should detach native DOM event listener proxy, everything', () => {
  290. const spy1a = testUtils.sinon.spy();
  291. const spy1b = testUtils.sinon.spy();
  292. const spy1c = testUtils.sinon.spy();
  293. const spy1d = testUtils.sinon.spy();
  294. domEmitter.listenTo( node, 'test1', spy1a );
  295. domEmitter.listenTo( node, 'test2', spy1b );
  296. const proxyEmitter = domEmitter._getProxyEmitter( node );
  297. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  298. node.dispatchEvent( new Event( 'test1' ) );
  299. node.dispatchEvent( new Event( 'test2' ) );
  300. sinon.assert.calledOnce( spy1a );
  301. sinon.assert.calledOnce( spy1b );
  302. sinon.assert.calledTwice( spy2 );
  303. domEmitter.stopListening();
  304. node.dispatchEvent( new Event( 'test1' ) );
  305. node.dispatchEvent( new Event( 'test2' ) );
  306. sinon.assert.calledOnce( spy1a );
  307. sinon.assert.calledOnce( spy1b );
  308. sinon.assert.calledTwice( spy2 );
  309. // Attach same event again.
  310. domEmitter.listenTo( node, 'test1', spy1c );
  311. domEmitter.listenTo( node, 'test2', spy1d );
  312. // Old proxy emitter died when stopped listening to the node.
  313. const proxyEmitter2 = domEmitter._getProxyEmitter( node );
  314. const spy3 = testUtils.sinon.spy( proxyEmitter2, 'fire' );
  315. node.dispatchEvent( new Event( 'test1' ) );
  316. node.dispatchEvent( new Event( 'test2' ) );
  317. expect( proxyEmitter ).to.not.be.equal( proxyEmitter2 );
  318. sinon.assert.calledOnce( spy1a );
  319. sinon.assert.calledOnce( spy1b );
  320. sinon.assert.calledOnce( spy1c );
  321. sinon.assert.calledOnce( spy1d );
  322. sinon.assert.calledTwice( spy2 );
  323. sinon.assert.calledTwice( spy3 );
  324. } );
  325. // #187
  326. it( 'should work for DOM Nodes belonging to another window', done => {
  327. const spy = testUtils.sinon.spy();
  328. const iframe = document.createElement( 'iframe' );
  329. iframe.addEventListener( 'load', () => {
  330. const iframeNode = iframe.contentWindow.document.createElement( 'div' );
  331. domEmitter.listenTo( iframeNode, 'test', spy );
  332. iframeNode.dispatchEvent( new Event( 'test' ) );
  333. domEmitter.stopListening( iframeNode );
  334. iframeNode.dispatchEvent( new Event( 'test' ) );
  335. sinon.assert.calledOnce( spy );
  336. iframe.remove();
  337. done();
  338. } );
  339. document.body.appendChild( iframe );
  340. } );
  341. describe( 'event capturing', () => {
  342. beforeEach( () => {
  343. document.body.appendChild( node );
  344. } );
  345. afterEach( () => {
  346. document.body.removeChild( node );
  347. } );
  348. it( 'should remove listeners when re–listen', () => {
  349. const spy = testUtils.sinon.spy();
  350. domEmitter.listenTo( document, 'test', spy, { useCapture: true } );
  351. node.dispatchEvent( new Event( 'test' ) );
  352. sinon.assert.calledOnce( spy );
  353. domEmitter.stopListening( document, 'test' );
  354. node.dispatchEvent( new Event( 'test' ) );
  355. sinon.assert.calledOnce( spy );
  356. // Listen again.
  357. domEmitter.listenTo( document, 'test', spy, { useCapture: true } );
  358. node.dispatchEvent( new Event( 'test', { bubbles: false } ) );
  359. sinon.assert.calledTwice( spy );
  360. domEmitter.stopListening( document, 'test' );
  361. node.dispatchEvent( new Event( 'test', { bubbles: false } ) );
  362. sinon.assert.calledTwice( spy );
  363. } );
  364. } );
  365. } );
  366. } );