emitter.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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();
  13. var spy2 = sinon.spy();
  14. var spy3 = sinon.spy();
  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();
  23. var spy2 = sinon.spy();
  24. var spy3 = sinon.spy();
  25. var spy4 = sinon.spy();
  26. var spy5 = sinon.spy();
  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. } );
  121. describe( 'once', function() {
  122. it( 'should be called just once', function() {
  123. var spy1 = sinon.spy();
  124. var spy2 = sinon.spy();
  125. var spy3 = sinon.spy();
  126. emitter.on( 'test', spy1 );
  127. emitter.once( 'test', spy2 );
  128. emitter.on( 'test', spy3 );
  129. emitter.fire( 'test' );
  130. emitter.fire( 'test' );
  131. sinon.assert.calledTwice( spy1 );
  132. sinon.assert.calledOnce( spy2 );
  133. sinon.assert.calledTwice( spy3 );
  134. } );
  135. it( 'should have proper scope', function() {
  136. var ctx = {};
  137. var spy1 = sinon.spy();
  138. var spy2 = sinon.spy();
  139. emitter.once( 'test', spy1, ctx );
  140. emitter.once( 'test', spy2 );
  141. emitter.fire( 'test' );
  142. sinon.assert.calledOn( spy1, ctx );
  143. sinon.assert.calledOn( spy2, emitter );
  144. } );
  145. it( 'should have proper arguments', function() {
  146. var EventInfo = modules.eventinfo;
  147. var spy = sinon.spy();
  148. emitter.once( 'test', spy );
  149. emitter.fire( 'test', 1, 2, 3 );
  150. sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), 1, 2, 3 );
  151. } );
  152. } );
  153. describe( 'off', function() {
  154. it( 'should get callbacks off', function() {
  155. var spy1 = sinon.spy();
  156. var spy2 = sinon.spy();
  157. var spy3 = sinon.spy();
  158. emitter.on( 'test', spy1 );
  159. emitter.on( 'test', spy2 );
  160. emitter.on( 'test', spy3 );
  161. emitter.fire( 'test' );
  162. emitter.off( 'test', spy2 );
  163. emitter.fire( 'test' );
  164. emitter.fire( 'test' );
  165. sinon.assert.calledThrice( spy1 );
  166. sinon.assert.calledOnce( spy2 );
  167. sinon.assert.calledThrice( spy3 );
  168. } );
  169. it( 'should no fail with unknown events', function() {
  170. emitter.off( 'test', function() {} );
  171. } );
  172. } );
  173. describe( 'listenTo', function() {
  174. beforeEach( refreshListener );
  175. it( 'should properly register callbacks', function() {
  176. var spy = sinon.spy();
  177. listener.listenTo( emitter, 'test', spy );
  178. emitter.fire( 'test' );
  179. sinon.assert.called( spy );
  180. } );
  181. } );
  182. describe( 'stopListening', function() {
  183. beforeEach( refreshListener );
  184. it( 'should stop listening callback on event', function() {
  185. var spy1 = sinon.spy();
  186. var spy2 = sinon.spy();
  187. listener.listenTo( emitter, 'event1', spy1 );
  188. listener.listenTo( emitter, 'event2', spy2 );
  189. emitter.fire( 'event1' );
  190. emitter.fire( 'event2' );
  191. listener.stopListening( emitter, 'event1', spy1 );
  192. emitter.fire( 'event1' );
  193. emitter.fire( 'event2' );
  194. sinon.assert.calledOnce( spy1 );
  195. sinon.assert.calledTwice( spy2 );
  196. } );
  197. it( 'should stop listening event', function() {
  198. var spy1a = sinon.spy();
  199. var spy1b = sinon.spy();
  200. var spy2 = sinon.spy();
  201. listener.listenTo( emitter, 'event1', spy1a );
  202. listener.listenTo( emitter, 'event1', spy1b );
  203. listener.listenTo( emitter, 'event2', spy2 );
  204. emitter.fire( 'event1' );
  205. emitter.fire( 'event2' );
  206. listener.stopListening( emitter, 'event1' );
  207. emitter.fire( 'event1' );
  208. emitter.fire( 'event2' );
  209. sinon.assert.calledOnce( spy1a );
  210. sinon.assert.calledOnce( spy1b );
  211. sinon.assert.calledTwice( spy2 );
  212. } );
  213. it( 'should stop listening all events for emitter', function() {
  214. var spy1 = sinon.spy();
  215. var spy2 = sinon.spy();
  216. listener.listenTo( emitter, 'event1', spy1 );
  217. listener.listenTo( emitter, 'event2', spy2 );
  218. emitter.fire( 'event1' );
  219. emitter.fire( 'event2' );
  220. listener.stopListening( emitter );
  221. emitter.fire( 'event1' );
  222. emitter.fire( 'event2' );
  223. sinon.assert.calledOnce( spy1 );
  224. sinon.assert.calledOnce( spy2 );
  225. } );
  226. it( 'should stop listening everything', function() {
  227. var spy1 = sinon.spy();
  228. var spy2 = sinon.spy();
  229. var emitter1 = getEmitterInstance();
  230. var emitter2 = getEmitterInstance();
  231. listener.listenTo( emitter1, 'event1', spy1 );
  232. listener.listenTo( emitter2, 'event2', spy2 );
  233. expect( listener ).to.have.property( '_listeningTo' );
  234. emitter1.fire( 'event1' );
  235. emitter2.fire( 'event2' );
  236. listener.stopListening();
  237. emitter1.fire( 'event1' );
  238. emitter2.fire( 'event2' );
  239. sinon.assert.calledOnce( spy1 );
  240. sinon.assert.calledOnce( spy2 );
  241. expect( listener ).to.not.have.property( '_listeningTo' );
  242. } );
  243. } );
  244. function refreshEmitter() {
  245. emitter = getEmitterInstance();
  246. }
  247. function refreshListener() {
  248. listener = getEmitterInstance();
  249. }
  250. function getEmitterInstance() {
  251. var Emitter = modules.emitter;
  252. var utils = modules.utils;
  253. return utils.extend( {}, Emitter );
  254. }