emittermixin.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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 EmitterMixin events, specific event', () => {
  82. const spy1 = testUtils.sinon.spy();
  83. const spy2 = testUtils.sinon.spy();
  84. domEmitter.listenTo( emitter, 'foo', spy1 );
  85. domEmitter.listenTo( emitter, 'foo:bar', spy2 );
  86. emitter.fire( 'foo:bar' );
  87. sinon.assert.calledOnce( spy1 );
  88. sinon.assert.calledOnce( spy2 );
  89. domEmitter.stopListening( emitter, 'foo' );
  90. emitter.fire( 'foo:bar' );
  91. sinon.assert.calledOnce( spy1 );
  92. sinon.assert.calledTwice( spy2 );
  93. } );
  94. it( 'should stop listening to EmitterMixin events, specific emitter', () => {
  95. const spy1 = testUtils.sinon.spy();
  96. const spy2 = testUtils.sinon.spy();
  97. domEmitter.listenTo( emitter, 'foo', spy1 );
  98. domEmitter.listenTo( emitter, 'foo:bar', spy2 );
  99. emitter.fire( 'foo:bar' );
  100. sinon.assert.calledOnce( spy1 );
  101. sinon.assert.calledOnce( spy2 );
  102. domEmitter.stopListening( emitter );
  103. emitter.fire( 'foo:bar' );
  104. sinon.assert.calledOnce( spy1 );
  105. sinon.assert.calledOnce( spy2 );
  106. } );
  107. it( 'should stop listening to a specific event callback', () => {
  108. const spy1 = testUtils.sinon.spy();
  109. const spy2 = testUtils.sinon.spy();
  110. domEmitter.listenTo( node, 'event1', spy1 );
  111. domEmitter.listenTo( node, 'event2', spy2 );
  112. node.dispatchEvent( new Event( 'event1' ) );
  113. node.dispatchEvent( new Event( 'event2' ) );
  114. domEmitter.stopListening( node, 'event1', spy1 );
  115. node.dispatchEvent( new Event( 'event1' ) );
  116. node.dispatchEvent( new Event( 'event2' ) );
  117. sinon.assert.calledOnce( spy1 );
  118. sinon.assert.calledTwice( spy2 );
  119. } );
  120. it( 'should stop listening to a specific event callback (only from the given node)', () => {
  121. const spy1 = testUtils.sinon.spy();
  122. const spy2 = testUtils.sinon.spy();
  123. const node1 = document.createElement( 'div' );
  124. const node2 = document.createElement( 'div' );
  125. domEmitter.listenTo( node1, 'event1', spy1 );
  126. domEmitter.listenTo( node1, 'event2', spy2 );
  127. domEmitter.listenTo( node2, 'event1', spy1 );
  128. node1.dispatchEvent( new Event( 'event1' ) );
  129. node1.dispatchEvent( new Event( 'event2' ) );
  130. node2.dispatchEvent( new Event( 'event1' ) );
  131. sinon.assert.calledTwice( spy1 );
  132. sinon.assert.calledOnce( spy2 );
  133. domEmitter.stopListening( node1, 'event1', spy1 );
  134. node1.dispatchEvent( new Event( 'event1' ) );
  135. node1.dispatchEvent( new Event( 'event2' ) );
  136. node2.dispatchEvent( new Event( 'event1' ) );
  137. sinon.assert.calledThrice( spy1 );
  138. sinon.assert.calledTwice( spy2 );
  139. } );
  140. it( 'should stop listening to a specific event callback (re–listen)', () => {
  141. const spy = testUtils.sinon.spy();
  142. domEmitter.listenTo( node, 'event', spy );
  143. node.dispatchEvent( new Event( 'event' ) );
  144. domEmitter.stopListening( node, 'event', spy );
  145. domEmitter.listenTo( node, 'event', spy );
  146. node.dispatchEvent( new Event( 'event' ) );
  147. sinon.assert.calledTwice( spy );
  148. } );
  149. it( 'should stop listening to an specific event', () => {
  150. const spy1a = testUtils.sinon.spy();
  151. const spy1b = testUtils.sinon.spy();
  152. const spy2 = testUtils.sinon.spy();
  153. domEmitter.listenTo( node, 'event1', spy1a );
  154. domEmitter.listenTo( node, 'event1', spy1b );
  155. domEmitter.listenTo( node, 'event2', spy2 );
  156. node.dispatchEvent( new Event( 'event1' ) );
  157. node.dispatchEvent( new Event( 'event2' ) );
  158. sinon.assert.calledOnce( spy1a );
  159. sinon.assert.calledOnce( spy1b );
  160. sinon.assert.calledOnce( spy2 );
  161. domEmitter.stopListening( node, 'event1' );
  162. node.dispatchEvent( new Event( 'event1' ) );
  163. node.dispatchEvent( new Event( 'event2' ) );
  164. sinon.assert.calledOnce( spy1a );
  165. sinon.assert.calledOnce( spy1b );
  166. sinon.assert.calledTwice( spy2 );
  167. } );
  168. it( 'should stop listening to all events from a specific node', () => {
  169. const spy1 = testUtils.sinon.spy();
  170. const spy2 = testUtils.sinon.spy();
  171. domEmitter.listenTo( node, 'event1', spy1 );
  172. domEmitter.listenTo( node, 'event2', spy2 );
  173. node.dispatchEvent( new Event( 'event1' ) );
  174. node.dispatchEvent( new Event( 'event2' ) );
  175. domEmitter.stopListening( node );
  176. node.dispatchEvent( new Event( 'event1' ) );
  177. node.dispatchEvent( new Event( 'event2' ) );
  178. sinon.assert.calledOnce( spy1 );
  179. sinon.assert.calledOnce( spy2 );
  180. } );
  181. it( 'should stop listening to all events from a specific node (only that node)', () => {
  182. const spy1 = testUtils.sinon.spy();
  183. const spy2 = testUtils.sinon.spy();
  184. const node1 = document.createElement( 'div' );
  185. const node2 = document.createElement( 'div' );
  186. domEmitter.listenTo( node1, 'event', spy1 );
  187. domEmitter.listenTo( node2, 'event', spy2 );
  188. node1.dispatchEvent( new Event( 'event' ) );
  189. node2.dispatchEvent( new Event( 'event' ) );
  190. domEmitter.stopListening( node1 );
  191. node1.dispatchEvent( new Event( 'event' ) );
  192. node2.dispatchEvent( new Event( 'event' ) );
  193. sinon.assert.calledOnce( spy1 );
  194. sinon.assert.calledTwice( spy2 );
  195. } );
  196. it( 'should stop listening to everything', () => {
  197. const spy1 = testUtils.sinon.spy();
  198. const spy2 = testUtils.sinon.spy();
  199. const spy3 = testUtils.sinon.spy();
  200. const node1 = document.createElement( 'div' );
  201. const node2 = document.createElement( 'div' );
  202. domEmitter.listenTo( node1, 'event1', spy1 );
  203. domEmitter.listenTo( node2, 'event2', spy2 );
  204. domEmitter.listenTo( emitter, 'event3', spy3 );
  205. node1.dispatchEvent( new Event( 'event1' ) );
  206. node2.dispatchEvent( new Event( 'event2' ) );
  207. emitter.fire( 'event3' );
  208. domEmitter.stopListening();
  209. node1.dispatchEvent( new Event( 'event1' ) );
  210. node2.dispatchEvent( new Event( 'event2' ) );
  211. emitter.fire( 'event3' );
  212. sinon.assert.calledOnce( spy1 );
  213. sinon.assert.calledOnce( spy2 );
  214. sinon.assert.calledOnce( spy3 );
  215. } );
  216. it( 'should stop listening to everything what left', () => {
  217. const spy1 = testUtils.sinon.spy();
  218. const spy2 = testUtils.sinon.spy();
  219. const spy3 = testUtils.sinon.spy();
  220. const spy4 = testUtils.sinon.spy();
  221. const node1 = document.createElement( 'div' );
  222. const node2 = document.createElement( 'div' );
  223. domEmitter.listenTo( node1, 'event1', spy1 );
  224. domEmitter.listenTo( node1, 'event2', spy2 );
  225. domEmitter.listenTo( node2, 'event1', spy3 );
  226. domEmitter.listenTo( node2, 'event2', spy4 );
  227. domEmitter.stopListening( node1, 'event1', spy1 );
  228. domEmitter.stopListening( node2, 'event1' );
  229. node1.dispatchEvent( new Event( 'event1' ) );
  230. node1.dispatchEvent( new Event( 'event2' ) );
  231. node2.dispatchEvent( new Event( 'event1' ) );
  232. node2.dispatchEvent( new Event( 'event2' ) );
  233. sinon.assert.notCalled( spy1 );
  234. sinon.assert.calledOnce( spy2 );
  235. sinon.assert.notCalled( spy3 );
  236. sinon.assert.calledOnce( spy4 );
  237. domEmitter.stopListening();
  238. node1.dispatchEvent( new Event( 'event1' ) );
  239. node1.dispatchEvent( new Event( 'event2' ) );
  240. node2.dispatchEvent( new Event( 'event1' ) );
  241. node2.dispatchEvent( new Event( 'event2' ) );
  242. sinon.assert.notCalled( spy1 );
  243. sinon.assert.calledOnce( spy2 );
  244. sinon.assert.notCalled( spy3 );
  245. sinon.assert.calledOnce( spy4 );
  246. } );
  247. it( 'should not stop other nodes when a non-listened node is provided', () => {
  248. const spy = testUtils.sinon.spy();
  249. const node1 = document.createElement( 'div' );
  250. const node2 = document.createElement( 'div' );
  251. domEmitter.listenTo( node1, 'test', spy );
  252. domEmitter.stopListening( node2 );
  253. node1.dispatchEvent( new Event( 'test' ) );
  254. sinon.assert.called( spy );
  255. } );
  256. it( 'should pass DOM Event data to the listener', () => {
  257. const spy = testUtils.sinon.spy();
  258. domEmitter.listenTo( node, 'click', spy );
  259. const mouseEvent = new MouseEvent( 'click', {
  260. screenX: 10,
  261. screenY: 20
  262. } );
  263. node.dispatchEvent( mouseEvent );
  264. sinon.assert.calledOnce( spy );
  265. expect( spy.args[ 0 ][ 1 ] ).to.be.equal( mouseEvent );
  266. } );
  267. it( 'should detach native DOM event listener proxy, specific event', () => {
  268. const spy1a = testUtils.sinon.spy();
  269. const spy1b = testUtils.sinon.spy();
  270. domEmitter.listenTo( node, 'test', spy1a );
  271. const proxyEmitter = domEmitter._getProxyEmitter( node );
  272. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  273. node.dispatchEvent( new Event( 'test' ) );
  274. sinon.assert.calledOnce( spy1a );
  275. sinon.assert.calledOnce( spy2 );
  276. domEmitter.stopListening( node, 'test' );
  277. node.dispatchEvent( new Event( 'test' ) );
  278. sinon.assert.calledOnce( spy1a );
  279. sinon.assert.calledOnce( spy2 );
  280. // Attach same event again.
  281. domEmitter.listenTo( node, 'test', spy1b );
  282. node.dispatchEvent( new Event( 'test' ) );
  283. expect( proxyEmitter ).to.be.equal( domEmitter._getProxyEmitter( node ) );
  284. sinon.assert.calledOnce( spy1a );
  285. sinon.assert.calledOnce( spy1b );
  286. sinon.assert.calledTwice( spy2 );
  287. } );
  288. it( 'should detach native DOM event listener proxy, specific callback', () => {
  289. const spy1a = testUtils.sinon.spy();
  290. const spy1b = testUtils.sinon.spy();
  291. const spy1c = testUtils.sinon.spy();
  292. domEmitter.listenTo( node, 'test', spy1a );
  293. domEmitter.listenTo( node, 'test', spy1b );
  294. const proxyEmitter = domEmitter._getProxyEmitter( node );
  295. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  296. node.dispatchEvent( new Event( 'test' ) );
  297. sinon.assert.calledOnce( spy1a );
  298. sinon.assert.calledOnce( spy1b );
  299. sinon.assert.calledOnce( spy2 );
  300. domEmitter.stopListening( node, 'test', spy1a );
  301. node.dispatchEvent( new Event( 'test' ) );
  302. sinon.assert.calledOnce( spy1a );
  303. sinon.assert.calledTwice( spy1b );
  304. sinon.assert.calledTwice( spy2 );
  305. domEmitter.stopListening( node, 'test', spy1b );
  306. node.dispatchEvent( new Event( 'test' ) );
  307. sinon.assert.calledOnce( spy1a );
  308. sinon.assert.calledTwice( spy1b );
  309. sinon.assert.calledTwice( spy2 );
  310. // Attach same event again.
  311. domEmitter.listenTo( node, 'test', spy1c );
  312. node.dispatchEvent( new Event( 'test' ) );
  313. expect( proxyEmitter ).to.be.equal( domEmitter._getProxyEmitter( node ) );
  314. sinon.assert.calledOnce( spy1a );
  315. sinon.assert.calledTwice( spy1b );
  316. sinon.assert.calledOnce( spy1c );
  317. sinon.assert.calledThrice( spy2 );
  318. } );
  319. it( 'should detach native DOM event listener proxy, specific emitter', () => {
  320. const spy1a = testUtils.sinon.spy();
  321. const spy1b = testUtils.sinon.spy();
  322. const spy1c = testUtils.sinon.spy();
  323. const spy1d = testUtils.sinon.spy();
  324. domEmitter.listenTo( node, 'test1', spy1a );
  325. domEmitter.listenTo( node, 'test2', spy1b );
  326. const proxyEmitter = domEmitter._getProxyEmitter( node );
  327. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  328. node.dispatchEvent( new Event( 'test1' ) );
  329. node.dispatchEvent( new Event( 'test2' ) );
  330. sinon.assert.calledOnce( spy1a );
  331. sinon.assert.calledOnce( spy1b );
  332. sinon.assert.calledTwice( spy2 );
  333. domEmitter.stopListening( node );
  334. node.dispatchEvent( new Event( 'test1' ) );
  335. node.dispatchEvent( new Event( 'test2' ) );
  336. sinon.assert.calledOnce( spy1a );
  337. sinon.assert.calledOnce( spy1b );
  338. sinon.assert.calledTwice( spy2 );
  339. // Attach same event again.
  340. domEmitter.listenTo( node, 'test1', spy1c );
  341. domEmitter.listenTo( node, 'test2', spy1d );
  342. // Old proxy emitter died when stopped listening to the node.
  343. const proxyEmitter2 = domEmitter._getProxyEmitter( node );
  344. const spy3 = testUtils.sinon.spy( proxyEmitter2, 'fire' );
  345. node.dispatchEvent( new Event( 'test1' ) );
  346. node.dispatchEvent( new Event( 'test2' ) );
  347. expect( proxyEmitter ).to.not.be.equal( proxyEmitter2 );
  348. sinon.assert.calledOnce( spy1a );
  349. sinon.assert.calledOnce( spy1b );
  350. sinon.assert.calledOnce( spy1c );
  351. sinon.assert.calledOnce( spy1d );
  352. sinon.assert.calledTwice( spy2 );
  353. sinon.assert.calledTwice( spy3 );
  354. } );
  355. it( 'should detach native DOM event listener proxy, everything', () => {
  356. const spy1a = testUtils.sinon.spy();
  357. const spy1b = testUtils.sinon.spy();
  358. const spy1c = testUtils.sinon.spy();
  359. const spy1d = testUtils.sinon.spy();
  360. const spyEl2 = testUtils.sinon.spy();
  361. const node2 = document.createElement( 'div' );
  362. domEmitter.listenTo( node, 'test1', spy1a );
  363. domEmitter.listenTo( node, 'test2', spy1b );
  364. domEmitter.listenTo( node2, 'test1', spyEl2 );
  365. const proxyEmitter = domEmitter._getProxyEmitter( node );
  366. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  367. node.dispatchEvent( new Event( 'test1' ) );
  368. node.dispatchEvent( new Event( 'test2' ) );
  369. node2.dispatchEvent( new Event( 'test1' ) );
  370. sinon.assert.calledOnce( spy1a );
  371. sinon.assert.calledOnce( spy1b );
  372. sinon.assert.calledTwice( spy2 );
  373. sinon.assert.calledOnce( spyEl2 );
  374. domEmitter.stopListening();
  375. node.dispatchEvent( new Event( 'test1' ) );
  376. node.dispatchEvent( new Event( 'test2' ) );
  377. node2.dispatchEvent( new Event( 'test1' ) );
  378. sinon.assert.calledOnce( spy1a );
  379. sinon.assert.calledOnce( spy1b );
  380. sinon.assert.calledTwice( spy2 );
  381. sinon.assert.calledOnce( spyEl2 );
  382. // Attach same event again.
  383. domEmitter.listenTo( node, 'test1', spy1c );
  384. domEmitter.listenTo( node, 'test2', spy1d );
  385. // Old proxy emitter died when stopped listening to the node.
  386. const proxyEmitter2 = domEmitter._getProxyEmitter( node );
  387. const spy3 = testUtils.sinon.spy( proxyEmitter2, 'fire' );
  388. node.dispatchEvent( new Event( 'test1' ) );
  389. node.dispatchEvent( new Event( 'test2' ) );
  390. expect( proxyEmitter ).to.not.be.equal( proxyEmitter2 );
  391. sinon.assert.calledOnce( spy1a );
  392. sinon.assert.calledOnce( spy1b );
  393. sinon.assert.calledOnce( spy1c );
  394. sinon.assert.calledOnce( spy1d );
  395. sinon.assert.calledTwice( spy2 );
  396. sinon.assert.calledTwice( spy3 );
  397. sinon.assert.calledOnce( spyEl2 );
  398. } );
  399. // #187
  400. it( 'should work for DOM Nodes belonging to another window', done => {
  401. const spy = testUtils.sinon.spy();
  402. const iframe = document.createElement( 'iframe' );
  403. iframe.addEventListener( 'load', () => {
  404. const iframeNode = iframe.contentWindow.document.createElement( 'div' );
  405. domEmitter.listenTo( iframeNode, 'test', spy );
  406. iframeNode.dispatchEvent( new Event( 'test' ) );
  407. domEmitter.stopListening( iframeNode );
  408. iframeNode.dispatchEvent( new Event( 'test' ) );
  409. sinon.assert.calledOnce( spy );
  410. iframe.remove();
  411. done();
  412. } );
  413. document.body.appendChild( iframe );
  414. } );
  415. describe( 'event capturing', () => {
  416. beforeEach( () => {
  417. document.body.appendChild( node );
  418. } );
  419. afterEach( () => {
  420. document.body.removeChild( node );
  421. } );
  422. it( 'should remove listeners when re–listen', () => {
  423. const spy = testUtils.sinon.spy();
  424. domEmitter.listenTo( document, 'test', spy, { useCapture: true } );
  425. node.dispatchEvent( new Event( 'test' ) );
  426. sinon.assert.calledOnce( spy );
  427. domEmitter.stopListening( document, 'test' );
  428. node.dispatchEvent( new Event( 'test' ) );
  429. sinon.assert.calledOnce( spy );
  430. // Listen again.
  431. domEmitter.listenTo( document, 'test', spy, { useCapture: true } );
  432. node.dispatchEvent( new Event( 'test', { bubbles: false } ) );
  433. sinon.assert.calledTwice( spy );
  434. domEmitter.stopListening( document, 'test' );
  435. node.dispatchEvent( new Event( 'test', { bubbles: false } ) );
  436. sinon.assert.calledTwice( spy );
  437. } );
  438. } );
  439. } );
  440. } );