8
0

emittermixin.js 19 KB

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