emittermixin.js 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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, { priority: 'high' } );
  27. emitter.on( 'test', spy3 ); // Defaults to 'normal'.
  28. emitter.on( 'test', spy4, { priority: 'low' } );
  29. emitter.on( 'test', spy1, { priority: 'highest' } );
  30. emitter.on( 'test', spy5, { priority: '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, { context: ctx1 } );
  50. emitter.on( 'test', spy2, { context: 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, { context: 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 = { context: 1 };
  238. let ctx2 = { context: 2 };
  239. emitter.on( 'test', spy, { context: ctx1 } );
  240. emitter.on( 'test', spy, { context: 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. describe( 'delegate', () => {
  378. it( 'should chain for a single event', () => {
  379. const emitter = getEmitterInstance();
  380. expect( emitter.delegate( 'foo' ) ).to.contain.keys( 'to' );
  381. } );
  382. it( 'should chain for multiple events', () => {
  383. const emitter = getEmitterInstance();
  384. expect( emitter.delegate( 'foo', 'bar' ) ).to.contain.keys( 'to' );
  385. } );
  386. describe( 'to', () => {
  387. it( 'forwards an event to another emitter', ( done ) => {
  388. const emitterA = getEmitterInstance();
  389. const emitterB = getEmitterInstance();
  390. const dataA = {};
  391. const dataB = {};
  392. emitterB.delegate( 'foo' ).to( emitterA );
  393. emitterA.on( 'foo', ( ...args ) => {
  394. assertDelegated( args, {
  395. expectedSource: emitterB,
  396. expectedName: 'foo',
  397. expectedPath: [ emitterB, emitterA ],
  398. expectedData: [ dataA, dataB ]
  399. } );
  400. done();
  401. } );
  402. emitterB.fire( 'foo', dataA, dataB );
  403. } );
  404. it( 'forwards multiple events to another emitter', () => {
  405. const emitterA = getEmitterInstance();
  406. const emitterB = getEmitterInstance();
  407. const spyFoo = sinon.spy();
  408. const spyBar = sinon.spy();
  409. const spyBaz = sinon.spy();
  410. const dataA = {};
  411. const dataB = {};
  412. emitterB.delegate( 'foo', 'bar', 'baz' ).to( emitterA );
  413. emitterA.on( 'foo', spyFoo );
  414. emitterA.on( 'bar', spyBar );
  415. emitterA.on( 'baz', spyBaz );
  416. emitterB.fire( 'foo', dataA, dataB );
  417. sinon.assert.calledOnce( spyFoo );
  418. sinon.assert.notCalled( spyBar );
  419. sinon.assert.notCalled( spyBaz );
  420. assertDelegated( spyFoo.args[ 0 ], {
  421. expectedSource: emitterB,
  422. expectedName: 'foo',
  423. expectedPath: [ emitterB, emitterA ],
  424. expectedData: [ dataA, dataB ]
  425. } );
  426. emitterB.fire( 'bar' );
  427. sinon.assert.calledOnce( spyFoo );
  428. sinon.assert.calledOnce( spyBar );
  429. sinon.assert.notCalled( spyBaz );
  430. assertDelegated( spyBar.args[ 0 ], {
  431. expectedSource: emitterB,
  432. expectedName: 'bar',
  433. expectedPath: [ emitterB, emitterA ],
  434. expectedData: []
  435. } );
  436. emitterB.fire( 'baz' );
  437. sinon.assert.calledOnce( spyFoo );
  438. sinon.assert.calledOnce( spyBar );
  439. sinon.assert.calledOnce( spyBaz );
  440. assertDelegated( spyBaz.args[ 0 ], {
  441. expectedSource: emitterB,
  442. expectedName: 'baz',
  443. expectedPath: [ emitterB, emitterA ],
  444. expectedData: []
  445. } );
  446. emitterB.fire( 'not-delegated' );
  447. sinon.assert.calledOnce( spyFoo );
  448. sinon.assert.calledOnce( spyBar );
  449. sinon.assert.calledOnce( spyBaz );
  450. } );
  451. it( 'does not forward events which are not supposed to be delegated', () => {
  452. const emitterA = getEmitterInstance();
  453. const emitterB = getEmitterInstance();
  454. const spyFoo = sinon.spy();
  455. const spyBar = sinon.spy();
  456. const spyBaz = sinon.spy();
  457. emitterB.delegate( 'foo', 'bar', 'baz' ).to( emitterA );
  458. emitterA.on( 'foo', spyFoo );
  459. emitterA.on( 'bar', spyBar );
  460. emitterA.on( 'baz', spyBaz );
  461. emitterB.fire( 'foo' );
  462. emitterB.fire( 'bar' );
  463. emitterB.fire( 'baz' );
  464. emitterB.fire( 'not-delegated' );
  465. sinon.assert.callOrder( spyFoo, spyBar, spyBaz );
  466. sinon.assert.callCount( spyFoo, 1 );
  467. sinon.assert.callCount( spyBar, 1 );
  468. sinon.assert.callCount( spyBaz, 1 );
  469. } );
  470. it( 'supports deep chain event delegation', ( done ) => {
  471. const emitterA = getEmitterInstance();
  472. const emitterB = getEmitterInstance();
  473. const emitterC = getEmitterInstance();
  474. const data = {};
  475. emitterC.delegate( 'foo' ).to( emitterB );
  476. emitterB.delegate( 'foo' ).to( emitterA );
  477. emitterA.on( 'foo', ( ...args ) => {
  478. assertDelegated( args, {
  479. expectedSource: emitterC,
  480. expectedName: 'foo',
  481. expectedPath: [ emitterC, emitterB, emitterA ],
  482. expectedData: [ data ]
  483. } );
  484. done();
  485. } );
  486. emitterC.fire( 'foo', data );
  487. } );
  488. it( 'preserves path in event delegation', ( done ) => {
  489. const data = {};
  490. const emitterA = getEmitterInstance();
  491. const emitterB = getEmitterInstance();
  492. const emitterC = getEmitterInstance();
  493. const emitterD = getEmitterInstance();
  494. emitterB.delegate( 'foo' ).to( emitterA );
  495. emitterB.delegate( 'foo' ).to( emitterC );
  496. emitterB.delegate( 'foo' ).to( emitterD );
  497. emitterD.on( 'foo', ( ...args ) => {
  498. assertDelegated( args, {
  499. expectedSource: emitterB,
  500. expectedName: 'foo',
  501. expectedPath: [ emitterB, emitterD ],
  502. expectedData: [ data ]
  503. } );
  504. done();
  505. } );
  506. emitterB.fire( 'foo', data );
  507. emitterC.fire( 'foo', data );
  508. } );
  509. it( 'executes callbacks first, then delegates further', () => {
  510. const emitterA = getEmitterInstance();
  511. const emitterB = getEmitterInstance();
  512. const spyA = sinon.spy();
  513. const spyB = sinon.spy();
  514. emitterB.delegate( 'foo' ).to( emitterA );
  515. emitterA.on( 'foo', spyA );
  516. emitterB.on( 'foo', spyB );
  517. emitterB.fire( 'foo' );
  518. sinon.assert.callOrder( spyB, spyA );
  519. } );
  520. it( 'supports delegation under a different name', () => {
  521. const emitterA = getEmitterInstance();
  522. const emitterB = getEmitterInstance();
  523. const emitterC = getEmitterInstance();
  524. const emitterD = getEmitterInstance();
  525. const spyAFoo = sinon.spy();
  526. const spyABar = sinon.spy();
  527. const spyCBaz = sinon.spy();
  528. const spyDFoo = sinon.spy();
  529. emitterB.delegate( 'foo' ).to( emitterA, 'bar' );
  530. emitterB.delegate( 'foo' ).to( emitterC, name => name + '-baz' );
  531. emitterB.delegate( 'foo' ).to( emitterD );
  532. emitterA.on( 'foo', spyAFoo );
  533. emitterA.on( 'bar', spyABar );
  534. emitterC.on( 'foo-baz', spyCBaz );
  535. emitterD.on( 'foo', spyDFoo );
  536. emitterB.fire( 'foo' );
  537. sinon.assert.calledOnce( spyABar );
  538. sinon.assert.calledOnce( spyCBaz );
  539. sinon.assert.calledOnce( spyDFoo );
  540. sinon.assert.notCalled( spyAFoo );
  541. } );
  542. it( 'supports delegation under a different name with multiple events', () => {
  543. const emitterA = getEmitterInstance();
  544. const emitterB = getEmitterInstance();
  545. const spyAFoo = sinon.spy();
  546. const spyABar = sinon.spy();
  547. const spyABaz = sinon.spy();
  548. const spyAQux = sinon.spy();
  549. emitterB.delegate( 'foo', 'bar', 'baz' ).to( emitterA, 'qux' );
  550. emitterA.on( 'foo', spyAFoo );
  551. emitterA.on( 'bar', spyABar );
  552. emitterA.on( 'baz', spyABaz );
  553. emitterA.on( 'qux', spyAQux );
  554. emitterB.fire( 'foo' );
  555. emitterB.fire( 'baz' );
  556. emitterB.fire( 'bar' );
  557. sinon.assert.notCalled( spyAFoo );
  558. sinon.assert.notCalled( spyABar );
  559. sinon.assert.notCalled( spyABaz );
  560. sinon.assert.calledThrice( spyAQux );
  561. } );
  562. it( 'supports delegation with multiple events, each under a different name', () => {
  563. const emitterA = getEmitterInstance();
  564. const emitterB = getEmitterInstance();
  565. const spyAFoo = sinon.spy();
  566. const spyABar = sinon.spy();
  567. const spyABaz = sinon.spy();
  568. const spyAFooQux = sinon.spy();
  569. const spyABarQux = sinon.spy();
  570. const spyABazQux = sinon.spy();
  571. emitterB.delegate( 'foo', 'bar', 'baz' ).to( emitterA, name => name + '-qux' );
  572. emitterA.on( 'foo', spyAFoo );
  573. emitterA.on( 'bar', spyABar );
  574. emitterA.on( 'baz', spyABaz );
  575. emitterA.on( 'foo-qux', spyAFooQux );
  576. emitterA.on( 'bar-qux', spyABarQux );
  577. emitterA.on( 'baz-qux', spyABazQux );
  578. emitterB.fire( 'foo' );
  579. emitterB.fire( 'baz' );
  580. emitterB.fire( 'bar' );
  581. sinon.assert.notCalled( spyAFoo );
  582. sinon.assert.notCalled( spyABar );
  583. sinon.assert.notCalled( spyABaz );
  584. sinon.assert.calledOnce( spyAFooQux );
  585. sinon.assert.calledOnce( spyABarQux );
  586. sinon.assert.calledOnce( spyABazQux );
  587. sinon.assert.callOrder( spyAFooQux, spyABazQux, spyABarQux );
  588. } );
  589. it( 'preserves path in delegation under a different name', ( done ) => {
  590. const data = {};
  591. const emitterA = getEmitterInstance();
  592. const emitterB = getEmitterInstance();
  593. const emitterC = getEmitterInstance();
  594. const emitterD = getEmitterInstance();
  595. emitterB.delegate( 'foo' ).to( emitterA, 'bar' );
  596. emitterB.delegate( 'foo' ).to( emitterC, 'baz' );
  597. emitterB.delegate( 'foo' ).to( emitterD );
  598. emitterD.on( 'foo', ( ...args ) => {
  599. assertDelegated( args, {
  600. expectedSource: emitterB,
  601. expectedName: 'foo',
  602. expectedPath: [ emitterB, emitterD ],
  603. expectedData: [ data ]
  604. } );
  605. done();
  606. } );
  607. emitterB.fire( 'foo', data );
  608. } );
  609. it( 'supports delegation of all events', () => {
  610. const emitterA = getEmitterInstance();
  611. const emitterB = getEmitterInstance();
  612. const spyAFoo = sinon.spy();
  613. const spyABar = sinon.spy();
  614. const spyABaz = sinon.spy();
  615. emitterB.delegate( '*' ).to( emitterA );
  616. emitterA.on( 'foo', spyAFoo );
  617. emitterA.on( 'bar', spyABar );
  618. emitterA.on( 'baz', spyABaz );
  619. emitterB.fire( 'foo' );
  620. emitterB.fire( 'baz' );
  621. emitterB.fire( 'bar' );
  622. sinon.assert.callOrder( spyAFoo, spyABaz, spyABar );
  623. } );
  624. it( 'supports delegation of all events under different names', () => {
  625. const emitterA = getEmitterInstance();
  626. const emitterB = getEmitterInstance();
  627. const spyAFoo = sinon.spy();
  628. const spyABar = sinon.spy();
  629. const spyABaz = sinon.spy();
  630. const spyAFooDel = sinon.spy();
  631. const spyABarDel = sinon.spy();
  632. const spyABazDel = sinon.spy();
  633. emitterB.delegate( '*' ).to( emitterA, name => name + '-delegated' );
  634. emitterA.on( 'foo', spyAFoo );
  635. emitterA.on( 'bar', spyABar );
  636. emitterA.on( 'baz', spyABaz );
  637. emitterA.on( 'foo-delegated', spyAFooDel );
  638. emitterA.on( 'bar-delegated', spyABarDel );
  639. emitterA.on( 'baz-delegated', spyABazDel );
  640. emitterB.fire( 'foo' );
  641. emitterB.fire( 'baz' );
  642. emitterB.fire( 'bar' );
  643. sinon.assert.notCalled( spyAFoo );
  644. sinon.assert.notCalled( spyABar );
  645. sinon.assert.notCalled( spyABaz );
  646. sinon.assert.callOrder( spyAFooDel, spyABazDel, spyABarDel );
  647. } );
  648. } );
  649. } );
  650. describe( 'stopDelegating', () => {
  651. it( 'passes if no delegation was set', () => {
  652. expect( () => {
  653. getEmitterInstance().stopDelegating();
  654. } ).to.not.throw();
  655. } );
  656. it( 'stops delegating all events to all emitters', () => {
  657. const emitterA = getEmitterInstance();
  658. const emitterB = getEmitterInstance();
  659. const emitterC = getEmitterInstance();
  660. const spyFoo = sinon.spy();
  661. const spyBar = sinon.spy();
  662. emitterA.delegate( 'foo' ).to( emitterB );
  663. emitterA.delegate( 'bar' ).to( emitterC );
  664. emitterB.on( 'foo', spyFoo );
  665. emitterC.on( 'bar', spyBar );
  666. emitterA.fire( 'foo' );
  667. emitterA.fire( 'bar' );
  668. sinon.assert.callOrder( spyFoo, spyBar );
  669. emitterA.stopDelegating();
  670. emitterA.fire( 'foo' );
  671. emitterA.fire( 'bar' );
  672. sinon.assert.callOrder( spyFoo, spyBar );
  673. } );
  674. it( 'stops delegating a specific event to all emitters', () => {
  675. const emitterA = getEmitterInstance();
  676. const emitterB = getEmitterInstance();
  677. const emitterC = getEmitterInstance();
  678. const spyFooB = sinon.spy();
  679. const spyFooC = sinon.spy();
  680. const spyBarC = sinon.spy();
  681. emitterA.delegate( 'foo' ).to( emitterB );
  682. emitterA.delegate( 'foo' ).to( emitterC );
  683. emitterA.delegate( 'bar' ).to( emitterC );
  684. emitterB.on( 'foo', spyFooB );
  685. emitterC.on( 'foo', spyFooC );
  686. emitterC.on( 'bar', spyBarC );
  687. emitterA.fire( 'foo' );
  688. emitterA.fire( 'bar' );
  689. sinon.assert.callOrder( spyFooB, spyFooC, spyBarC );
  690. emitterA.stopDelegating( 'foo' );
  691. emitterA.fire( 'foo' );
  692. emitterA.fire( 'bar' );
  693. sinon.assert.callOrder( spyFooB, spyFooC, spyBarC, spyBarC );
  694. } );
  695. it( 'stops delegating a specific event to a specific emitter', () => {
  696. const emitterA = getEmitterInstance();
  697. const emitterB = getEmitterInstance();
  698. const emitterC = getEmitterInstance();
  699. const spyFooB = sinon.spy();
  700. const spyFooC = sinon.spy();
  701. emitterA.delegate( 'foo' ).to( emitterB );
  702. emitterA.delegate( 'foo' ).to( emitterC );
  703. emitterB.on( 'foo', spyFooB );
  704. emitterC.on( 'foo', spyFooC );
  705. emitterA.fire( 'foo' );
  706. sinon.assert.callOrder( spyFooB, spyFooC );
  707. emitterA.stopDelegating( 'foo', emitterC );
  708. emitterA.fire( 'foo' );
  709. sinon.assert.callOrder( spyFooB, spyFooC, spyFooB );
  710. } );
  711. it( 'stops delegating a specific event under a different name to a specific emitter', () => {
  712. const emitterA = getEmitterInstance();
  713. const emitterB = getEmitterInstance();
  714. const emitterC = getEmitterInstance();
  715. const spyFooB = sinon.spy();
  716. const spyFooC = sinon.spy();
  717. emitterA.delegate( 'foo' ).to( emitterB );
  718. emitterA.delegate( 'foo' ).to( emitterC, 'bar' );
  719. emitterB.on( 'foo', spyFooB );
  720. emitterC.on( 'bar', spyFooC );
  721. emitterA.fire( 'foo' );
  722. sinon.assert.callOrder( spyFooB, spyFooC );
  723. emitterA.stopDelegating( 'foo', emitterC );
  724. emitterA.fire( 'foo' );
  725. sinon.assert.callOrder( spyFooB, spyFooC, spyFooB );
  726. } );
  727. it( 'stops delegating all ("*")', () => {
  728. const emitterA = getEmitterInstance();
  729. const emitterB = getEmitterInstance();
  730. const emitterC = getEmitterInstance();
  731. const spyAFoo = sinon.spy();
  732. const spyABar = sinon.spy();
  733. const spyCFoo = sinon.spy();
  734. const spyCBar = sinon.spy();
  735. emitterB.delegate( '*' ).to( emitterA );
  736. emitterB.delegate( '*' ).to( emitterC );
  737. emitterA.on( 'foo', spyAFoo );
  738. emitterA.on( 'bar', spyABar );
  739. emitterC.on( 'foo', spyCFoo );
  740. emitterC.on( 'bar', spyCBar );
  741. emitterB.fire( 'foo' );
  742. emitterB.fire( 'bar' );
  743. sinon.assert.calledOnce( spyAFoo );
  744. sinon.assert.calledOnce( spyABar );
  745. sinon.assert.calledOnce( spyCFoo );
  746. sinon.assert.calledOnce( spyCBar );
  747. emitterB.stopDelegating( '*' );
  748. emitterB.fire( 'foo' );
  749. emitterB.fire( 'bar' );
  750. sinon.assert.calledOnce( spyAFoo );
  751. sinon.assert.calledOnce( spyABar );
  752. sinon.assert.calledOnce( spyCFoo );
  753. sinon.assert.calledOnce( spyCBar );
  754. } );
  755. it( 'stops delegating all ("*") to a specific emitter', () => {
  756. const emitterA = getEmitterInstance();
  757. const emitterB = getEmitterInstance();
  758. const emitterC = getEmitterInstance();
  759. const spyAFoo = sinon.spy();
  760. const spyABar = sinon.spy();
  761. const spyCFoo = sinon.spy();
  762. const spyCBar = sinon.spy();
  763. emitterB.delegate( '*' ).to( emitterA );
  764. emitterB.delegate( 'foo' ).to( emitterC );
  765. emitterA.on( 'foo', spyAFoo );
  766. emitterA.on( 'bar', spyABar );
  767. emitterC.on( 'foo', spyCFoo );
  768. emitterC.on( 'bar', spyCBar );
  769. emitterB.fire( 'foo' );
  770. emitterB.fire( 'bar' );
  771. sinon.assert.calledOnce( spyAFoo );
  772. sinon.assert.calledOnce( spyABar );
  773. sinon.assert.calledOnce( spyCFoo );
  774. sinon.assert.notCalled( spyCBar );
  775. emitterB.stopDelegating( '*', emitterA );
  776. emitterB.fire( 'foo' );
  777. emitterB.fire( 'bar' );
  778. sinon.assert.calledOnce( spyAFoo );
  779. sinon.assert.calledOnce( spyABar );
  780. sinon.assert.calledTwice( spyCFoo );
  781. sinon.assert.notCalled( spyCBar );
  782. } );
  783. it( 'passes when stopping delegation of a specific event which has never been delegated', () => {
  784. const emitterA = getEmitterInstance();
  785. const emitterB = getEmitterInstance();
  786. expect( () => {
  787. emitterA.stopDelegating( 'bar' );
  788. emitterA.stopDelegating( 'bar', emitterB );
  789. } ).to.not.throw();
  790. } );
  791. it( 'passes when stopping delegation of a specific event to an emitter which wasn\'t a destination', () => {
  792. const emitterA = getEmitterInstance();
  793. const emitterB = getEmitterInstance();
  794. const emitterC = getEmitterInstance();
  795. emitterA.delegate( 'foo' ).to( emitterB );
  796. expect( () => {
  797. emitterA.stopDelegating( 'foo', emitterC );
  798. } ).to.not.throw();
  799. } );
  800. it( 'passes when stopping delegation of a specific event to a specific emitter which has never been delegated', () => {
  801. const emitterA = getEmitterInstance();
  802. const emitterB = getEmitterInstance();
  803. const emitterC = getEmitterInstance();
  804. emitterA.delegate( 'foo' ).to( emitterB );
  805. expect( () => {
  806. emitterA.stopDelegating( 'bar', emitterC );
  807. } ).to.not.throw();
  808. } );
  809. } );
  810. function refreshEmitter() {
  811. emitter = getEmitterInstance();
  812. }
  813. function refreshListener() {
  814. listener = getEmitterInstance();
  815. }
  816. function getEmitterInstance() {
  817. return Object.create( EmitterMixin );
  818. }
  819. function assertDelegated( evtArgs, { expectedName, expectedSource, expectedPath, expectedData } ) {
  820. const evtInfo = evtArgs[ 0 ];
  821. expect( evtInfo.name ).to.equal( expectedName );
  822. expect( evtInfo.source ).to.equal( expectedSource );
  823. expect( evtInfo.path ).to.deep.equal( expectedPath );
  824. expect( evtArgs.slice( 1 ) ).to.deep.equal( expectedData );
  825. }