emittermixin.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import EmitterMixin from '/ckeditor5/core/emittermixin.js';
  7. import EventInfo from '/ckeditor5/core/eventinfo.js';
  8. import utilsObject from '/ckeditor5/core/lib/lodash/object.js';
  9. let emitter, listener;
  10. beforeEach( refreshEmitter );
  11. describe( 'fire', () => {
  12. it( 'should execute callbacks in the right order without priority', () => {
  13. let spy1 = sinon.spy().named( 1 );
  14. let spy2 = sinon.spy().named( 2 );
  15. let spy3 = sinon.spy().named( 3 );
  16. emitter.on( 'test', spy1 );
  17. emitter.on( 'test', spy2 );
  18. emitter.on( 'test', spy3 );
  19. emitter.fire( 'test' );
  20. sinon.assert.callOrder( spy1, spy2, spy3 );
  21. } );
  22. it( 'should execute callbacks in the right order with priority defined', () => {
  23. let spy1 = sinon.spy().named( 1 );
  24. let spy2 = sinon.spy().named( 2 );
  25. let spy3 = sinon.spy().named( 3 );
  26. let spy4 = sinon.spy().named( 4 );
  27. let spy5 = sinon.spy().named( 5 );
  28. emitter.on( 'test', spy2, null, 9 );
  29. emitter.on( 'test', spy3 ); // Defaults to 10.
  30. emitter.on( 'test', spy4, null, 11 );
  31. emitter.on( 'test', spy1, null, -1 );
  32. emitter.on( 'test', spy5, null, 11 );
  33. emitter.fire( 'test' );
  34. sinon.assert.callOrder( spy1, spy2, spy3, spy4, spy5 );
  35. } );
  36. it( 'should pass arguments to callbacks', () => {
  37. let spy1 = sinon.spy();
  38. let spy2 = sinon.spy();
  39. emitter.on( 'test', spy1 );
  40. emitter.on( 'test', spy2 );
  41. emitter.fire( 'test', 1, 'b', true );
  42. sinon.assert.calledWithExactly( spy1, sinon.match.instanceOf( EventInfo ), 1, 'b', true );
  43. sinon.assert.calledWithExactly( spy2, sinon.match.instanceOf( EventInfo ), 1, 'b', true );
  44. } );
  45. it( 'should pass proper context to callbacks', () => {
  46. let ctx1 = {};
  47. let ctx2 = {};
  48. let spy1 = sinon.spy();
  49. let spy2 = sinon.spy();
  50. let spy3 = sinon.spy();
  51. emitter.on( 'test', spy1, ctx1 );
  52. emitter.on( 'test', spy2, ctx2 );
  53. emitter.on( 'test', spy3 );
  54. emitter.fire( 'test' );
  55. sinon.assert.calledOn( spy1, ctx1 );
  56. sinon.assert.calledOn( spy2, ctx2 );
  57. sinon.assert.calledOn( spy3, emitter );
  58. } );
  59. it( 'should fire the right event', () => {
  60. let spy1 = sinon.spy();
  61. let spy2 = sinon.spy();
  62. emitter.on( '1', spy1 );
  63. emitter.on( '2', spy2 );
  64. emitter.fire( '2' );
  65. sinon.assert.notCalled( spy1 );
  66. sinon.assert.called( spy2 );
  67. } );
  68. it( 'should execute callbacks many times', () => {
  69. let spy = sinon.spy();
  70. emitter.on( 'test', spy );
  71. emitter.fire( 'test' );
  72. emitter.fire( 'test' );
  73. emitter.fire( 'test' );
  74. sinon.assert.calledThrice( spy );
  75. } );
  76. it( 'should do nothing for a non listened event', () => {
  77. emitter.fire( 'test' );
  78. } );
  79. it( 'should accept the same callback many times', () => {
  80. let spy = sinon.spy();
  81. emitter.on( 'test', spy );
  82. emitter.on( 'test', spy );
  83. emitter.on( 'test', spy );
  84. emitter.fire( 'test' );
  85. sinon.assert.calledThrice( spy );
  86. } );
  87. it( 'should not fire callbacks for an event that were added while firing that event', () => {
  88. let spy = sinon.spy();
  89. emitter.on( 'test', () => {
  90. emitter.on( 'test', spy );
  91. } );
  92. emitter.fire( 'test' );
  93. sinon.assert.notCalled( spy );
  94. } );
  95. } );
  96. describe( 'on', () => {
  97. it( 'should stop()', () => {
  98. let spy1 = sinon.spy();
  99. let spy2 = sinon.spy();
  100. let spy3 = sinon.spy( ( event ) => {
  101. event.stop();
  102. } );
  103. emitter.on( 'test', spy1 );
  104. emitter.on( 'test', spy2 );
  105. emitter.on( 'test', spy3 );
  106. emitter.on( 'test', sinon.stub().throws() );
  107. emitter.on( 'test', sinon.stub().throws() );
  108. emitter.fire( 'test' );
  109. sinon.assert.called( spy1 );
  110. sinon.assert.called( spy2 );
  111. sinon.assert.called( spy3 );
  112. } );
  113. it( 'should take a callback off()', () => {
  114. let spy1 = sinon.spy();
  115. let spy2 = sinon.spy( ( event ) => {
  116. event.off();
  117. } );
  118. let spy3 = sinon.spy();
  119. emitter.on( 'test', spy1 );
  120. emitter.on( 'test', spy2 );
  121. emitter.on( 'test', spy3 );
  122. emitter.fire( 'test' );
  123. emitter.fire( 'test' );
  124. sinon.assert.calledTwice( spy1 );
  125. sinon.assert.calledOnce( spy2 );
  126. sinon.assert.calledTwice( spy3 );
  127. } );
  128. it( 'should take the callback off() even after stop()', () => {
  129. let spy1 = sinon.spy( ( event ) => {
  130. event.stop();
  131. event.off();
  132. } );
  133. let spy2 = sinon.spy();
  134. emitter.on( 'test', spy1 );
  135. emitter.on( 'test', spy2 );
  136. emitter.fire( 'test' );
  137. emitter.fire( 'test' );
  138. sinon.assert.calledOnce( spy1 );
  139. sinon.assert.calledOnce( spy2 );
  140. } );
  141. } );
  142. describe( 'once', () => {
  143. it( 'should be called just once', () => {
  144. let spy1 = sinon.spy();
  145. let spy2 = sinon.spy();
  146. let spy3 = sinon.spy();
  147. emitter.on( 'test', spy1 );
  148. emitter.once( 'test', spy2 );
  149. emitter.on( 'test', spy3 );
  150. emitter.fire( 'test' );
  151. emitter.fire( 'test' );
  152. sinon.assert.calledTwice( spy1 );
  153. sinon.assert.calledOnce( spy2 );
  154. sinon.assert.calledTwice( spy3 );
  155. } );
  156. it( 'should have proper scope', () => {
  157. let ctx = {};
  158. let spy1 = sinon.spy();
  159. let spy2 = sinon.spy();
  160. emitter.once( 'test', spy1, ctx );
  161. emitter.once( 'test', spy2 );
  162. emitter.fire( 'test' );
  163. sinon.assert.calledOn( spy1, ctx );
  164. sinon.assert.calledOn( spy2, emitter );
  165. } );
  166. it( 'should have proper arguments', () => {
  167. let spy = sinon.spy();
  168. emitter.once( 'test', spy );
  169. emitter.fire( 'test', 1, 2, 3 );
  170. sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), 1, 2, 3 );
  171. } );
  172. } );
  173. describe( 'off', () => {
  174. it( 'should get callbacks off()', () => {
  175. let spy1 = sinon.spy();
  176. let spy2 = sinon.spy();
  177. let spy3 = sinon.spy();
  178. emitter.on( 'test', spy1 );
  179. emitter.on( 'test', spy2 );
  180. emitter.on( 'test', spy3 );
  181. emitter.fire( 'test' );
  182. emitter.off( 'test', spy2 );
  183. emitter.fire( 'test' );
  184. emitter.fire( 'test' );
  185. sinon.assert.calledThrice( spy1 );
  186. sinon.assert.calledOnce( spy2 );
  187. sinon.assert.calledThrice( spy3 );
  188. } );
  189. it( 'should not fail with unknown events', () => {
  190. emitter.off( 'test', () => {} );
  191. } );
  192. it( 'should remove all entries for the same callback', () => {
  193. let spy1 = sinon.spy().named( 1 );
  194. let spy2 = sinon.spy().named( 2 );
  195. emitter.on( 'test', spy1 );
  196. emitter.on( 'test', spy2 );
  197. emitter.on( 'test', spy1 );
  198. emitter.on( 'test', spy2 );
  199. emitter.fire( 'test' );
  200. emitter.off( 'test', spy1 );
  201. emitter.fire( 'test' );
  202. sinon.assert.callCount( spy1, 2 );
  203. sinon.assert.callCount( spy2, 4 );
  204. } );
  205. it( 'should remove the callback for a specific context only', () => {
  206. let spy = sinon.spy().named( 1 );
  207. let ctx1 = { ctx: 1 };
  208. let ctx2 = { ctx: 2 };
  209. emitter.on( 'test', spy, ctx1 );
  210. emitter.on( 'test', spy, ctx2 );
  211. emitter.fire( 'test' );
  212. spy.reset();
  213. emitter.off( 'test', spy, ctx1 );
  214. emitter.fire( 'test' );
  215. sinon.assert.calledOnce( spy );
  216. sinon.assert.calledOn( spy, ctx2 );
  217. } );
  218. } );
  219. describe( 'listenTo', () => {
  220. beforeEach( refreshListener );
  221. it( 'should properly register callbacks', () => {
  222. let spy = sinon.spy();
  223. listener.listenTo( emitter, 'test', spy );
  224. emitter.fire( 'test' );
  225. sinon.assert.called( spy );
  226. } );
  227. } );
  228. describe( 'stopListening', () => {
  229. beforeEach( refreshListener );
  230. it( 'should stop listening to a specific event callback', () => {
  231. let spy1 = sinon.spy();
  232. let spy2 = sinon.spy();
  233. listener.listenTo( emitter, 'event1', spy1 );
  234. listener.listenTo( emitter, 'event2', spy2 );
  235. emitter.fire( 'event1' );
  236. emitter.fire( 'event2' );
  237. listener.stopListening( emitter, 'event1', spy1 );
  238. emitter.fire( 'event1' );
  239. emitter.fire( 'event2' );
  240. sinon.assert.calledOnce( spy1 );
  241. sinon.assert.calledTwice( spy2 );
  242. } );
  243. it( 'should stop listening to an specific event', () => {
  244. let spy1a = sinon.spy();
  245. let spy1b = sinon.spy();
  246. let spy2 = sinon.spy();
  247. listener.listenTo( emitter, 'event1', spy1a );
  248. listener.listenTo( emitter, 'event1', spy1b );
  249. listener.listenTo( emitter, 'event2', spy2 );
  250. emitter.fire( 'event1' );
  251. emitter.fire( 'event2' );
  252. listener.stopListening( emitter, 'event1' );
  253. emitter.fire( 'event1' );
  254. emitter.fire( 'event2' );
  255. sinon.assert.calledOnce( spy1a );
  256. sinon.assert.calledOnce( spy1b );
  257. sinon.assert.calledTwice( spy2 );
  258. } );
  259. it( 'should stop listening to all events from a specific emitter', () => {
  260. let spy1 = sinon.spy();
  261. let spy2 = sinon.spy();
  262. listener.listenTo( emitter, 'event1', spy1 );
  263. listener.listenTo( emitter, 'event2', spy2 );
  264. emitter.fire( 'event1' );
  265. emitter.fire( 'event2' );
  266. listener.stopListening( emitter );
  267. emitter.fire( 'event1' );
  268. emitter.fire( 'event2' );
  269. sinon.assert.calledOnce( spy1 );
  270. sinon.assert.calledOnce( spy2 );
  271. } );
  272. it( 'should stop listening to everything', () => {
  273. let spy1 = sinon.spy();
  274. let spy2 = sinon.spy();
  275. let emitter1 = getEmitterInstance();
  276. let emitter2 = getEmitterInstance();
  277. listener.listenTo( emitter1, 'event1', spy1 );
  278. listener.listenTo( emitter2, 'event2', spy2 );
  279. expect( listener ).to.have.property( '_listeningTo' );
  280. emitter1.fire( 'event1' );
  281. emitter2.fire( 'event2' );
  282. listener.stopListening();
  283. emitter1.fire( 'event1' );
  284. emitter2.fire( 'event2' );
  285. sinon.assert.calledOnce( spy1 );
  286. sinon.assert.calledOnce( spy2 );
  287. expect( listener ).to.not.have.property( '_listeningTo' );
  288. } );
  289. it( 'should not stop other emitters when a non-listened emitter is provided', () => {
  290. let spy = sinon.spy();
  291. let emitter1 = getEmitterInstance();
  292. let emitter2 = getEmitterInstance();
  293. listener.listenTo( emitter1, 'test', spy );
  294. listener.stopListening( emitter2 );
  295. emitter1.fire( 'test' );
  296. sinon.assert.called( spy );
  297. } );
  298. } );
  299. function refreshEmitter() {
  300. emitter = getEmitterInstance();
  301. }
  302. function refreshListener() {
  303. listener = getEmitterInstance();
  304. }
  305. function getEmitterInstance() {
  306. return utilsObject.extend( {}, EmitterMixin );
  307. }