domemittermixin.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document, Event, MouseEvent */
  6. /* bender-tags: ui */
  7. 'use strict';
  8. import testUtils from '/tests/ckeditor5/_utils/utils.js';
  9. import DOMEmitterMixin from '/ckeditor5/ui/domemittermixin.js';
  10. import EmitterMixin from '/ckeditor5/utils/emittermixin.js';
  11. let emitter, domEmitter, node;
  12. testUtils.createSinonSandbox();
  13. const getEmitterInstance = () => Object.create( EmitterMixin );
  14. const getDOMEmitterInstance = () => Object.create( DOMEmitterMixin );
  15. const getDOMNodeInstance = () => document.createElement( 'div' );
  16. function updateEmitterInstance() {
  17. emitter = getEmitterInstance();
  18. }
  19. function updateDOMEmitterInstance() {
  20. domEmitter = getDOMEmitterInstance();
  21. }
  22. function updateDOMNodeInstance() {
  23. node = getDOMNodeInstance();
  24. }
  25. describe( 'DOMEmitterMixin', () => {
  26. beforeEach( () => {
  27. updateEmitterInstance();
  28. updateDOMEmitterInstance();
  29. updateDOMNodeInstance();
  30. } );
  31. describe( 'listenTo', () => {
  32. it( 'should listen to EmitterMixin events', () => {
  33. const spy = testUtils.sinon.spy();
  34. domEmitter.listenTo( emitter, 'test', spy );
  35. emitter.fire( 'test' );
  36. sinon.assert.calledOnce( spy );
  37. } );
  38. it( 'should listen to native DOM events', () => {
  39. const spy = testUtils.sinon.spy();
  40. domEmitter.listenTo( node, 'test', spy );
  41. node.dispatchEvent( new Event( 'test' ) );
  42. sinon.assert.calledOnce( spy );
  43. } );
  44. it( 'should listen to native DOM events - window as source', () => {
  45. const spy = testUtils.sinon.spy();
  46. domEmitter.listenTo( window, 'test', spy );
  47. window.dispatchEvent( new Event( 'test' ) );
  48. sinon.assert.calledOnce( spy );
  49. } );
  50. it( 'should listen to native DOM events - document as source', () => {
  51. const spy = testUtils.sinon.spy();
  52. domEmitter.listenTo( document, 'test', spy );
  53. document.dispatchEvent( new Event( 'test' ) );
  54. sinon.assert.calledOnce( spy );
  55. } );
  56. // #187
  57. it( 'should work for DOM Nodes belonging to another window', () => {
  58. const spy = testUtils.sinon.spy();
  59. const iframe = document.createElement( 'iframe' );
  60. document.body.appendChild( iframe );
  61. const iframeNode = iframe.contentWindow.document.createElement( 'div' );
  62. domEmitter.listenTo( iframeNode, 'test', spy );
  63. iframeNode.dispatchEvent( new Event( 'test' ) );
  64. sinon.assert.calledOnce( spy );
  65. } );
  66. } );
  67. describe( 'stopListening', () => {
  68. it( 'should stop listening to a specific event callback', () => {
  69. const spy1 = testUtils.sinon.spy();
  70. const spy2 = testUtils.sinon.spy();
  71. domEmitter.listenTo( node, 'event1', spy1 );
  72. domEmitter.listenTo( node, 'event2', spy2 );
  73. node.dispatchEvent( new Event( 'event1' ) );
  74. node.dispatchEvent( new Event( 'event2' ) );
  75. domEmitter.stopListening( node, 'event1', spy1 );
  76. node.dispatchEvent( new Event( 'event1' ) );
  77. node.dispatchEvent( new Event( 'event2' ) );
  78. sinon.assert.calledOnce( spy1 );
  79. sinon.assert.calledTwice( spy2 );
  80. } );
  81. it( 'should stop listening to an specific event', () => {
  82. const spy1a = testUtils.sinon.spy();
  83. const spy1b = testUtils.sinon.spy();
  84. const spy2 = testUtils.sinon.spy();
  85. domEmitter.listenTo( node, 'event1', spy1a );
  86. domEmitter.listenTo( node, 'event1', spy1b );
  87. domEmitter.listenTo( node, 'event2', spy2 );
  88. node.dispatchEvent( new Event( 'event1' ) );
  89. node.dispatchEvent( new Event( 'event2' ) );
  90. sinon.assert.calledOnce( spy1a );
  91. sinon.assert.calledOnce( spy1b );
  92. sinon.assert.calledOnce( spy2 );
  93. domEmitter.stopListening( node, 'event1' );
  94. node.dispatchEvent( new Event( 'event1' ) );
  95. node.dispatchEvent( new Event( 'event2' ) );
  96. sinon.assert.calledOnce( spy1a );
  97. sinon.assert.calledOnce( spy1b );
  98. sinon.assert.calledTwice( spy2 );
  99. } );
  100. it( 'should stop listening to all events from a specific node', () => {
  101. const spy1 = testUtils.sinon.spy();
  102. const spy2 = testUtils.sinon.spy();
  103. domEmitter.listenTo( node, 'event1', spy1 );
  104. domEmitter.listenTo( node, 'event2', spy2 );
  105. node.dispatchEvent( new Event( 'event1' ) );
  106. node.dispatchEvent( new Event( 'event2' ) );
  107. domEmitter.stopListening( node );
  108. node.dispatchEvent( new Event( 'event1' ) );
  109. node.dispatchEvent( new Event( 'event2' ) );
  110. sinon.assert.calledOnce( spy1 );
  111. sinon.assert.calledOnce( spy2 );
  112. } );
  113. it( 'should stop listening to everything', () => {
  114. const spy1 = testUtils.sinon.spy();
  115. const spy2 = testUtils.sinon.spy();
  116. const node1 = getDOMNodeInstance();
  117. const node2 = getDOMNodeInstance();
  118. domEmitter.listenTo( node1, 'event1', spy1 );
  119. domEmitter.listenTo( node2, 'event2', spy2 );
  120. expect( domEmitter ).to.have.property( '_listeningTo' );
  121. node1.dispatchEvent( new Event( 'event1' ) );
  122. node2.dispatchEvent( new Event( 'event2' ) );
  123. domEmitter.stopListening();
  124. node1.dispatchEvent( new Event( 'event1' ) );
  125. node2.dispatchEvent( new Event( 'event2' ) );
  126. sinon.assert.calledOnce( spy1 );
  127. sinon.assert.calledOnce( spy2 );
  128. expect( domEmitter ).to.not.have.property( '_listeningTo' );
  129. } );
  130. it( 'should not stop other nodes when a non-listened node is provided', () => {
  131. const spy = testUtils.sinon.spy();
  132. const node1 = getDOMNodeInstance();
  133. const node2 = getDOMNodeInstance();
  134. domEmitter.listenTo( node1, 'test', spy );
  135. domEmitter.stopListening( node2 );
  136. node1.dispatchEvent( new Event( 'test' ) );
  137. sinon.assert.called( spy );
  138. } );
  139. it( 'should pass DOM Event data to the listener', () => {
  140. const spy = testUtils.sinon.spy();
  141. const node = getDOMNodeInstance();
  142. domEmitter.listenTo( node, 'click', spy );
  143. const mouseEvent = new MouseEvent( 'click', {
  144. screenX: 10,
  145. screenY: 20
  146. } );
  147. node.dispatchEvent( mouseEvent );
  148. sinon.assert.calledOnce( spy );
  149. expect( spy.args[ 0 ][ 1 ] ).to.be.equal( mouseEvent );
  150. } );
  151. it( 'should detach native DOM event listener proxy, specific event', () => {
  152. const spy1a = testUtils.sinon.spy();
  153. const spy1b = testUtils.sinon.spy();
  154. domEmitter.listenTo( node, 'test', spy1a );
  155. const proxyEmitter = domEmitter._getProxyEmitter( node );
  156. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  157. node.dispatchEvent( new Event( 'test' ) );
  158. sinon.assert.calledOnce( spy1a );
  159. sinon.assert.calledOnce( spy2 );
  160. domEmitter.stopListening( node, 'test' );
  161. node.dispatchEvent( new Event( 'test' ) );
  162. sinon.assert.calledOnce( spy1a );
  163. sinon.assert.calledOnce( spy2 );
  164. // Attach same event again.
  165. domEmitter.listenTo( node, 'test', spy1b );
  166. node.dispatchEvent( new Event( 'test' ) );
  167. expect( proxyEmitter ).to.be.equal( domEmitter._getProxyEmitter( node ) );
  168. sinon.assert.calledOnce( spy1a );
  169. sinon.assert.calledOnce( spy1b );
  170. sinon.assert.calledTwice( spy2 );
  171. } );
  172. it( 'should detach native DOM event listener proxy, specific callback', () => {
  173. const spy1a = testUtils.sinon.spy();
  174. const spy1b = testUtils.sinon.spy();
  175. const spy1c = testUtils.sinon.spy();
  176. domEmitter.listenTo( node, 'test', spy1a );
  177. domEmitter.listenTo( node, 'test', spy1b );
  178. const proxyEmitter = domEmitter._getProxyEmitter( node );
  179. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  180. node.dispatchEvent( new Event( 'test' ) );
  181. sinon.assert.calledOnce( spy1a );
  182. sinon.assert.calledOnce( spy1b );
  183. sinon.assert.calledOnce( spy2 );
  184. domEmitter.stopListening( node, 'test', spy1a );
  185. node.dispatchEvent( new Event( 'test' ) );
  186. sinon.assert.calledOnce( spy1a );
  187. sinon.assert.calledTwice( spy1b );
  188. sinon.assert.calledTwice( spy2 );
  189. domEmitter.stopListening( node, 'test', spy1b );
  190. node.dispatchEvent( new Event( 'test' ) );
  191. sinon.assert.calledOnce( spy1a );
  192. sinon.assert.calledTwice( spy1b );
  193. sinon.assert.calledTwice( spy2 );
  194. // Attach same event again.
  195. domEmitter.listenTo( node, 'test', spy1c );
  196. node.dispatchEvent( new Event( 'test' ) );
  197. expect( proxyEmitter ).to.be.equal( domEmitter._getProxyEmitter( node ) );
  198. sinon.assert.calledOnce( spy1a );
  199. sinon.assert.calledTwice( spy1b );
  200. sinon.assert.calledOnce( spy1c );
  201. sinon.assert.calledThrice( spy2 );
  202. } );
  203. it( 'should detach native DOM event listener proxy, specific emitter', () => {
  204. const spy1a = testUtils.sinon.spy();
  205. const spy1b = testUtils.sinon.spy();
  206. const spy1c = testUtils.sinon.spy();
  207. const spy1d = testUtils.sinon.spy();
  208. domEmitter.listenTo( node, 'test1', spy1a );
  209. domEmitter.listenTo( node, 'test2', spy1b );
  210. const proxyEmitter = domEmitter._getProxyEmitter( node );
  211. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  212. node.dispatchEvent( new Event( 'test1' ) );
  213. node.dispatchEvent( new Event( 'test2' ) );
  214. sinon.assert.calledOnce( spy1a );
  215. sinon.assert.calledOnce( spy1b );
  216. sinon.assert.calledTwice( spy2 );
  217. domEmitter.stopListening( node );
  218. node.dispatchEvent( new Event( 'test1' ) );
  219. node.dispatchEvent( new Event( 'test2' ) );
  220. sinon.assert.calledOnce( spy1a );
  221. sinon.assert.calledOnce( spy1b );
  222. sinon.assert.calledTwice( spy2 );
  223. // Attach same event again.
  224. domEmitter.listenTo( node, 'test1', spy1c );
  225. domEmitter.listenTo( node, 'test2', spy1d );
  226. // Old proxy emitter died when stopped listening to the node.
  227. const proxyEmitter2 = domEmitter._getProxyEmitter( node );
  228. const spy3 = testUtils.sinon.spy( proxyEmitter2, 'fire' );
  229. node.dispatchEvent( new Event( 'test1' ) );
  230. node.dispatchEvent( new Event( 'test2' ) );
  231. expect( proxyEmitter ).to.not.be.equal( proxyEmitter2 );
  232. sinon.assert.calledOnce( spy1a );
  233. sinon.assert.calledOnce( spy1b );
  234. sinon.assert.calledOnce( spy1c );
  235. sinon.assert.calledOnce( spy1d );
  236. sinon.assert.calledTwice( spy2 );
  237. sinon.assert.calledTwice( spy3 );
  238. } );
  239. it( 'should detach native DOM event listener proxy, everything', () => {
  240. const spy1a = testUtils.sinon.spy();
  241. const spy1b = testUtils.sinon.spy();
  242. const spy1c = testUtils.sinon.spy();
  243. const spy1d = testUtils.sinon.spy();
  244. domEmitter.listenTo( node, 'test1', spy1a );
  245. domEmitter.listenTo( node, 'test2', spy1b );
  246. const proxyEmitter = domEmitter._getProxyEmitter( node );
  247. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  248. node.dispatchEvent( new Event( 'test1' ) );
  249. node.dispatchEvent( new Event( 'test2' ) );
  250. sinon.assert.calledOnce( spy1a );
  251. sinon.assert.calledOnce( spy1b );
  252. sinon.assert.calledTwice( spy2 );
  253. domEmitter.stopListening();
  254. node.dispatchEvent( new Event( 'test1' ) );
  255. node.dispatchEvent( new Event( 'test2' ) );
  256. sinon.assert.calledOnce( spy1a );
  257. sinon.assert.calledOnce( spy1b );
  258. sinon.assert.calledTwice( spy2 );
  259. // Attach same event again.
  260. domEmitter.listenTo( node, 'test1', spy1c );
  261. domEmitter.listenTo( node, 'test2', spy1d );
  262. // Old proxy emitter died when stopped listening to the node.
  263. const proxyEmitter2 = domEmitter._getProxyEmitter( node );
  264. const spy3 = testUtils.sinon.spy( proxyEmitter2, 'fire' );
  265. node.dispatchEvent( new Event( 'test1' ) );
  266. node.dispatchEvent( new Event( 'test2' ) );
  267. expect( proxyEmitter ).to.not.be.equal( proxyEmitter2 );
  268. sinon.assert.calledOnce( spy1a );
  269. sinon.assert.calledOnce( spy1b );
  270. sinon.assert.calledOnce( spy1c );
  271. sinon.assert.calledOnce( spy1d );
  272. sinon.assert.calledTwice( spy2 );
  273. sinon.assert.calledTwice( spy3 );
  274. } );
  275. // #187
  276. it( 'should work for DOM Nodes belonging to another window', () => {
  277. const spy = testUtils.sinon.spy();
  278. const iframe = document.createElement( 'iframe' );
  279. document.body.appendChild( iframe );
  280. const iframeNode = iframe.contentWindow.document.createElement( 'div' );
  281. domEmitter.listenTo( iframeNode, 'test', spy );
  282. iframeNode.dispatchEvent( new Event( 'test' ) );
  283. domEmitter.stopListening( iframeNode );
  284. iframeNode.dispatchEvent( new Event( 'test' ) );
  285. sinon.assert.calledOnce( spy );
  286. } );
  287. } );
  288. } );