8
0

emittermixin.js 9.5 KB

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