8
0

emitter.js 9.6 KB

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