emittermixin.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { default as EmitterMixin, _getEmitterListenedTo, _getEmitterId, _setEmitterId } from '../src/emittermixin';
  6. import EventInfo from '../src/eventinfo';
  7. describe( 'EmitterMixin', () => {
  8. let emitter, listener;
  9. beforeEach( () => {
  10. emitter = getEmitterInstance();
  11. listener = getEmitterInstance();
  12. } );
  13. describe( 'fire', () => {
  14. it( 'should execute callbacks in the right order without priority', () => {
  15. const spy1 = sinon.spy().named( 1 );
  16. const spy2 = sinon.spy().named( 2 );
  17. const spy3 = sinon.spy().named( 3 );
  18. emitter.on( 'test', spy1 );
  19. emitter.on( 'test', spy2 );
  20. emitter.on( 'test', spy3 );
  21. emitter.fire( 'test' );
  22. sinon.assert.callOrder( spy1, spy2, spy3 );
  23. } );
  24. it( 'should execute callbacks in the right order with priority defined', () => {
  25. const spy1 = sinon.spy().named( 1 );
  26. const spy2 = sinon.spy().named( 2 );
  27. const spy3 = sinon.spy().named( 3 );
  28. const spy4 = sinon.spy().named( 4 );
  29. const spy5 = sinon.spy().named( 5 );
  30. emitter.on( 'test', spy2, { priority: 'high' } );
  31. emitter.on( 'test', spy3 ); // Defaults to 'normal'.
  32. emitter.on( 'test', spy4, { priority: 'low' } );
  33. emitter.on( 'test', spy1, { priority: 'highest' } );
  34. emitter.on( 'test', spy5, { priority: 'lowest' } );
  35. emitter.fire( 'test' );
  36. sinon.assert.callOrder( spy1, spy2, spy3, spy4, spy5 );
  37. } );
  38. it( 'should pass arguments to callbacks', () => {
  39. const spy1 = sinon.spy();
  40. const spy2 = sinon.spy();
  41. emitter.on( 'test', spy1 );
  42. emitter.on( 'test', spy2 );
  43. emitter.fire( 'test', 1, 'b', true );
  44. sinon.assert.calledWithExactly( spy1, sinon.match.instanceOf( EventInfo ), 1, 'b', true );
  45. sinon.assert.calledWithExactly( spy2, sinon.match.instanceOf( EventInfo ), 1, 'b', true );
  46. } );
  47. it( 'should fire the right event', () => {
  48. const spy1 = sinon.spy();
  49. const spy2 = sinon.spy();
  50. emitter.on( '1', spy1 );
  51. emitter.on( '2', spy2 );
  52. emitter.fire( '2' );
  53. sinon.assert.notCalled( spy1 );
  54. sinon.assert.called( spy2 );
  55. } );
  56. it( 'should execute callbacks many times', () => {
  57. const spy = sinon.spy();
  58. emitter.on( 'test', spy );
  59. emitter.fire( 'test' );
  60. emitter.fire( 'test' );
  61. emitter.fire( 'test' );
  62. sinon.assert.calledThrice( spy );
  63. } );
  64. it( 'should do nothing for a non listened event', () => {
  65. emitter.fire( 'test' );
  66. } );
  67. it( 'should accept the same callback many times', () => {
  68. const spy = sinon.spy();
  69. emitter.on( 'test', spy );
  70. emitter.on( 'test', spy );
  71. emitter.on( 'test', spy );
  72. emitter.fire( 'test' );
  73. sinon.assert.calledThrice( spy );
  74. } );
  75. it( 'should not fire callbacks for an event that were added while firing that event', () => {
  76. const spy = sinon.spy();
  77. emitter.on( 'test', () => {
  78. emitter.on( 'test', spy );
  79. } );
  80. emitter.fire( 'test' );
  81. sinon.assert.notCalled( spy );
  82. } );
  83. it( 'should correctly fire callbacks for namespaced events', () => {
  84. const spyFoo = sinon.spy();
  85. const spyBar = sinon.spy();
  86. const spyAbc = sinon.spy();
  87. const spyFoo2 = sinon.spy();
  88. // Mess up with callbacks order to check whether they are called in adding order.
  89. emitter.on( 'foo', spyFoo );
  90. emitter.on( 'foo:bar:abc', spyAbc );
  91. emitter.on( 'foo:bar', spyBar );
  92. // This tests whether generic callbacks are also added to specific callbacks lists.
  93. emitter.on( 'foo', spyFoo2 );
  94. // All four callbacks should be fired.
  95. emitter.fire( 'foo:bar:abc' );
  96. sinon.assert.callOrder( spyFoo, spyAbc, spyBar, spyFoo2 );
  97. sinon.assert.calledOnce( spyFoo );
  98. sinon.assert.calledOnce( spyAbc );
  99. sinon.assert.calledOnce( spyBar );
  100. sinon.assert.calledOnce( spyFoo2 );
  101. // Only callbacks for foo and foo:bar event should be called.
  102. emitter.fire( 'foo:bar' );
  103. sinon.assert.calledOnce( spyAbc );
  104. sinon.assert.calledTwice( spyFoo );
  105. sinon.assert.calledTwice( spyBar );
  106. sinon.assert.calledTwice( spyFoo2 );
  107. // Only callback for foo should be called as foo:abc has not been registered.
  108. // Still, foo is a valid, existing namespace.
  109. emitter.fire( 'foo:abc' );
  110. sinon.assert.calledOnce( spyAbc );
  111. sinon.assert.calledTwice( spyBar );
  112. sinon.assert.calledThrice( spyFoo );
  113. sinon.assert.calledThrice( spyFoo2 );
  114. } );
  115. describe( 'return value', () => {
  116. it( 'is undefined by default', () => {
  117. expect( emitter.fire( 'foo' ) ).to.be.undefined;
  118. } );
  119. it( 'is undefined if none of the listeners modified EventInfo#return', () => {
  120. emitter.on( 'foo', () => {} );
  121. expect( emitter.fire( 'foo' ) ).to.be.undefined;
  122. } );
  123. it( 'equals EventInfo#return\'s value', () => {
  124. emitter.on( 'foo', evt => {
  125. evt.return = 1;
  126. } );
  127. expect( emitter.fire( 'foo' ) ).to.equal( 1 );
  128. } );
  129. it( 'equals EventInfo#return\'s value even if the event was stopped', () => {
  130. emitter.on( 'foo', evt => {
  131. evt.return = 1;
  132. } );
  133. emitter.on( 'foo', evt => {
  134. evt.stop();
  135. } );
  136. expect( emitter.fire( 'foo' ) ).to.equal( 1 );
  137. } );
  138. it( 'equals EventInfo#return\'s value when it was set in a namespaced event', () => {
  139. emitter.on( 'foo', evt => {
  140. evt.return = 1;
  141. } );
  142. expect( emitter.fire( 'foo:bar' ) ).to.equal( 1 );
  143. } );
  144. // Rationale – delegation keeps the listeners of the two objects separate.
  145. // E.g. the emitterB's listeners will always be executed before emitterA's ones.
  146. // Hence, values should not be shared either.
  147. it( 'is not affected by listeners executed on emitter to which the event was delegated', () => {
  148. const emitterA = getEmitterInstance();
  149. const emitterB = getEmitterInstance();
  150. emitterB.delegate( 'foo' ).to( emitterA );
  151. emitterA.on( 'foo', evt => {
  152. evt.return = 1;
  153. } );
  154. expect( emitterB.fire( 'foo' ) ).to.be.undefined;
  155. } );
  156. it( 'equals the value set by the last callback', () => {
  157. emitter.on( 'foo', evt => {
  158. evt.return = 1;
  159. } );
  160. emitter.on( 'foo', evt => {
  161. evt.return = 2;
  162. }, { priority: 'high' } );
  163. expect( emitter.fire( 'foo' ) ).to.equal( 1 );
  164. } );
  165. } );
  166. } );
  167. describe( 'on', () => {
  168. it( 'should stop()', () => {
  169. const spy1 = sinon.spy();
  170. const spy2 = sinon.spy();
  171. const spy3 = sinon.spy( event => {
  172. event.stop();
  173. } );
  174. emitter.on( 'test', spy1 );
  175. emitter.on( 'test', spy2 );
  176. emitter.on( 'test', spy3 );
  177. emitter.on( 'test', sinon.stub().throws() );
  178. emitter.on( 'test', sinon.stub().throws() );
  179. emitter.fire( 'test' );
  180. sinon.assert.called( spy1 );
  181. sinon.assert.called( spy2 );
  182. sinon.assert.called( spy3 );
  183. } );
  184. it( 'should take a callback off()', () => {
  185. const spy1 = sinon.spy();
  186. const spy2 = sinon.spy( event => {
  187. event.off();
  188. } );
  189. const spy3 = sinon.spy();
  190. emitter.on( 'test', spy1 );
  191. emitter.on( 'test', spy2 );
  192. emitter.on( 'test', spy3 );
  193. emitter.fire( 'test' );
  194. emitter.fire( 'test' );
  195. sinon.assert.calledTwice( spy1 );
  196. sinon.assert.calledOnce( spy2 );
  197. sinon.assert.calledTwice( spy3 );
  198. } );
  199. it( 'should take the callback off() even after stop()', () => {
  200. const spy1 = sinon.spy( event => {
  201. event.stop();
  202. event.off();
  203. } );
  204. const spy2 = sinon.spy();
  205. emitter.on( 'test', spy1 );
  206. emitter.on( 'test', spy2 );
  207. emitter.fire( 'test' );
  208. emitter.fire( 'test' );
  209. sinon.assert.calledOnce( spy1 );
  210. sinon.assert.calledOnce( spy2 );
  211. } );
  212. } );
  213. describe( 'once', () => {
  214. it( 'should be called just once for general event', () => {
  215. const spy1 = sinon.spy();
  216. const spy2 = sinon.spy();
  217. const spy3 = sinon.spy();
  218. emitter.on( 'test', spy1 );
  219. emitter.once( 'test', spy2 );
  220. emitter.on( 'test', spy3 );
  221. emitter.fire( 'test' );
  222. emitter.fire( 'test' );
  223. sinon.assert.calledTwice( spy1 );
  224. sinon.assert.calledOnce( spy2 );
  225. sinon.assert.calledTwice( spy3 );
  226. } );
  227. it( 'should be called just once for namespaced event', () => {
  228. const spy1 = sinon.spy();
  229. const spy2 = sinon.spy();
  230. const spy3 = sinon.spy();
  231. emitter.on( 'foo:bar', spy1 );
  232. emitter.once( 'foo:bar', spy2 );
  233. emitter.on( 'foo:bar', spy3 );
  234. emitter.fire( 'foo:bar' );
  235. emitter.fire( 'foo:bar' );
  236. sinon.assert.calledTwice( spy1 );
  237. sinon.assert.calledOnce( spy2 );
  238. sinon.assert.calledTwice( spy3 );
  239. } );
  240. it( 'should have proper arguments', () => {
  241. const spy = sinon.spy();
  242. emitter.once( 'test', spy );
  243. emitter.fire( 'test', 1, 2, 3 );
  244. sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), 1, 2, 3 );
  245. } );
  246. it( 'should be removed also when fired through namespaced event', () => {
  247. const spy = sinon.spy();
  248. emitter.once( 'foo', spy );
  249. emitter.fire( 'foo:bar' );
  250. emitter.fire( 'foo' );
  251. sinon.assert.calledOnce( spy );
  252. } );
  253. it( 'should be called only once and have infinite loop protection', () => {
  254. const spy = sinon.spy();
  255. emitter.once( 'foo', () => {
  256. spy();
  257. emitter.fire( 'foo' );
  258. } );
  259. emitter.fire( 'foo' );
  260. sinon.assert.calledOnce( spy );
  261. } );
  262. } );
  263. describe( 'off', () => {
  264. it( 'should get callbacks off()', () => {
  265. const spy1 = sinon.spy();
  266. const spy2 = sinon.spy();
  267. const spy3 = sinon.spy();
  268. emitter.on( 'test', spy1 );
  269. emitter.on( 'test', spy2 );
  270. emitter.on( 'test', spy3 );
  271. emitter.fire( 'test' );
  272. emitter.off( 'test', spy2 );
  273. emitter.fire( 'test' );
  274. emitter.fire( 'test' );
  275. sinon.assert.calledThrice( spy1 );
  276. sinon.assert.calledOnce( spy2 );
  277. sinon.assert.calledThrice( spy3 );
  278. } );
  279. it( 'should remove all callbacks for event', () => {
  280. const spy1 = sinon.spy();
  281. const spy2 = sinon.spy();
  282. emitter.on( 'test', spy1 );
  283. emitter.on( 'test', spy2 );
  284. emitter.fire( 'test' );
  285. emitter.off( 'test' );
  286. emitter.fire( 'test' );
  287. emitter.fire( 'test' );
  288. sinon.assert.calledOnce( spy1 );
  289. sinon.assert.calledOnce( spy2 );
  290. } );
  291. it( 'should not fail with unknown events', () => {
  292. emitter.off( 'foo', () => {} );
  293. emitter.off( 'foo:bar', () => {} );
  294. emitter.off( 'foo' );
  295. emitter.off( 'foo:bar' );
  296. } );
  297. it( 'should remove all entries for the same callback', () => {
  298. const spy1 = sinon.spy().named( 1 );
  299. const spy2 = sinon.spy().named( 2 );
  300. emitter.on( 'test', spy1 );
  301. emitter.on( 'test', spy2 );
  302. emitter.on( 'test', spy1 );
  303. emitter.on( 'test', spy2 );
  304. emitter.fire( 'test' );
  305. emitter.off( 'test', spy1 );
  306. emitter.fire( 'test' );
  307. sinon.assert.callCount( spy1, 2 );
  308. sinon.assert.callCount( spy2, 4 );
  309. } );
  310. it( 'should not remove all namespaced entries when removing namespace inner group', () => {
  311. const spy1 = sinon.spy().named( 'foo' );
  312. const spy2 = sinon.spy().named( 'foo:bar' );
  313. const spy3 = sinon.spy().named( 'foo:bar:baz' );
  314. const spy4 = sinon.spy().named( 'foo:bar:baz:abc' );
  315. emitter.on( 'foo', spy1 );
  316. emitter.on( 'foo:bar', spy2 );
  317. emitter.on( 'foo:bar:baz', spy3 );
  318. emitter.on( 'foo:bar:baz:abc', spy4 );
  319. emitter.fire( 'foo:bar:baz:abc' );
  320. sinon.assert.calledOnce( spy1 );
  321. sinon.assert.calledOnce( spy2 );
  322. sinon.assert.calledOnce( spy3 );
  323. sinon.assert.calledOnce( spy4 );
  324. emitter.off( 'foo:bar' );
  325. emitter.fire( 'foo:bar:baz:abc' );
  326. sinon.assert.calledTwice( spy1 );
  327. sinon.assert.calledOnce( spy2 );
  328. sinon.assert.calledTwice( spy3 );
  329. sinon.assert.calledTwice( spy4 );
  330. } );
  331. it( 'should properly remove callbacks for namespaced events', () => {
  332. const spyFoo = sinon.spy();
  333. const spyAbc = sinon.spy();
  334. const spyBar = sinon.spy();
  335. const spyFoo2 = sinon.spy();
  336. emitter.on( 'foo', spyFoo );
  337. emitter.on( 'foo:bar:abc', spyAbc );
  338. emitter.on( 'foo:bar', spyBar );
  339. emitter.on( 'foo', spyFoo2 );
  340. emitter.off( 'foo', spyFoo );
  341. emitter.fire( 'foo:bar:abc' );
  342. sinon.assert.calledOnce( spyAbc );
  343. sinon.assert.calledOnce( spyBar );
  344. sinon.assert.calledOnce( spyFoo2 );
  345. sinon.assert.notCalled( spyFoo );
  346. emitter.fire( 'foo:bar' );
  347. sinon.assert.notCalled( spyFoo );
  348. sinon.assert.calledOnce( spyAbc );
  349. sinon.assert.calledTwice( spyBar );
  350. sinon.assert.calledTwice( spyFoo2 );
  351. emitter.fire( 'foo' );
  352. sinon.assert.notCalled( spyFoo );
  353. sinon.assert.calledOnce( spyAbc );
  354. sinon.assert.calledTwice( spyBar );
  355. sinon.assert.calledThrice( spyFoo2 );
  356. } );
  357. } );
  358. describe( 'listenTo', () => {
  359. it( 'should properly register callbacks', () => {
  360. const spy = sinon.spy();
  361. listener.listenTo( emitter, 'test', spy );
  362. emitter.fire( 'test' );
  363. sinon.assert.called( spy );
  364. } );
  365. it( 'should correctly listen to namespaced events', () => {
  366. const spyFoo = sinon.spy();
  367. const spyBar = sinon.spy();
  368. const spyBaz = sinon.spy();
  369. listener.listenTo( emitter, 'foo', spyFoo );
  370. listener.listenTo( emitter, 'foo:bar', spyBar );
  371. listener.listenTo( emitter, 'foo:bar:baz', spyBaz );
  372. emitter.fire( 'foo:bar:baz' );
  373. sinon.assert.calledOnce( spyFoo );
  374. sinon.assert.calledOnce( spyBar );
  375. sinon.assert.calledOnce( spyBaz );
  376. emitter.fire( 'foo:bar' );
  377. sinon.assert.calledTwice( spyFoo );
  378. sinon.assert.calledTwice( spyBar );
  379. sinon.assert.calledOnce( spyBaz );
  380. emitter.fire( 'foo' );
  381. sinon.assert.calledThrice( spyFoo );
  382. sinon.assert.calledTwice( spyBar );
  383. sinon.assert.calledOnce( spyBaz );
  384. } );
  385. } );
  386. describe( 'stopListening', () => {
  387. it( 'should stop listening to given event callback', () => {
  388. const spy1 = sinon.spy();
  389. const spy2 = sinon.spy();
  390. listener.listenTo( emitter, 'event1', spy1 );
  391. listener.listenTo( emitter, 'event2', spy2 );
  392. emitter.fire( 'event1' );
  393. emitter.fire( 'event2' );
  394. listener.stopListening( emitter, 'event1', spy1 );
  395. emitter.fire( 'event1' );
  396. emitter.fire( 'event2' );
  397. sinon.assert.calledOnce( spy1 );
  398. sinon.assert.calledTwice( spy2 );
  399. } );
  400. it( 'should stop listening to given event', () => {
  401. const spy1a = sinon.spy();
  402. const spy1b = sinon.spy();
  403. const spy2 = sinon.spy();
  404. listener.listenTo( emitter, 'event1', spy1a );
  405. listener.listenTo( emitter, 'event1', spy1b );
  406. listener.listenTo( emitter, 'event2', spy2 );
  407. emitter.fire( 'event1' );
  408. emitter.fire( 'event2' );
  409. listener.stopListening( emitter, 'event1' );
  410. emitter.fire( 'event1' );
  411. emitter.fire( 'event2' );
  412. sinon.assert.calledOnce( spy1a );
  413. sinon.assert.calledOnce( spy1b );
  414. sinon.assert.calledTwice( spy2 );
  415. } );
  416. it( 'should stop listening to all events from given emitter', () => {
  417. const spy1 = sinon.spy();
  418. const spy2 = sinon.spy();
  419. const spy3 = sinon.spy();
  420. const spy4 = sinon.spy();
  421. listener.listenTo( emitter, 'event1', spy1 );
  422. listener.listenTo( emitter, 'event2', spy2 );
  423. listener.listenTo( emitter, 'foo', spy3 );
  424. listener.listenTo( emitter, 'foo:bar:baz', spy4 );
  425. emitter.fire( 'event1' );
  426. emitter.fire( 'event2' );
  427. emitter.fire( 'foo:bar:baz' );
  428. listener.stopListening( emitter );
  429. emitter.fire( 'event1' );
  430. emitter.fire( 'event2' );
  431. emitter.fire( 'foo:bar:baz' );
  432. sinon.assert.calledOnce( spy1 );
  433. sinon.assert.calledOnce( spy2 );
  434. sinon.assert.calledOnce( spy3 );
  435. sinon.assert.calledOnce( spy4 );
  436. } );
  437. it( 'should stop listening to everything', () => {
  438. const spy1 = sinon.spy();
  439. const spy2 = sinon.spy();
  440. const spy3 = sinon.spy();
  441. const spy4 = sinon.spy();
  442. const emitter1 = getEmitterInstance();
  443. const emitter2 = getEmitterInstance();
  444. listener.listenTo( emitter1, 'event1', spy1 );
  445. listener.listenTo( emitter2, 'event2', spy2 );
  446. listener.listenTo( emitter1, 'foo', spy3 );
  447. listener.listenTo( emitter1, 'foo:bar:baz', spy4 );
  448. emitter1.fire( 'event1' );
  449. emitter2.fire( 'event2' );
  450. emitter1.fire( 'foo' );
  451. emitter1.fire( 'foo:bar' );
  452. emitter1.fire( 'foo:bar:baz' );
  453. listener.stopListening();
  454. emitter1.fire( 'event1' );
  455. emitter2.fire( 'event2' );
  456. emitter1.fire( 'foo' );
  457. emitter1.fire( 'foo:bar' );
  458. emitter1.fire( 'foo:bar:baz' );
  459. sinon.assert.calledOnce( spy1 );
  460. sinon.assert.calledOnce( spy2 );
  461. sinon.assert.calledThrice( spy3 );
  462. sinon.assert.calledOnce( spy4 );
  463. } );
  464. it( 'should not stop other emitters when a non-listened emitter is provided', () => {
  465. const spy = sinon.spy();
  466. const emitter1 = getEmitterInstance();
  467. const emitter2 = getEmitterInstance();
  468. listener.listenTo( emitter1, 'test', spy );
  469. listener.stopListening( emitter2 );
  470. emitter1.fire( 'test' );
  471. sinon.assert.called( spy );
  472. } );
  473. it( 'should correctly stop listening to namespaced events', () => {
  474. const spyFoo = sinon.spy();
  475. const spyBar = sinon.spy();
  476. const spyBaz = sinon.spy();
  477. listener.listenTo( emitter, 'foo', spyFoo );
  478. listener.listenTo( emitter, 'foo:bar', spyBar );
  479. listener.listenTo( emitter, 'foo:bar:baz', spyBaz );
  480. listener.stopListening( emitter, 'foo' );
  481. emitter.fire( 'foo:bar:baz' );
  482. sinon.assert.notCalled( spyFoo );
  483. sinon.assert.calledOnce( spyBar );
  484. sinon.assert.calledOnce( spyBaz );
  485. } );
  486. it( 'should correctly stop listening to namespaced events when removing specialised event', () => {
  487. const spyFoo = sinon.spy();
  488. const spyBar = sinon.spy();
  489. const spyBaz = sinon.spy();
  490. listener.listenTo( emitter, 'foo', spyFoo );
  491. listener.listenTo( emitter, 'foo:bar', spyBar );
  492. listener.listenTo( emitter, 'foo:bar:baz', spyBaz );
  493. listener.stopListening( emitter, 'foo:bar' );
  494. emitter.fire( 'foo:bar:baz' );
  495. sinon.assert.calledOnce( spyFoo );
  496. sinon.assert.notCalled( spyBar );
  497. sinon.assert.calledOnce( spyBaz );
  498. } );
  499. it( 'should not fail with unknown events', () => {
  500. listener.stopListening( emitter, 'foo', () => {} );
  501. listener.stopListening( emitter, 'foo:bar', () => {} );
  502. listener.stopListening( emitter, 'foo' );
  503. listener.stopListening( emitter, 'foo:bar' );
  504. } );
  505. it( 'should not fail with unknown emitter', () => {
  506. listener.listenTo( emitter, 'foo:bar', () => {} );
  507. listener.stopListening( {}, 'foo', () => {} );
  508. listener.stopListening( {}, 'foo:bar', () => {} );
  509. listener.stopListening( {}, 'foo' );
  510. listener.stopListening( {}, 'foo:bar' );
  511. listener.stopListening( {} );
  512. } );
  513. it( 'should not fail with unknown callbacks', () => {
  514. const spy = sinon.spy();
  515. listener.listenTo( emitter, 'foo', spy );
  516. listener.stopListening( emitter, 'foo', () => {} );
  517. emitter.fire( 'foo' );
  518. sinon.assert.calledOnce( spy );
  519. } );
  520. } );
  521. describe( 'delegate', () => {
  522. it( 'should chain for a single event', () => {
  523. const emitter = getEmitterInstance();
  524. expect( emitter.delegate( 'foo' ) ).to.contain.keys( 'to' );
  525. } );
  526. it( 'should chain for multiple events', () => {
  527. const emitter = getEmitterInstance();
  528. expect( emitter.delegate( 'foo', 'bar' ) ).to.contain.keys( 'to' );
  529. } );
  530. describe( 'to', () => {
  531. it( 'forwards an event to another emitter', done => {
  532. const emitterA = getEmitterInstance();
  533. const emitterB = getEmitterInstance();
  534. const dataA = {};
  535. const dataB = {};
  536. emitterB.delegate( 'foo' ).to( emitterA );
  537. emitterA.on( 'foo', ( ...args ) => {
  538. assertDelegated( args, {
  539. expectedSource: emitterB,
  540. expectedName: 'foo',
  541. expectedPath: [ emitterB, emitterA ],
  542. expectedData: [ dataA, dataB ]
  543. } );
  544. done();
  545. } );
  546. emitterB.fire( 'foo', dataA, dataB );
  547. } );
  548. it( 'forwards multiple events to another emitter', () => {
  549. const emitterA = getEmitterInstance();
  550. const emitterB = getEmitterInstance();
  551. const spyFoo = sinon.spy();
  552. const spyBar = sinon.spy();
  553. const spyBaz = sinon.spy();
  554. const dataA = {};
  555. const dataB = {};
  556. emitterB.delegate( 'foo', 'bar', 'baz' ).to( emitterA );
  557. emitterA.on( 'foo', spyFoo );
  558. emitterA.on( 'bar', spyBar );
  559. emitterA.on( 'baz', spyBaz );
  560. emitterB.fire( 'foo', dataA, dataB );
  561. sinon.assert.calledOnce( spyFoo );
  562. sinon.assert.notCalled( spyBar );
  563. sinon.assert.notCalled( spyBaz );
  564. assertDelegated( spyFoo.args[ 0 ], {
  565. expectedSource: emitterB,
  566. expectedName: 'foo',
  567. expectedPath: [ emitterB, emitterA ],
  568. expectedData: [ dataA, dataB ]
  569. } );
  570. emitterB.fire( 'bar' );
  571. sinon.assert.calledOnce( spyFoo );
  572. sinon.assert.calledOnce( spyBar );
  573. sinon.assert.notCalled( spyBaz );
  574. assertDelegated( spyBar.args[ 0 ], {
  575. expectedSource: emitterB,
  576. expectedName: 'bar',
  577. expectedPath: [ emitterB, emitterA ],
  578. expectedData: []
  579. } );
  580. emitterB.fire( 'baz' );
  581. sinon.assert.calledOnce( spyFoo );
  582. sinon.assert.calledOnce( spyBar );
  583. sinon.assert.calledOnce( spyBaz );
  584. assertDelegated( spyBaz.args[ 0 ], {
  585. expectedSource: emitterB,
  586. expectedName: 'baz',
  587. expectedPath: [ emitterB, emitterA ],
  588. expectedData: []
  589. } );
  590. emitterB.fire( 'not-delegated' );
  591. sinon.assert.calledOnce( spyFoo );
  592. sinon.assert.calledOnce( spyBar );
  593. sinon.assert.calledOnce( spyBaz );
  594. } );
  595. it( 'does not forward events which are not supposed to be delegated', () => {
  596. const emitterA = getEmitterInstance();
  597. const emitterB = getEmitterInstance();
  598. const spyFoo = sinon.spy();
  599. const spyBar = sinon.spy();
  600. const spyBaz = sinon.spy();
  601. emitterB.delegate( 'foo', 'bar', 'baz' ).to( emitterA );
  602. emitterA.on( 'foo', spyFoo );
  603. emitterA.on( 'bar', spyBar );
  604. emitterA.on( 'baz', spyBaz );
  605. emitterB.fire( 'foo' );
  606. emitterB.fire( 'bar' );
  607. emitterB.fire( 'baz' );
  608. emitterB.fire( 'not-delegated' );
  609. sinon.assert.callOrder( spyFoo, spyBar, spyBaz );
  610. sinon.assert.callCount( spyFoo, 1 );
  611. sinon.assert.callCount( spyBar, 1 );
  612. sinon.assert.callCount( spyBaz, 1 );
  613. } );
  614. it( 'supports deep chain event delegation', done => {
  615. const emitterA = getEmitterInstance();
  616. const emitterB = getEmitterInstance();
  617. const emitterC = getEmitterInstance();
  618. const data = {};
  619. emitterC.delegate( 'foo' ).to( emitterB );
  620. emitterB.delegate( 'foo' ).to( emitterA );
  621. emitterA.on( 'foo', ( ...args ) => {
  622. assertDelegated( args, {
  623. expectedSource: emitterC,
  624. expectedName: 'foo',
  625. expectedPath: [ emitterC, emitterB, emitterA ],
  626. expectedData: [ data ]
  627. } );
  628. done();
  629. } );
  630. emitterC.fire( 'foo', data );
  631. } );
  632. it( 'preserves path in event delegation', done => {
  633. const data = {};
  634. const emitterA = getEmitterInstance();
  635. const emitterB = getEmitterInstance();
  636. const emitterC = getEmitterInstance();
  637. const emitterD = getEmitterInstance();
  638. emitterB.delegate( 'foo' ).to( emitterA );
  639. emitterB.delegate( 'foo' ).to( emitterC );
  640. emitterB.delegate( 'foo' ).to( emitterD );
  641. emitterD.on( 'foo', ( ...args ) => {
  642. assertDelegated( args, {
  643. expectedSource: emitterB,
  644. expectedName: 'foo',
  645. expectedPath: [ emitterB, emitterD ],
  646. expectedData: [ data ]
  647. } );
  648. done();
  649. } );
  650. emitterB.fire( 'foo', data );
  651. emitterC.fire( 'foo', data );
  652. } );
  653. it( 'executes callbacks first, then delegates further', () => {
  654. const emitterA = getEmitterInstance();
  655. const emitterB = getEmitterInstance();
  656. const spyA = sinon.spy();
  657. const spyB = sinon.spy();
  658. emitterB.delegate( 'foo' ).to( emitterA );
  659. emitterA.on( 'foo', spyA );
  660. emitterB.on( 'foo', spyB );
  661. emitterB.fire( 'foo' );
  662. sinon.assert.callOrder( spyB, spyA );
  663. } );
  664. it( 'supports delegation under a different name', () => {
  665. const emitterA = getEmitterInstance();
  666. const emitterB = getEmitterInstance();
  667. const emitterC = getEmitterInstance();
  668. const emitterD = getEmitterInstance();
  669. const spyAFoo = sinon.spy();
  670. const spyABar = sinon.spy();
  671. const spyCBaz = sinon.spy();
  672. const spyDFoo = sinon.spy();
  673. emitterB.delegate( 'foo' ).to( emitterA, 'bar' );
  674. emitterB.delegate( 'foo' ).to( emitterC, name => name + '-baz' );
  675. emitterB.delegate( 'foo' ).to( emitterD );
  676. emitterA.on( 'foo', spyAFoo );
  677. emitterA.on( 'bar', spyABar );
  678. emitterC.on( 'foo-baz', spyCBaz );
  679. emitterD.on( 'foo', spyDFoo );
  680. emitterB.fire( 'foo' );
  681. sinon.assert.calledOnce( spyABar );
  682. sinon.assert.calledOnce( spyCBaz );
  683. sinon.assert.calledOnce( spyDFoo );
  684. sinon.assert.notCalled( spyAFoo );
  685. } );
  686. it( 'supports delegation under a different name with multiple events', () => {
  687. const emitterA = getEmitterInstance();
  688. const emitterB = getEmitterInstance();
  689. const spyAFoo = sinon.spy();
  690. const spyABar = sinon.spy();
  691. const spyABaz = sinon.spy();
  692. const spyAQux = sinon.spy();
  693. emitterB.delegate( 'foo', 'bar', 'baz' ).to( emitterA, 'qux' );
  694. emitterA.on( 'foo', spyAFoo );
  695. emitterA.on( 'bar', spyABar );
  696. emitterA.on( 'baz', spyABaz );
  697. emitterA.on( 'qux', spyAQux );
  698. emitterB.fire( 'foo' );
  699. emitterB.fire( 'baz' );
  700. emitterB.fire( 'bar' );
  701. sinon.assert.notCalled( spyAFoo );
  702. sinon.assert.notCalled( spyABar );
  703. sinon.assert.notCalled( spyABaz );
  704. sinon.assert.calledThrice( spyAQux );
  705. } );
  706. it( 'supports delegation with multiple events, each under a different name', () => {
  707. const emitterA = getEmitterInstance();
  708. const emitterB = getEmitterInstance();
  709. const spyAFoo = sinon.spy();
  710. const spyABar = sinon.spy();
  711. const spyABaz = sinon.spy();
  712. const spyAFooQux = sinon.spy();
  713. const spyABarQux = sinon.spy();
  714. const spyABazQux = sinon.spy();
  715. emitterB.delegate( 'foo', 'bar', 'baz' ).to( emitterA, name => name + '-qux' );
  716. emitterA.on( 'foo', spyAFoo );
  717. emitterA.on( 'bar', spyABar );
  718. emitterA.on( 'baz', spyABaz );
  719. emitterA.on( 'foo-qux', spyAFooQux );
  720. emitterA.on( 'bar-qux', spyABarQux );
  721. emitterA.on( 'baz-qux', spyABazQux );
  722. emitterB.fire( 'foo' );
  723. emitterB.fire( 'baz' );
  724. emitterB.fire( 'bar' );
  725. sinon.assert.notCalled( spyAFoo );
  726. sinon.assert.notCalled( spyABar );
  727. sinon.assert.notCalled( spyABaz );
  728. sinon.assert.calledOnce( spyAFooQux );
  729. sinon.assert.calledOnce( spyABarQux );
  730. sinon.assert.calledOnce( spyABazQux );
  731. sinon.assert.callOrder( spyAFooQux, spyABazQux, spyABarQux );
  732. } );
  733. it( 'preserves path in delegation under a different name', done => {
  734. const data = {};
  735. const emitterA = getEmitterInstance();
  736. const emitterB = getEmitterInstance();
  737. const emitterC = getEmitterInstance();
  738. const emitterD = getEmitterInstance();
  739. emitterB.delegate( 'foo' ).to( emitterA, 'bar' );
  740. emitterB.delegate( 'foo' ).to( emitterC, 'baz' );
  741. emitterB.delegate( 'foo' ).to( emitterD );
  742. emitterD.on( 'foo', ( ...args ) => {
  743. assertDelegated( args, {
  744. expectedSource: emitterB,
  745. expectedName: 'foo',
  746. expectedPath: [ emitterB, emitterD ],
  747. expectedData: [ data ]
  748. } );
  749. done();
  750. } );
  751. emitterB.fire( 'foo', data );
  752. } );
  753. it( 'supports delegation of all events', () => {
  754. const emitterA = getEmitterInstance();
  755. const emitterB = getEmitterInstance();
  756. const spyAFoo = sinon.spy();
  757. const spyABar = sinon.spy();
  758. const spyABaz = sinon.spy();
  759. emitterB.delegate( '*' ).to( emitterA );
  760. emitterA.on( 'foo', spyAFoo );
  761. emitterA.on( 'bar', spyABar );
  762. emitterA.on( 'baz', spyABaz );
  763. emitterB.fire( 'foo' );
  764. emitterB.fire( 'baz' );
  765. emitterB.fire( 'bar' );
  766. sinon.assert.callOrder( spyAFoo, spyABaz, spyABar );
  767. } );
  768. it( 'supports delegation of all events under different names', () => {
  769. const emitterA = getEmitterInstance();
  770. const emitterB = getEmitterInstance();
  771. const spyAFoo = sinon.spy();
  772. const spyABar = sinon.spy();
  773. const spyABaz = sinon.spy();
  774. const spyAFooDel = sinon.spy();
  775. const spyABarDel = sinon.spy();
  776. const spyABazDel = sinon.spy();
  777. emitterB.delegate( '*' ).to( emitterA, name => name + '-delegated' );
  778. emitterA.on( 'foo', spyAFoo );
  779. emitterA.on( 'bar', spyABar );
  780. emitterA.on( 'baz', spyABaz );
  781. emitterA.on( 'foo-delegated', spyAFooDel );
  782. emitterA.on( 'bar-delegated', spyABarDel );
  783. emitterA.on( 'baz-delegated', spyABazDel );
  784. emitterB.fire( 'foo' );
  785. emitterB.fire( 'baz' );
  786. emitterB.fire( 'bar' );
  787. sinon.assert.notCalled( spyAFoo );
  788. sinon.assert.notCalled( spyABar );
  789. sinon.assert.notCalled( spyABaz );
  790. sinon.assert.callOrder( spyAFooDel, spyABazDel, spyABarDel );
  791. } );
  792. } );
  793. } );
  794. describe( 'stopDelegating', () => {
  795. it( 'passes if no delegation was set', () => {
  796. expect( () => {
  797. getEmitterInstance().stopDelegating();
  798. } ).to.not.throw();
  799. } );
  800. it( 'stops delegating all events to all emitters', () => {
  801. const emitterA = getEmitterInstance();
  802. const emitterB = getEmitterInstance();
  803. const emitterC = getEmitterInstance();
  804. const spyFoo = sinon.spy();
  805. const spyBar = sinon.spy();
  806. emitterA.delegate( 'foo' ).to( emitterB );
  807. emitterA.delegate( 'bar' ).to( emitterC );
  808. emitterB.on( 'foo', spyFoo );
  809. emitterC.on( 'bar', spyBar );
  810. emitterA.fire( 'foo' );
  811. emitterA.fire( 'bar' );
  812. sinon.assert.callOrder( spyFoo, spyBar );
  813. emitterA.stopDelegating();
  814. emitterA.fire( 'foo' );
  815. emitterA.fire( 'bar' );
  816. sinon.assert.callOrder( spyFoo, spyBar );
  817. } );
  818. it( 'stops delegating a specific event to all emitters', () => {
  819. const emitterA = getEmitterInstance();
  820. const emitterB = getEmitterInstance();
  821. const emitterC = getEmitterInstance();
  822. const spyFooB = sinon.spy();
  823. const spyFooC = sinon.spy();
  824. const spyBarC = sinon.spy();
  825. emitterA.delegate( 'foo' ).to( emitterB );
  826. emitterA.delegate( 'foo' ).to( emitterC );
  827. emitterA.delegate( 'bar' ).to( emitterC );
  828. emitterB.on( 'foo', spyFooB );
  829. emitterC.on( 'foo', spyFooC );
  830. emitterC.on( 'bar', spyBarC );
  831. emitterA.fire( 'foo' );
  832. emitterA.fire( 'bar' );
  833. sinon.assert.callOrder( spyFooB, spyFooC, spyBarC );
  834. emitterA.stopDelegating( 'foo' );
  835. emitterA.fire( 'foo' );
  836. emitterA.fire( 'bar' );
  837. sinon.assert.callOrder( spyFooB, spyFooC, spyBarC, spyBarC );
  838. } );
  839. it( 'stops delegating a specific event to a specific emitter', () => {
  840. const emitterA = getEmitterInstance();
  841. const emitterB = getEmitterInstance();
  842. const emitterC = getEmitterInstance();
  843. const spyFooB = sinon.spy();
  844. const spyFooC = sinon.spy();
  845. emitterA.delegate( 'foo' ).to( emitterB );
  846. emitterA.delegate( 'foo' ).to( emitterC );
  847. emitterB.on( 'foo', spyFooB );
  848. emitterC.on( 'foo', spyFooC );
  849. emitterA.fire( 'foo' );
  850. sinon.assert.callOrder( spyFooB, spyFooC );
  851. emitterA.stopDelegating( 'foo', emitterC );
  852. emitterA.fire( 'foo' );
  853. sinon.assert.callOrder( spyFooB, spyFooC, spyFooB );
  854. } );
  855. it( 'stops delegating a specific event under a different name to a specific emitter', () => {
  856. const emitterA = getEmitterInstance();
  857. const emitterB = getEmitterInstance();
  858. const emitterC = getEmitterInstance();
  859. const spyFooB = sinon.spy();
  860. const spyFooC = sinon.spy();
  861. emitterA.delegate( 'foo' ).to( emitterB );
  862. emitterA.delegate( 'foo' ).to( emitterC, 'bar' );
  863. emitterB.on( 'foo', spyFooB );
  864. emitterC.on( 'bar', spyFooC );
  865. emitterA.fire( 'foo' );
  866. sinon.assert.callOrder( spyFooB, spyFooC );
  867. emitterA.stopDelegating( 'foo', emitterC );
  868. emitterA.fire( 'foo' );
  869. sinon.assert.callOrder( spyFooB, spyFooC, spyFooB );
  870. } );
  871. it( 'stops delegating all ("*")', () => {
  872. const emitterA = getEmitterInstance();
  873. const emitterB = getEmitterInstance();
  874. const emitterC = getEmitterInstance();
  875. const spyAFoo = sinon.spy();
  876. const spyABar = sinon.spy();
  877. const spyCFoo = sinon.spy();
  878. const spyCBar = sinon.spy();
  879. emitterB.delegate( '*' ).to( emitterA );
  880. emitterB.delegate( '*' ).to( emitterC );
  881. emitterA.on( 'foo', spyAFoo );
  882. emitterA.on( 'bar', spyABar );
  883. emitterC.on( 'foo', spyCFoo );
  884. emitterC.on( 'bar', spyCBar );
  885. emitterB.fire( 'foo' );
  886. emitterB.fire( 'bar' );
  887. sinon.assert.calledOnce( spyAFoo );
  888. sinon.assert.calledOnce( spyABar );
  889. sinon.assert.calledOnce( spyCFoo );
  890. sinon.assert.calledOnce( spyCBar );
  891. emitterB.stopDelegating( '*' );
  892. emitterB.fire( 'foo' );
  893. emitterB.fire( 'bar' );
  894. sinon.assert.calledOnce( spyAFoo );
  895. sinon.assert.calledOnce( spyABar );
  896. sinon.assert.calledOnce( spyCFoo );
  897. sinon.assert.calledOnce( spyCBar );
  898. } );
  899. it( 'stops delegating all ("*") to a specific emitter', () => {
  900. const emitterA = getEmitterInstance();
  901. const emitterB = getEmitterInstance();
  902. const emitterC = getEmitterInstance();
  903. const spyAFoo = sinon.spy();
  904. const spyABar = sinon.spy();
  905. const spyCFoo = sinon.spy();
  906. const spyCBar = sinon.spy();
  907. emitterB.delegate( '*' ).to( emitterA );
  908. emitterB.delegate( 'foo' ).to( emitterC );
  909. emitterA.on( 'foo', spyAFoo );
  910. emitterA.on( 'bar', spyABar );
  911. emitterC.on( 'foo', spyCFoo );
  912. emitterC.on( 'bar', spyCBar );
  913. emitterB.fire( 'foo' );
  914. emitterB.fire( 'bar' );
  915. sinon.assert.calledOnce( spyAFoo );
  916. sinon.assert.calledOnce( spyABar );
  917. sinon.assert.calledOnce( spyCFoo );
  918. sinon.assert.notCalled( spyCBar );
  919. emitterB.stopDelegating( '*', emitterA );
  920. emitterB.fire( 'foo' );
  921. emitterB.fire( 'bar' );
  922. sinon.assert.calledOnce( spyAFoo );
  923. sinon.assert.calledOnce( spyABar );
  924. sinon.assert.calledTwice( spyCFoo );
  925. sinon.assert.notCalled( spyCBar );
  926. } );
  927. it( 'passes when stopping delegation of a specific event which has never been delegated', () => {
  928. const emitterA = getEmitterInstance();
  929. const emitterB = getEmitterInstance();
  930. expect( () => {
  931. emitterA.stopDelegating( 'bar' );
  932. emitterA.stopDelegating( 'bar', emitterB );
  933. } ).to.not.throw();
  934. } );
  935. it( 'passes when stopping delegation of a specific event to an emitter which wasn\'t a destination', () => {
  936. const emitterA = getEmitterInstance();
  937. const emitterB = getEmitterInstance();
  938. const emitterC = getEmitterInstance();
  939. emitterA.delegate( 'foo' ).to( emitterB );
  940. expect( () => {
  941. emitterA.stopDelegating( 'foo', emitterC );
  942. } ).to.not.throw();
  943. } );
  944. it( 'passes when stopping delegation of a specific event to a specific emitter which has never been delegated', () => {
  945. const emitterA = getEmitterInstance();
  946. const emitterB = getEmitterInstance();
  947. const emitterC = getEmitterInstance();
  948. emitterA.delegate( 'foo' ).to( emitterB );
  949. expect( () => {
  950. emitterA.stopDelegating( 'bar', emitterC );
  951. } ).to.not.throw();
  952. } );
  953. } );
  954. function assertDelegated( evtArgs, { expectedName, expectedSource, expectedPath, expectedData } ) {
  955. const evtInfo = evtArgs[ 0 ];
  956. expect( evtInfo.name ).to.equal( expectedName );
  957. expect( evtInfo.source ).to.equal( expectedSource );
  958. expect( evtInfo.path ).to.deep.equal( expectedPath );
  959. expect( evtArgs.slice( 1 ) ).to.deep.equal( expectedData );
  960. }
  961. } );
  962. describe( 'emitter id', () => {
  963. let emitter;
  964. beforeEach( () => {
  965. emitter = getEmitterInstance();
  966. } );
  967. it( 'should be undefined before it is set', () => {
  968. expect( _getEmitterId( emitter ) ).to.be.undefined;
  969. } );
  970. it( 'should be settable but only once', () => {
  971. _setEmitterId( emitter, 'abc' );
  972. expect( _getEmitterId( emitter ) ).to.equal( 'abc' );
  973. _setEmitterId( emitter, 'xyz' );
  974. expect( _getEmitterId( emitter ) ).to.equal( 'abc' );
  975. } );
  976. } );
  977. describe( '_getEmitterListenedTo', () => {
  978. let emitter, listener;
  979. beforeEach( () => {
  980. emitter = getEmitterInstance();
  981. listener = getEmitterInstance();
  982. } );
  983. it( 'should return null if listener do not listen to emitter with given id', () => {
  984. expect( _getEmitterListenedTo( listener, 'abc' ) ).to.be.null;
  985. } );
  986. it( 'should return emitter with given id', () => {
  987. listener.listenTo( emitter, 'eventName', () => {} );
  988. const emitterId = _getEmitterId( emitter );
  989. expect( _getEmitterListenedTo( listener, emitterId ) ).to.equal( emitter );
  990. } );
  991. } );
  992. function getEmitterInstance() {
  993. return Object.create( EmitterMixin );
  994. }