emittermixin.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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/utils/emittermixin.js';
  7. import EventInfo from '/ckeditor5/utils/eventinfo.js';
  8. import extend from '/ckeditor5/utils/lib/lodash/extend.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. it( 'should correctly fire callbacks for namespaced events', () => {
  96. let spyFoo = sinon.spy();
  97. let spyBar = sinon.spy();
  98. let spyAbc = sinon.spy();
  99. let spyFoo2 = sinon.spy();
  100. // Mess up with callbacks order to check whether they are called in adding order.
  101. emitter.on( 'foo', spyFoo );
  102. emitter.on( 'foo:bar:abc', spyAbc );
  103. emitter.on( 'foo:bar', spyBar );
  104. // This tests whether generic callbacks are also added to specific callbacks lists.
  105. emitter.on( 'foo', spyFoo2 );
  106. // All four callbacks should be fired.
  107. emitter.fire( 'foo:bar:abc' );
  108. sinon.assert.callOrder( spyFoo, spyAbc, spyBar, spyFoo2 );
  109. sinon.assert.calledOnce( spyFoo );
  110. sinon.assert.calledOnce( spyAbc );
  111. sinon.assert.calledOnce( spyBar );
  112. sinon.assert.calledOnce( spyFoo2 );
  113. // Only callbacks for foo and foo:bar event should be called.
  114. emitter.fire( 'foo:bar' );
  115. sinon.assert.calledOnce( spyAbc );
  116. sinon.assert.calledTwice( spyFoo );
  117. sinon.assert.calledTwice( spyBar );
  118. sinon.assert.calledTwice( spyFoo2 );
  119. // Only callback for foo should be called as foo:abc has not been registered.
  120. // Still, foo is a valid, existing namespace.
  121. emitter.fire( 'foo:abc' );
  122. sinon.assert.calledOnce( spyAbc );
  123. sinon.assert.calledTwice( spyBar );
  124. sinon.assert.calledThrice( spyFoo );
  125. sinon.assert.calledThrice( spyFoo2 );
  126. } );
  127. } );
  128. describe( 'on', () => {
  129. it( 'should stop()', () => {
  130. let spy1 = sinon.spy();
  131. let spy2 = sinon.spy();
  132. let spy3 = sinon.spy( ( event ) => {
  133. event.stop();
  134. } );
  135. emitter.on( 'test', spy1 );
  136. emitter.on( 'test', spy2 );
  137. emitter.on( 'test', spy3 );
  138. emitter.on( 'test', sinon.stub().throws() );
  139. emitter.on( 'test', sinon.stub().throws() );
  140. emitter.fire( 'test' );
  141. sinon.assert.called( spy1 );
  142. sinon.assert.called( spy2 );
  143. sinon.assert.called( spy3 );
  144. } );
  145. it( 'should take a callback off()', () => {
  146. let spy1 = sinon.spy();
  147. let spy2 = sinon.spy( ( event ) => {
  148. event.off();
  149. } );
  150. let spy3 = sinon.spy();
  151. emitter.on( 'test', spy1 );
  152. emitter.on( 'test', spy2 );
  153. emitter.on( 'test', spy3 );
  154. emitter.fire( 'test' );
  155. emitter.fire( 'test' );
  156. sinon.assert.calledTwice( spy1 );
  157. sinon.assert.calledOnce( spy2 );
  158. sinon.assert.calledTwice( spy3 );
  159. } );
  160. it( 'should take the callback off() even after stop()', () => {
  161. let spy1 = sinon.spy( ( event ) => {
  162. event.stop();
  163. event.off();
  164. } );
  165. let spy2 = sinon.spy();
  166. emitter.on( 'test', spy1 );
  167. emitter.on( 'test', spy2 );
  168. emitter.fire( 'test' );
  169. emitter.fire( 'test' );
  170. sinon.assert.calledOnce( spy1 );
  171. sinon.assert.calledOnce( spy2 );
  172. } );
  173. } );
  174. describe( 'once', () => {
  175. it( 'should be called just once', () => {
  176. let spy1 = sinon.spy();
  177. let spy2 = sinon.spy();
  178. let spy3 = sinon.spy();
  179. emitter.on( 'test', spy1 );
  180. emitter.once( 'test', spy2 );
  181. emitter.on( 'test', spy3 );
  182. emitter.fire( 'test' );
  183. emitter.fire( 'test' );
  184. sinon.assert.calledTwice( spy1 );
  185. sinon.assert.calledOnce( spy2 );
  186. sinon.assert.calledTwice( spy3 );
  187. } );
  188. it( 'should have proper scope', () => {
  189. let ctx = {};
  190. let spy1 = sinon.spy();
  191. let spy2 = sinon.spy();
  192. emitter.once( 'test', spy1, ctx );
  193. emitter.once( 'test', spy2 );
  194. emitter.fire( 'test' );
  195. sinon.assert.calledOn( spy1, ctx );
  196. sinon.assert.calledOn( spy2, emitter );
  197. } );
  198. it( 'should have proper arguments', () => {
  199. let spy = sinon.spy();
  200. emitter.once( 'test', spy );
  201. emitter.fire( 'test', 1, 2, 3 );
  202. sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), 1, 2, 3 );
  203. } );
  204. } );
  205. describe( 'off', () => {
  206. it( 'should get callbacks off()', () => {
  207. let spy1 = sinon.spy();
  208. let spy2 = sinon.spy();
  209. let spy3 = sinon.spy();
  210. emitter.on( 'test', spy1 );
  211. emitter.on( 'test', spy2 );
  212. emitter.on( 'test', spy3 );
  213. emitter.fire( 'test' );
  214. emitter.off( 'test', spy2 );
  215. emitter.fire( 'test' );
  216. emitter.fire( 'test' );
  217. sinon.assert.calledThrice( spy1 );
  218. sinon.assert.calledOnce( spy2 );
  219. sinon.assert.calledThrice( spy3 );
  220. } );
  221. it( 'should not fail with unknown events', () => {
  222. emitter.off( 'test', () => {} );
  223. } );
  224. it( 'should remove all entries for the same callback', () => {
  225. let spy1 = sinon.spy().named( 1 );
  226. let spy2 = sinon.spy().named( 2 );
  227. emitter.on( 'test', spy1 );
  228. emitter.on( 'test', spy2 );
  229. emitter.on( 'test', spy1 );
  230. emitter.on( 'test', spy2 );
  231. emitter.fire( 'test' );
  232. emitter.off( 'test', spy1 );
  233. emitter.fire( 'test' );
  234. sinon.assert.callCount( spy1, 2 );
  235. sinon.assert.callCount( spy2, 4 );
  236. } );
  237. it( 'should remove the callback for given context only', () => {
  238. let spy = sinon.spy().named( 1 );
  239. let ctx1 = { ctx: 1 };
  240. let ctx2 = { ctx: 2 };
  241. emitter.on( 'test', spy, ctx1 );
  242. emitter.on( 'test', spy, ctx2 );
  243. emitter.fire( 'test' );
  244. spy.reset();
  245. emitter.off( 'test', spy, ctx1 );
  246. emitter.fire( 'test' );
  247. sinon.assert.calledOnce( spy );
  248. sinon.assert.calledOn( spy, ctx2 );
  249. } );
  250. it( 'should properly remove callbacks for namespaced events', () => {
  251. let spyFoo = sinon.spy();
  252. let spyAbc = sinon.spy();
  253. let spyBar = sinon.spy();
  254. let spyFoo2 = sinon.spy();
  255. emitter.on( 'foo', spyFoo );
  256. emitter.on( 'foo:bar:abc', spyAbc );
  257. emitter.on( 'foo:bar', spyBar );
  258. emitter.on( 'foo', spyFoo2 );
  259. emitter.off( 'foo', spyFoo );
  260. emitter.fire( 'foo:bar:abc' );
  261. sinon.assert.calledOnce( spyAbc );
  262. sinon.assert.calledOnce( spyBar );
  263. sinon.assert.calledOnce( spyFoo2 );
  264. sinon.assert.notCalled( spyFoo );
  265. emitter.fire( 'foo:bar' );
  266. sinon.assert.notCalled( spyFoo );
  267. sinon.assert.calledOnce( spyAbc );
  268. sinon.assert.calledTwice( spyBar );
  269. sinon.assert.calledTwice( spyFoo2 );
  270. emitter.fire( 'foo' );
  271. sinon.assert.notCalled( spyFoo );
  272. sinon.assert.calledOnce( spyAbc );
  273. sinon.assert.calledTwice( spyBar );
  274. sinon.assert.calledThrice( spyFoo2 );
  275. } );
  276. } );
  277. describe( 'listenTo', () => {
  278. beforeEach( refreshListener );
  279. it( 'should properly register callbacks', () => {
  280. let spy = sinon.spy();
  281. listener.listenTo( emitter, 'test', spy );
  282. emitter.fire( 'test' );
  283. sinon.assert.called( spy );
  284. } );
  285. it( 'should correctly listen to namespaced events', () => {
  286. let spyFoo = sinon.spy();
  287. let spyBar = sinon.spy();
  288. listener.listenTo( emitter, 'foo', spyFoo );
  289. listener.listenTo( emitter, 'foo:bar', spyBar );
  290. emitter.fire( 'foo:bar' );
  291. sinon.assert.calledOnce( spyFoo );
  292. sinon.assert.calledOnce( spyBar );
  293. emitter.fire( 'foo' );
  294. sinon.assert.calledTwice( spyFoo );
  295. sinon.assert.calledOnce( spyBar );
  296. } );
  297. } );
  298. describe( 'stopListening', () => {
  299. beforeEach( refreshListener );
  300. it( 'should stop listening to given event callback', () => {
  301. let spy1 = sinon.spy();
  302. let spy2 = sinon.spy();
  303. listener.listenTo( emitter, 'event1', spy1 );
  304. listener.listenTo( emitter, 'event2', spy2 );
  305. emitter.fire( 'event1' );
  306. emitter.fire( 'event2' );
  307. listener.stopListening( emitter, 'event1', spy1 );
  308. emitter.fire( 'event1' );
  309. emitter.fire( 'event2' );
  310. sinon.assert.calledOnce( spy1 );
  311. sinon.assert.calledTwice( spy2 );
  312. } );
  313. it( 'should stop listening to given event', () => {
  314. let spy1a = sinon.spy();
  315. let spy1b = sinon.spy();
  316. let spy2 = sinon.spy();
  317. listener.listenTo( emitter, 'event1', spy1a );
  318. listener.listenTo( emitter, 'event1', spy1b );
  319. listener.listenTo( emitter, 'event2', spy2 );
  320. emitter.fire( 'event1' );
  321. emitter.fire( 'event2' );
  322. listener.stopListening( emitter, 'event1' );
  323. emitter.fire( 'event1' );
  324. emitter.fire( 'event2' );
  325. sinon.assert.calledOnce( spy1a );
  326. sinon.assert.calledOnce( spy1b );
  327. sinon.assert.calledTwice( spy2 );
  328. } );
  329. it( 'should stop listening to all events from given emitter', () => {
  330. let spy1 = sinon.spy();
  331. let spy2 = sinon.spy();
  332. listener.listenTo( emitter, 'event1', spy1 );
  333. listener.listenTo( emitter, 'event2', spy2 );
  334. emitter.fire( 'event1' );
  335. emitter.fire( 'event2' );
  336. listener.stopListening( emitter );
  337. emitter.fire( 'event1' );
  338. emitter.fire( 'event2' );
  339. sinon.assert.calledOnce( spy1 );
  340. sinon.assert.calledOnce( spy2 );
  341. } );
  342. it( 'should stop listening to everything', () => {
  343. let spy1 = sinon.spy();
  344. let spy2 = sinon.spy();
  345. let emitter1 = getEmitterInstance();
  346. let emitter2 = getEmitterInstance();
  347. listener.listenTo( emitter1, 'event1', spy1 );
  348. listener.listenTo( emitter2, 'event2', spy2 );
  349. expect( listener ).to.have.property( '_listeningTo' );
  350. emitter1.fire( 'event1' );
  351. emitter2.fire( 'event2' );
  352. listener.stopListening();
  353. emitter1.fire( 'event1' );
  354. emitter2.fire( 'event2' );
  355. sinon.assert.calledOnce( spy1 );
  356. sinon.assert.calledOnce( spy2 );
  357. expect( listener ).to.not.have.property( '_listeningTo' );
  358. } );
  359. it( 'should not stop other emitters when a non-listened emitter is provided', () => {
  360. let spy = sinon.spy();
  361. let emitter1 = getEmitterInstance();
  362. let emitter2 = getEmitterInstance();
  363. listener.listenTo( emitter1, 'test', spy );
  364. listener.stopListening( emitter2 );
  365. emitter1.fire( 'test' );
  366. sinon.assert.called( spy );
  367. } );
  368. it( 'should correctly stop listening to namespaced events', () => {
  369. let spyFoo = sinon.spy();
  370. let spyBar = sinon.spy();
  371. listener.listenTo( emitter, 'foo', spyFoo );
  372. listener.listenTo( emitter, 'foo:bar', spyBar );
  373. listener.stopListening( emitter, 'foo' );
  374. emitter.fire( 'foo:bar' );
  375. sinon.assert.notCalled( spyFoo );
  376. sinon.assert.calledOnce( spyBar );
  377. } );
  378. } );
  379. function refreshEmitter() {
  380. emitter = getEmitterInstance();
  381. }
  382. function refreshListener() {
  383. listener = getEmitterInstance();
  384. }
  385. function getEmitterInstance() {
  386. return extend( {}, EmitterMixin );
  387. }