8
0

emittermixin.js 15 KB

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