8
0

emittermixin.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. let emitter, listener;
  9. beforeEach( refreshEmitter );
  10. describe( 'fire', () => {
  11. it( 'should execute callbacks in the right order without priority', () => {
  12. let spy1 = sinon.spy().named( 1 );
  13. let spy2 = sinon.spy().named( 2 );
  14. let spy3 = sinon.spy().named( 3 );
  15. emitter.on( 'test', spy1 );
  16. emitter.on( 'test', spy2 );
  17. emitter.on( 'test', spy3 );
  18. emitter.fire( 'test' );
  19. sinon.assert.callOrder( spy1, spy2, spy3 );
  20. } );
  21. it( 'should execute callbacks in the right order with priority defined', () => {
  22. let spy1 = sinon.spy().named( 1 );
  23. let spy2 = sinon.spy().named( 2 );
  24. let spy3 = sinon.spy().named( 3 );
  25. let spy4 = sinon.spy().named( 4 );
  26. let spy5 = sinon.spy().named( 5 );
  27. emitter.on( 'test', spy2, null, 9 );
  28. emitter.on( 'test', spy3 ); // Defaults to 10.
  29. emitter.on( 'test', spy4, null, 11 );
  30. emitter.on( 'test', spy1, null, -1 );
  31. emitter.on( 'test', spy5, null, 11 );
  32. emitter.fire( 'test' );
  33. sinon.assert.callOrder( spy1, spy2, spy3, spy4, spy5 );
  34. } );
  35. it( 'should pass arguments to callbacks', () => {
  36. let spy1 = sinon.spy();
  37. let spy2 = sinon.spy();
  38. emitter.on( 'test', spy1 );
  39. emitter.on( 'test', spy2 );
  40. emitter.fire( 'test', 1, 'b', true );
  41. sinon.assert.calledWithExactly( spy1, sinon.match.instanceOf( EventInfo ), 1, 'b', true );
  42. sinon.assert.calledWithExactly( spy2, sinon.match.instanceOf( EventInfo ), 1, 'b', true );
  43. } );
  44. it( 'should pass proper context to callbacks', () => {
  45. let ctx1 = {};
  46. let ctx2 = {};
  47. let spy1 = sinon.spy();
  48. let spy2 = sinon.spy();
  49. let spy3 = sinon.spy();
  50. emitter.on( 'test', spy1, ctx1 );
  51. emitter.on( 'test', spy2, ctx2 );
  52. emitter.on( 'test', spy3 );
  53. emitter.fire( 'test' );
  54. sinon.assert.calledOn( spy1, ctx1 );
  55. sinon.assert.calledOn( spy2, ctx2 );
  56. sinon.assert.calledOn( spy3, emitter );
  57. } );
  58. it( 'should fire the right event', () => {
  59. let spy1 = sinon.spy();
  60. let spy2 = sinon.spy();
  61. emitter.on( '1', spy1 );
  62. emitter.on( '2', spy2 );
  63. emitter.fire( '2' );
  64. sinon.assert.notCalled( spy1 );
  65. sinon.assert.called( spy2 );
  66. } );
  67. it( 'should execute callbacks many times', () => {
  68. let spy = sinon.spy();
  69. emitter.on( 'test', spy );
  70. emitter.fire( 'test' );
  71. emitter.fire( 'test' );
  72. emitter.fire( 'test' );
  73. sinon.assert.calledThrice( spy );
  74. } );
  75. it( 'should do nothing for a non listened event', () => {
  76. emitter.fire( 'test' );
  77. } );
  78. it( 'should accept the same callback many times', () => {
  79. let spy = sinon.spy();
  80. emitter.on( 'test', spy );
  81. emitter.on( 'test', spy );
  82. emitter.on( 'test', spy );
  83. emitter.fire( 'test' );
  84. sinon.assert.calledThrice( spy );
  85. } );
  86. it( 'should not fire callbacks for an event that were added while firing that event', () => {
  87. let spy = sinon.spy();
  88. emitter.on( 'test', () => {
  89. emitter.on( 'test', spy );
  90. } );
  91. emitter.fire( 'test' );
  92. sinon.assert.notCalled( spy );
  93. } );
  94. it( 'should correctly fire callbacks for namespaced events', () => {
  95. let spyFoo = sinon.spy();
  96. let spyBar = sinon.spy();
  97. let spyAbc = sinon.spy();
  98. let spyFoo2 = sinon.spy();
  99. // Mess up with callbacks order to check whether they are called in adding order.
  100. emitter.on( 'foo', spyFoo );
  101. emitter.on( 'foo:bar:abc', spyAbc );
  102. emitter.on( 'foo:bar', spyBar );
  103. // This tests whether generic callbacks are also added to specific callbacks lists.
  104. emitter.on( 'foo', spyFoo2 );
  105. // All four callbacks should be fired.
  106. emitter.fire( 'foo:bar:abc' );
  107. sinon.assert.callOrder( spyFoo, spyAbc, spyBar, spyFoo2 );
  108. sinon.assert.calledOnce( spyFoo );
  109. sinon.assert.calledOnce( spyAbc );
  110. sinon.assert.calledOnce( spyBar );
  111. sinon.assert.calledOnce( spyFoo2 );
  112. // Only callbacks for foo and foo:bar event should be called.
  113. emitter.fire( 'foo:bar' );
  114. sinon.assert.calledOnce( spyAbc );
  115. sinon.assert.calledTwice( spyFoo );
  116. sinon.assert.calledTwice( spyBar );
  117. sinon.assert.calledTwice( spyFoo2 );
  118. // Only callback for foo should be called as foo:abc has not been registered.
  119. // Still, foo is a valid, existing namespace.
  120. emitter.fire( 'foo:abc' );
  121. sinon.assert.calledOnce( spyAbc );
  122. sinon.assert.calledTwice( spyBar );
  123. sinon.assert.calledThrice( spyFoo );
  124. sinon.assert.calledThrice( spyFoo2 );
  125. } );
  126. } );
  127. describe( 'on', () => {
  128. it( 'should stop()', () => {
  129. let spy1 = sinon.spy();
  130. let spy2 = sinon.spy();
  131. let spy3 = sinon.spy( ( event ) => {
  132. event.stop();
  133. } );
  134. emitter.on( 'test', spy1 );
  135. emitter.on( 'test', spy2 );
  136. emitter.on( 'test', spy3 );
  137. emitter.on( 'test', sinon.stub().throws() );
  138. emitter.on( 'test', sinon.stub().throws() );
  139. emitter.fire( 'test' );
  140. sinon.assert.called( spy1 );
  141. sinon.assert.called( spy2 );
  142. sinon.assert.called( spy3 );
  143. } );
  144. it( 'should take a callback off()', () => {
  145. let spy1 = sinon.spy();
  146. let spy2 = sinon.spy( ( event ) => {
  147. event.off();
  148. } );
  149. let spy3 = sinon.spy();
  150. emitter.on( 'test', spy1 );
  151. emitter.on( 'test', spy2 );
  152. emitter.on( 'test', spy3 );
  153. emitter.fire( 'test' );
  154. emitter.fire( 'test' );
  155. sinon.assert.calledTwice( spy1 );
  156. sinon.assert.calledOnce( spy2 );
  157. sinon.assert.calledTwice( spy3 );
  158. } );
  159. it( 'should take the callback off() even after stop()', () => {
  160. let spy1 = sinon.spy( ( event ) => {
  161. event.stop();
  162. event.off();
  163. } );
  164. let spy2 = sinon.spy();
  165. emitter.on( 'test', spy1 );
  166. emitter.on( 'test', spy2 );
  167. emitter.fire( 'test' );
  168. emitter.fire( 'test' );
  169. sinon.assert.calledOnce( spy1 );
  170. sinon.assert.calledOnce( spy2 );
  171. } );
  172. } );
  173. describe( 'once', () => {
  174. it( 'should be called just once', () => {
  175. let spy1 = sinon.spy();
  176. let spy2 = sinon.spy();
  177. let spy3 = sinon.spy();
  178. emitter.on( 'test', spy1 );
  179. emitter.once( 'test', spy2 );
  180. emitter.on( 'test', spy3 );
  181. emitter.fire( 'test' );
  182. emitter.fire( 'test' );
  183. sinon.assert.calledTwice( spy1 );
  184. sinon.assert.calledOnce( spy2 );
  185. sinon.assert.calledTwice( spy3 );
  186. } );
  187. it( 'should have proper scope', () => {
  188. let ctx = {};
  189. let spy1 = sinon.spy();
  190. let spy2 = sinon.spy();
  191. emitter.once( 'test', spy1, ctx );
  192. emitter.once( 'test', spy2 );
  193. emitter.fire( 'test' );
  194. sinon.assert.calledOn( spy1, ctx );
  195. sinon.assert.calledOn( spy2, emitter );
  196. } );
  197. it( 'should have proper arguments', () => {
  198. let spy = sinon.spy();
  199. emitter.once( 'test', spy );
  200. emitter.fire( 'test', 1, 2, 3 );
  201. sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), 1, 2, 3 );
  202. } );
  203. } );
  204. describe( 'off', () => {
  205. it( 'should get callbacks off()', () => {
  206. let spy1 = sinon.spy();
  207. let spy2 = sinon.spy();
  208. let spy3 = sinon.spy();
  209. emitter.on( 'test', spy1 );
  210. emitter.on( 'test', spy2 );
  211. emitter.on( 'test', spy3 );
  212. emitter.fire( 'test' );
  213. emitter.off( 'test', spy2 );
  214. emitter.fire( 'test' );
  215. emitter.fire( 'test' );
  216. sinon.assert.calledThrice( spy1 );
  217. sinon.assert.calledOnce( spy2 );
  218. sinon.assert.calledThrice( spy3 );
  219. } );
  220. it( 'should not fail with unknown events', () => {
  221. emitter.off( 'test', () => {} );
  222. } );
  223. it( 'should remove all entries for the same callback', () => {
  224. let spy1 = sinon.spy().named( 1 );
  225. let spy2 = sinon.spy().named( 2 );
  226. emitter.on( 'test', spy1 );
  227. emitter.on( 'test', spy2 );
  228. emitter.on( 'test', spy1 );
  229. emitter.on( 'test', spy2 );
  230. emitter.fire( 'test' );
  231. emitter.off( 'test', spy1 );
  232. emitter.fire( 'test' );
  233. sinon.assert.callCount( spy1, 2 );
  234. sinon.assert.callCount( spy2, 4 );
  235. } );
  236. it( 'should remove the callback for given context only', () => {
  237. let spy = sinon.spy().named( 1 );
  238. let ctx1 = { ctx: 1 };
  239. let ctx2 = { ctx: 2 };
  240. emitter.on( 'test', spy, ctx1 );
  241. emitter.on( 'test', spy, ctx2 );
  242. emitter.fire( 'test' );
  243. spy.reset();
  244. emitter.off( 'test', spy, ctx1 );
  245. emitter.fire( 'test' );
  246. sinon.assert.calledOnce( spy );
  247. sinon.assert.calledOn( spy, ctx2 );
  248. } );
  249. it( 'should properly remove callbacks for namespaced events', () => {
  250. let spyFoo = sinon.spy();
  251. let spyAbc = sinon.spy();
  252. let spyBar = sinon.spy();
  253. let spyFoo2 = sinon.spy();
  254. emitter.on( 'foo', spyFoo );
  255. emitter.on( 'foo:bar:abc', spyAbc );
  256. emitter.on( 'foo:bar', spyBar );
  257. emitter.on( 'foo', spyFoo2 );
  258. emitter.off( 'foo', spyFoo );
  259. emitter.fire( 'foo:bar:abc' );
  260. sinon.assert.calledOnce( spyAbc );
  261. sinon.assert.calledOnce( spyBar );
  262. sinon.assert.calledOnce( spyFoo2 );
  263. sinon.assert.notCalled( spyFoo );
  264. emitter.fire( 'foo:bar' );
  265. sinon.assert.notCalled( spyFoo );
  266. sinon.assert.calledOnce( spyAbc );
  267. sinon.assert.calledTwice( spyBar );
  268. sinon.assert.calledTwice( spyFoo2 );
  269. emitter.fire( 'foo' );
  270. sinon.assert.notCalled( spyFoo );
  271. sinon.assert.calledOnce( spyAbc );
  272. sinon.assert.calledTwice( spyBar );
  273. sinon.assert.calledThrice( spyFoo2 );
  274. } );
  275. } );
  276. describe( 'listenTo', () => {
  277. beforeEach( refreshListener );
  278. it( 'should properly register callbacks', () => {
  279. let spy = sinon.spy();
  280. listener.listenTo( emitter, 'test', spy );
  281. emitter.fire( 'test' );
  282. sinon.assert.called( spy );
  283. } );
  284. it( 'should correctly listen to namespaced events', () => {
  285. let spyFoo = sinon.spy();
  286. let spyBar = sinon.spy();
  287. listener.listenTo( emitter, 'foo', spyFoo );
  288. listener.listenTo( emitter, 'foo:bar', spyBar );
  289. emitter.fire( 'foo:bar' );
  290. sinon.assert.calledOnce( spyFoo );
  291. sinon.assert.calledOnce( spyBar );
  292. emitter.fire( 'foo' );
  293. sinon.assert.calledTwice( spyFoo );
  294. sinon.assert.calledOnce( spyBar );
  295. } );
  296. } );
  297. describe( 'stopListening', () => {
  298. beforeEach( refreshListener );
  299. it( 'should stop listening to given event callback', () => {
  300. let spy1 = sinon.spy();
  301. let spy2 = sinon.spy();
  302. listener.listenTo( emitter, 'event1', spy1 );
  303. listener.listenTo( emitter, 'event2', spy2 );
  304. emitter.fire( 'event1' );
  305. emitter.fire( 'event2' );
  306. listener.stopListening( emitter, 'event1', spy1 );
  307. emitter.fire( 'event1' );
  308. emitter.fire( 'event2' );
  309. sinon.assert.calledOnce( spy1 );
  310. sinon.assert.calledTwice( spy2 );
  311. } );
  312. it( 'should stop listening to given event', () => {
  313. let spy1a = sinon.spy();
  314. let spy1b = sinon.spy();
  315. let spy2 = sinon.spy();
  316. listener.listenTo( emitter, 'event1', spy1a );
  317. listener.listenTo( emitter, 'event1', spy1b );
  318. listener.listenTo( emitter, 'event2', spy2 );
  319. emitter.fire( 'event1' );
  320. emitter.fire( 'event2' );
  321. listener.stopListening( emitter, 'event1' );
  322. emitter.fire( 'event1' );
  323. emitter.fire( 'event2' );
  324. sinon.assert.calledOnce( spy1a );
  325. sinon.assert.calledOnce( spy1b );
  326. sinon.assert.calledTwice( spy2 );
  327. } );
  328. it( 'should stop listening to all events from given emitter', () => {
  329. let spy1 = sinon.spy();
  330. let spy2 = sinon.spy();
  331. listener.listenTo( emitter, 'event1', spy1 );
  332. listener.listenTo( emitter, 'event2', spy2 );
  333. emitter.fire( 'event1' );
  334. emitter.fire( 'event2' );
  335. listener.stopListening( emitter );
  336. emitter.fire( 'event1' );
  337. emitter.fire( 'event2' );
  338. sinon.assert.calledOnce( spy1 );
  339. sinon.assert.calledOnce( spy2 );
  340. } );
  341. it( 'should stop listening to everything', () => {
  342. let spy1 = sinon.spy();
  343. let spy2 = sinon.spy();
  344. let emitter1 = getEmitterInstance();
  345. let emitter2 = getEmitterInstance();
  346. listener.listenTo( emitter1, 'event1', spy1 );
  347. listener.listenTo( emitter2, 'event2', spy2 );
  348. expect( listener ).to.have.property( '_listeningTo' );
  349. emitter1.fire( 'event1' );
  350. emitter2.fire( 'event2' );
  351. listener.stopListening();
  352. emitter1.fire( 'event1' );
  353. emitter2.fire( 'event2' );
  354. sinon.assert.calledOnce( spy1 );
  355. sinon.assert.calledOnce( spy2 );
  356. expect( listener ).to.not.have.property( '_listeningTo' );
  357. } );
  358. it( 'should not stop other emitters when a non-listened emitter is provided', () => {
  359. let spy = sinon.spy();
  360. let emitter1 = getEmitterInstance();
  361. let emitter2 = getEmitterInstance();
  362. listener.listenTo( emitter1, 'test', spy );
  363. listener.stopListening( emitter2 );
  364. emitter1.fire( 'test' );
  365. sinon.assert.called( spy );
  366. } );
  367. it( 'should correctly stop listening to namespaced events', () => {
  368. let spyFoo = sinon.spy();
  369. let spyBar = sinon.spy();
  370. listener.listenTo( emitter, 'foo', spyFoo );
  371. listener.listenTo( emitter, 'foo:bar', spyBar );
  372. listener.stopListening( emitter, 'foo' );
  373. emitter.fire( 'foo:bar' );
  374. sinon.assert.notCalled( spyFoo );
  375. sinon.assert.calledOnce( spyBar );
  376. } );
  377. } );
  378. function refreshEmitter() {
  379. emitter = getEmitterInstance();
  380. }
  381. function refreshListener() {
  382. listener = getEmitterInstance();
  383. }
  384. function getEmitterInstance() {
  385. return Object.create( EmitterMixin );
  386. }