emittermixin.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. const modules = bender.amd.require( 'emittermixin', 'eventinfo', 'utils' );
  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, null, 9 );
  27. emitter.on( 'test', spy3 ); // Defaults to 10.
  28. emitter.on( 'test', spy4, null, 11 );
  29. emitter.on( 'test', spy1, null, -1 );
  30. emitter.on( 'test', spy5, null, 11 );
  31. emitter.fire( 'test' );
  32. sinon.assert.callOrder( spy1, spy2, spy3, spy4, spy5 );
  33. } );
  34. it( 'should pass arguments to callbacks', () => {
  35. const EventInfo = modules.eventinfo;
  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. } );
  87. describe( 'on', () => {
  88. it( 'should stop()', () => {
  89. let spy1 = sinon.spy();
  90. let spy2 = sinon.spy();
  91. let spy3 = sinon.spy( ( event ) => {
  92. event.stop();
  93. } );
  94. emitter.on( 'test', spy1 );
  95. emitter.on( 'test', spy2 );
  96. emitter.on( 'test', spy3 );
  97. emitter.on( 'test', sinon.stub().throws() );
  98. emitter.on( 'test', sinon.stub().throws() );
  99. emitter.fire( 'test' );
  100. sinon.assert.called( spy1 );
  101. sinon.assert.called( spy2 );
  102. sinon.assert.called( spy3 );
  103. } );
  104. it( 'should take a callback off()', () => {
  105. let spy1 = sinon.spy();
  106. let spy2 = sinon.spy( ( event ) => {
  107. event.off();
  108. } );
  109. let spy3 = sinon.spy();
  110. emitter.on( 'test', spy1 );
  111. emitter.on( 'test', spy2 );
  112. emitter.on( 'test', spy3 );
  113. emitter.fire( 'test' );
  114. emitter.fire( 'test' );
  115. sinon.assert.calledTwice( spy1 );
  116. sinon.assert.calledOnce( spy2 );
  117. sinon.assert.calledTwice( spy3 );
  118. } );
  119. it( 'should take the callback off() even after stop()', () => {
  120. let spy1 = sinon.spy( ( event ) => {
  121. event.stop();
  122. event.off();
  123. } );
  124. let spy2 = sinon.spy();
  125. emitter.on( 'test', spy1 );
  126. emitter.on( 'test', spy2 );
  127. emitter.fire( 'test' );
  128. emitter.fire( 'test' );
  129. sinon.assert.calledOnce( spy1 );
  130. sinon.assert.calledOnce( spy2 );
  131. } );
  132. } );
  133. describe( 'once', () => {
  134. it( 'should be called just once', () => {
  135. let spy1 = sinon.spy();
  136. let spy2 = sinon.spy();
  137. let spy3 = sinon.spy();
  138. emitter.on( 'test', spy1 );
  139. emitter.once( 'test', spy2 );
  140. emitter.on( 'test', spy3 );
  141. emitter.fire( 'test' );
  142. emitter.fire( 'test' );
  143. sinon.assert.calledTwice( spy1 );
  144. sinon.assert.calledOnce( spy2 );
  145. sinon.assert.calledTwice( spy3 );
  146. } );
  147. it( 'should have proper scope', () => {
  148. let ctx = {};
  149. let spy1 = sinon.spy();
  150. let spy2 = sinon.spy();
  151. emitter.once( 'test', spy1, ctx );
  152. emitter.once( 'test', spy2 );
  153. emitter.fire( 'test' );
  154. sinon.assert.calledOn( spy1, ctx );
  155. sinon.assert.calledOn( spy2, emitter );
  156. } );
  157. it( 'should have proper arguments', () => {
  158. const EventInfo = modules.eventinfo;
  159. let spy = sinon.spy();
  160. emitter.once( 'test', spy );
  161. emitter.fire( 'test', 1, 2, 3 );
  162. sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), 1, 2, 3 );
  163. } );
  164. } );
  165. describe( 'off', () => {
  166. it( 'should get callbacks off()', () => {
  167. let spy1 = sinon.spy();
  168. let spy2 = sinon.spy();
  169. let spy3 = sinon.spy();
  170. emitter.on( 'test', spy1 );
  171. emitter.on( 'test', spy2 );
  172. emitter.on( 'test', spy3 );
  173. emitter.fire( 'test' );
  174. emitter.off( 'test', spy2 );
  175. emitter.fire( 'test' );
  176. emitter.fire( 'test' );
  177. sinon.assert.calledThrice( spy1 );
  178. sinon.assert.calledOnce( spy2 );
  179. sinon.assert.calledThrice( spy3 );
  180. } );
  181. it( 'should not fail with unknown events', () => {
  182. emitter.off( 'test', () => {} );
  183. } );
  184. it( 'should remove all entries for the same callback', () => {
  185. let spy1 = sinon.spy().named( 1 );
  186. let spy2 = sinon.spy().named( 2 );
  187. emitter.on( 'test', spy1 );
  188. emitter.on( 'test', spy2 );
  189. emitter.on( 'test', spy1 );
  190. emitter.on( 'test', spy2 );
  191. emitter.fire( 'test' );
  192. emitter.off( 'test', spy1 );
  193. emitter.fire( 'test' );
  194. sinon.assert.callCount( spy1, 2 );
  195. sinon.assert.callCount( spy2, 4 );
  196. } );
  197. it( 'should remove the callback for a specific context only', () => {
  198. let spy = sinon.spy().named( 1 );
  199. let ctx1 = { ctx: 1 };
  200. let ctx2 = { ctx: 2 };
  201. emitter.on( 'test', spy, ctx1 );
  202. emitter.on( 'test', spy, ctx2 );
  203. emitter.fire( 'test' );
  204. spy.reset();
  205. emitter.off( 'test', spy, ctx1 );
  206. emitter.fire( 'test' );
  207. sinon.assert.calledOnce( spy );
  208. sinon.assert.calledOn( spy, ctx2 );
  209. } );
  210. } );
  211. describe( 'listenTo', () => {
  212. beforeEach( refreshListener );
  213. it( 'should properly register callbacks', () => {
  214. let spy = sinon.spy();
  215. listener.listenTo( emitter, 'test', spy );
  216. emitter.fire( 'test' );
  217. sinon.assert.called( spy );
  218. } );
  219. } );
  220. describe( 'stopListening', () => {
  221. beforeEach( refreshListener );
  222. it( 'should stop listening to a specific event callback', () => {
  223. let spy1 = sinon.spy();
  224. let spy2 = sinon.spy();
  225. listener.listenTo( emitter, 'event1', spy1 );
  226. listener.listenTo( emitter, 'event2', spy2 );
  227. emitter.fire( 'event1' );
  228. emitter.fire( 'event2' );
  229. listener.stopListening( emitter, 'event1', spy1 );
  230. emitter.fire( 'event1' );
  231. emitter.fire( 'event2' );
  232. sinon.assert.calledOnce( spy1 );
  233. sinon.assert.calledTwice( spy2 );
  234. } );
  235. it( 'should stop listening to an specific event', () => {
  236. let spy1a = sinon.spy();
  237. let spy1b = sinon.spy();
  238. let spy2 = sinon.spy();
  239. listener.listenTo( emitter, 'event1', spy1a );
  240. listener.listenTo( emitter, 'event1', spy1b );
  241. listener.listenTo( emitter, 'event2', spy2 );
  242. emitter.fire( 'event1' );
  243. emitter.fire( 'event2' );
  244. listener.stopListening( emitter, 'event1' );
  245. emitter.fire( 'event1' );
  246. emitter.fire( 'event2' );
  247. sinon.assert.calledOnce( spy1a );
  248. sinon.assert.calledOnce( spy1b );
  249. sinon.assert.calledTwice( spy2 );
  250. } );
  251. it( 'should stop listening to all events from a specific emitter', () => {
  252. let spy1 = sinon.spy();
  253. let spy2 = sinon.spy();
  254. listener.listenTo( emitter, 'event1', spy1 );
  255. listener.listenTo( emitter, 'event2', spy2 );
  256. emitter.fire( 'event1' );
  257. emitter.fire( 'event2' );
  258. listener.stopListening( emitter );
  259. emitter.fire( 'event1' );
  260. emitter.fire( 'event2' );
  261. sinon.assert.calledOnce( spy1 );
  262. sinon.assert.calledOnce( spy2 );
  263. } );
  264. it( 'should stop listening to everything', () => {
  265. let spy1 = sinon.spy();
  266. let spy2 = sinon.spy();
  267. let emitter1 = getEmitterInstance();
  268. let emitter2 = getEmitterInstance();
  269. listener.listenTo( emitter1, 'event1', spy1 );
  270. listener.listenTo( emitter2, 'event2', spy2 );
  271. expect( listener ).to.have.property( '_listeningTo' );
  272. emitter1.fire( 'event1' );
  273. emitter2.fire( 'event2' );
  274. listener.stopListening();
  275. emitter1.fire( 'event1' );
  276. emitter2.fire( 'event2' );
  277. sinon.assert.calledOnce( spy1 );
  278. sinon.assert.calledOnce( spy2 );
  279. expect( listener ).to.not.have.property( '_listeningTo' );
  280. } );
  281. it( 'should not stop other emitters when a non-listened emitter is provided', () => {
  282. let spy = sinon.spy();
  283. let emitter1 = getEmitterInstance();
  284. let emitter2 = getEmitterInstance();
  285. listener.listenTo( emitter1, 'test', spy );
  286. listener.stopListening( emitter2 );
  287. emitter1.fire( 'test' );
  288. sinon.assert.called( spy );
  289. } );
  290. } );
  291. function refreshEmitter() {
  292. emitter = getEmitterInstance();
  293. }
  294. function refreshListener() {
  295. listener = getEmitterInstance();
  296. }
  297. function getEmitterInstance() {
  298. const EmitterMixin = modules.emittermixin;
  299. let utils = modules.utils;
  300. return utils.extend( {}, EmitterMixin );
  301. }