8
0

emittermixin.js 13 KB

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