emittermixin.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  1. /**
  2. * @license Copyright (c) 2003-2018, 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 only after exact event fired', () => {
  247. const spy1 = sinon.spy();
  248. const spy2 = sinon.spy();
  249. emitter.on( 'foo', spy1 );
  250. emitter.once( 'foo', spy2 );
  251. emitter.fire( 'foo:bar' );
  252. emitter.fire( 'foo' );
  253. emitter.fire( 'foo:bar' );
  254. emitter.fire( 'foo' );
  255. sinon.assert.callCount( spy1, 4 );
  256. sinon.assert.calledTwice( spy2 );
  257. } );
  258. } );
  259. describe( 'off', () => {
  260. it( 'should get callbacks off()', () => {
  261. const spy1 = sinon.spy();
  262. const spy2 = sinon.spy();
  263. const spy3 = sinon.spy();
  264. emitter.on( 'test', spy1 );
  265. emitter.on( 'test', spy2 );
  266. emitter.on( 'test', spy3 );
  267. emitter.fire( 'test' );
  268. emitter.off( 'test', spy2 );
  269. emitter.fire( 'test' );
  270. emitter.fire( 'test' );
  271. sinon.assert.calledThrice( spy1 );
  272. sinon.assert.calledOnce( spy2 );
  273. sinon.assert.calledThrice( spy3 );
  274. } );
  275. it( 'should remove all callbacks for event', () => {
  276. const spy1 = sinon.spy();
  277. const spy2 = sinon.spy();
  278. emitter.on( 'test', spy1 );
  279. emitter.on( 'test', spy2 );
  280. emitter.fire( 'test' );
  281. emitter.off( 'test' );
  282. emitter.fire( 'test' );
  283. emitter.fire( 'test' );
  284. sinon.assert.calledOnce( spy1 );
  285. sinon.assert.calledOnce( spy2 );
  286. } );
  287. it( 'should not fail with unknown events', () => {
  288. emitter.off( 'foo', () => {} );
  289. emitter.off( 'foo:bar', () => {} );
  290. emitter.off( 'foo' );
  291. emitter.off( 'foo:bar' );
  292. } );
  293. it( 'should remove all entries for the same callback', () => {
  294. const spy1 = sinon.spy().named( 1 );
  295. const spy2 = sinon.spy().named( 2 );
  296. emitter.on( 'test', spy1 );
  297. emitter.on( 'test', spy2 );
  298. emitter.on( 'test', spy1 );
  299. emitter.on( 'test', spy2 );
  300. emitter.fire( 'test' );
  301. emitter.off( 'test', spy1 );
  302. emitter.fire( 'test' );
  303. sinon.assert.callCount( spy1, 2 );
  304. sinon.assert.callCount( spy2, 4 );
  305. } );
  306. it( 'should not remove all namespaced entries when removing namespace inner group', () => {
  307. const spy1 = sinon.spy().named( 'foo' );
  308. const spy2 = sinon.spy().named( 'foo:bar' );
  309. const spy3 = sinon.spy().named( 'foo:bar:baz' );
  310. const spy4 = sinon.spy().named( 'foo:bar:baz:abc' );
  311. emitter.on( 'foo', spy1 );
  312. emitter.on( 'foo:bar', spy2 );
  313. emitter.on( 'foo:bar:baz', spy3 );
  314. emitter.on( 'foo:bar:baz:abc', spy4 );
  315. emitter.fire( 'foo:bar:baz:abc' );
  316. sinon.assert.calledOnce( spy1 );
  317. sinon.assert.calledOnce( spy2 );
  318. sinon.assert.calledOnce( spy3 );
  319. sinon.assert.calledOnce( spy4 );
  320. emitter.off( 'foo:bar' );
  321. emitter.fire( 'foo:bar:baz:abc' );
  322. sinon.assert.calledTwice( spy1 );
  323. sinon.assert.calledOnce( spy2 );
  324. sinon.assert.calledTwice( spy3 );
  325. sinon.assert.calledTwice( spy4 );
  326. } );
  327. it( 'should properly remove callbacks for namespaced events', () => {
  328. const spyFoo = sinon.spy();
  329. const spyAbc = sinon.spy();
  330. const spyBar = sinon.spy();
  331. const spyFoo2 = sinon.spy();
  332. emitter.on( 'foo', spyFoo );
  333. emitter.on( 'foo:bar:abc', spyAbc );
  334. emitter.on( 'foo:bar', spyBar );
  335. emitter.on( 'foo', spyFoo2 );
  336. emitter.off( 'foo', spyFoo );
  337. emitter.fire( 'foo:bar:abc' );
  338. sinon.assert.calledOnce( spyAbc );
  339. sinon.assert.calledOnce( spyBar );
  340. sinon.assert.calledOnce( spyFoo2 );
  341. sinon.assert.notCalled( spyFoo );
  342. emitter.fire( 'foo:bar' );
  343. sinon.assert.notCalled( spyFoo );
  344. sinon.assert.calledOnce( spyAbc );
  345. sinon.assert.calledTwice( spyBar );
  346. sinon.assert.calledTwice( spyFoo2 );
  347. emitter.fire( 'foo' );
  348. sinon.assert.notCalled( spyFoo );
  349. sinon.assert.calledOnce( spyAbc );
  350. sinon.assert.calledTwice( spyBar );
  351. sinon.assert.calledThrice( spyFoo2 );
  352. } );
  353. } );
  354. describe( 'listenTo', () => {
  355. it( 'should properly register callbacks', () => {
  356. const spy = sinon.spy();
  357. listener.listenTo( emitter, 'test', spy );
  358. emitter.fire( 'test' );
  359. sinon.assert.called( spy );
  360. } );
  361. it( 'should correctly listen to namespaced events', () => {
  362. const spyFoo = sinon.spy();
  363. const spyBar = sinon.spy();
  364. const spyBaz = sinon.spy();
  365. listener.listenTo( emitter, 'foo', spyFoo );
  366. listener.listenTo( emitter, 'foo:bar', spyBar );
  367. listener.listenTo( emitter, 'foo:bar:baz', spyBaz );
  368. emitter.fire( 'foo:bar:baz' );
  369. sinon.assert.calledOnce( spyFoo );
  370. sinon.assert.calledOnce( spyBar );
  371. sinon.assert.calledOnce( spyBaz );
  372. emitter.fire( 'foo:bar' );
  373. sinon.assert.calledTwice( spyFoo );
  374. sinon.assert.calledTwice( spyBar );
  375. sinon.assert.calledOnce( spyBaz );
  376. emitter.fire( 'foo' );
  377. sinon.assert.calledThrice( spyFoo );
  378. sinon.assert.calledTwice( spyBar );
  379. sinon.assert.calledOnce( spyBaz );
  380. } );
  381. } );
  382. describe( 'stopListening', () => {
  383. it( 'should stop listening to given event callback', () => {
  384. const spy1 = sinon.spy();
  385. const spy2 = sinon.spy();
  386. listener.listenTo( emitter, 'event1', spy1 );
  387. listener.listenTo( emitter, 'event2', spy2 );
  388. emitter.fire( 'event1' );
  389. emitter.fire( 'event2' );
  390. listener.stopListening( emitter, 'event1', spy1 );
  391. emitter.fire( 'event1' );
  392. emitter.fire( 'event2' );
  393. sinon.assert.calledOnce( spy1 );
  394. sinon.assert.calledTwice( spy2 );
  395. } );
  396. it( 'should stop listening to given event', () => {
  397. const spy1a = sinon.spy();
  398. const spy1b = sinon.spy();
  399. const spy2 = sinon.spy();
  400. listener.listenTo( emitter, 'event1', spy1a );
  401. listener.listenTo( emitter, 'event1', spy1b );
  402. listener.listenTo( emitter, 'event2', spy2 );
  403. emitter.fire( 'event1' );
  404. emitter.fire( 'event2' );
  405. listener.stopListening( emitter, 'event1' );
  406. emitter.fire( 'event1' );
  407. emitter.fire( 'event2' );
  408. sinon.assert.calledOnce( spy1a );
  409. sinon.assert.calledOnce( spy1b );
  410. sinon.assert.calledTwice( spy2 );
  411. } );
  412. it( 'should stop listening to all events from given emitter', () => {
  413. const spy1 = sinon.spy();
  414. const spy2 = sinon.spy();
  415. const spy3 = sinon.spy();
  416. const spy4 = sinon.spy();
  417. listener.listenTo( emitter, 'event1', spy1 );
  418. listener.listenTo( emitter, 'event2', spy2 );
  419. listener.listenTo( emitter, 'foo', spy3 );
  420. listener.listenTo( emitter, 'foo:bar:baz', spy4 );
  421. emitter.fire( 'event1' );
  422. emitter.fire( 'event2' );
  423. emitter.fire( 'foo:bar:baz' );
  424. listener.stopListening( emitter );
  425. emitter.fire( 'event1' );
  426. emitter.fire( 'event2' );
  427. emitter.fire( 'foo:bar:baz' );
  428. sinon.assert.calledOnce( spy1 );
  429. sinon.assert.calledOnce( spy2 );
  430. sinon.assert.calledOnce( spy3 );
  431. sinon.assert.calledOnce( spy4 );
  432. } );
  433. it( 'should stop listening to everything', () => {
  434. const spy1 = sinon.spy();
  435. const spy2 = sinon.spy();
  436. const spy3 = sinon.spy();
  437. const spy4 = sinon.spy();
  438. const emitter1 = getEmitterInstance();
  439. const emitter2 = getEmitterInstance();
  440. listener.listenTo( emitter1, 'event1', spy1 );
  441. listener.listenTo( emitter2, 'event2', spy2 );
  442. listener.listenTo( emitter1, 'foo', spy3 );
  443. listener.listenTo( emitter1, 'foo:bar:baz', spy4 );
  444. emitter1.fire( 'event1' );
  445. emitter2.fire( 'event2' );
  446. emitter1.fire( 'foo' );
  447. emitter1.fire( 'foo:bar' );
  448. emitter1.fire( 'foo:bar:baz' );
  449. listener.stopListening();
  450. emitter1.fire( 'event1' );
  451. emitter2.fire( 'event2' );
  452. emitter1.fire( 'foo' );
  453. emitter1.fire( 'foo:bar' );
  454. emitter1.fire( 'foo:bar:baz' );
  455. sinon.assert.calledOnce( spy1 );
  456. sinon.assert.calledOnce( spy2 );
  457. sinon.assert.calledThrice( spy3 );
  458. sinon.assert.calledOnce( spy4 );
  459. } );
  460. it( 'should not stop other emitters when a non-listened emitter is provided', () => {
  461. const spy = sinon.spy();
  462. const emitter1 = getEmitterInstance();
  463. const emitter2 = getEmitterInstance();
  464. listener.listenTo( emitter1, 'test', spy );
  465. listener.stopListening( emitter2 );
  466. emitter1.fire( 'test' );
  467. sinon.assert.called( spy );
  468. } );
  469. it( 'should correctly stop listening to namespaced events', () => {
  470. const spyFoo = sinon.spy();
  471. const spyBar = sinon.spy();
  472. const spyBaz = sinon.spy();
  473. listener.listenTo( emitter, 'foo', spyFoo );
  474. listener.listenTo( emitter, 'foo:bar', spyBar );
  475. listener.listenTo( emitter, 'foo:bar:baz', spyBaz );
  476. listener.stopListening( emitter, 'foo' );
  477. emitter.fire( 'foo:bar:baz' );
  478. sinon.assert.notCalled( spyFoo );
  479. sinon.assert.calledOnce( spyBar );
  480. sinon.assert.calledOnce( spyBaz );
  481. } );
  482. it( 'should correctly stop listening to namespaced events when removing specialised event', () => {
  483. const spyFoo = sinon.spy();
  484. const spyBar = sinon.spy();
  485. const spyBaz = sinon.spy();
  486. listener.listenTo( emitter, 'foo', spyFoo );
  487. listener.listenTo( emitter, 'foo:bar', spyBar );
  488. listener.listenTo( emitter, 'foo:bar:baz', spyBaz );
  489. listener.stopListening( emitter, 'foo:bar' );
  490. emitter.fire( 'foo:bar:baz' );
  491. sinon.assert.calledOnce( spyFoo );
  492. sinon.assert.notCalled( spyBar );
  493. sinon.assert.calledOnce( spyBaz );
  494. } );
  495. it( 'should not fail with unknown events', () => {
  496. listener.stopListening( emitter, 'foo', () => {} );
  497. listener.stopListening( emitter, 'foo:bar', () => {} );
  498. listener.stopListening( emitter, 'foo' );
  499. listener.stopListening( emitter, 'foo:bar' );
  500. } );
  501. it( 'should not fail with unknown emitter', () => {
  502. listener.listenTo( emitter, 'foo:bar', () => {} );
  503. listener.stopListening( {}, 'foo', () => {} );
  504. listener.stopListening( {}, 'foo:bar', () => {} );
  505. listener.stopListening( {}, 'foo' );
  506. listener.stopListening( {}, 'foo:bar' );
  507. listener.stopListening( {} );
  508. } );
  509. it( 'should not fail with unknown callbacks', () => {
  510. const spy = sinon.spy();
  511. listener.listenTo( emitter, 'foo', spy );
  512. listener.stopListening( emitter, 'foo', () => {} );
  513. emitter.fire( 'foo' );
  514. sinon.assert.calledOnce( spy );
  515. } );
  516. } );
  517. describe( 'delegate', () => {
  518. it( 'should chain for a single event', () => {
  519. const emitter = getEmitterInstance();
  520. expect( emitter.delegate( 'foo' ) ).to.contain.keys( 'to' );
  521. } );
  522. it( 'should chain for multiple events', () => {
  523. const emitter = getEmitterInstance();
  524. expect( emitter.delegate( 'foo', 'bar' ) ).to.contain.keys( 'to' );
  525. } );
  526. describe( 'to', () => {
  527. it( 'forwards an event to another emitter', done => {
  528. const emitterA = getEmitterInstance();
  529. const emitterB = getEmitterInstance();
  530. const dataA = {};
  531. const dataB = {};
  532. emitterB.delegate( 'foo' ).to( emitterA );
  533. emitterA.on( 'foo', ( ...args ) => {
  534. assertDelegated( args, {
  535. expectedSource: emitterB,
  536. expectedName: 'foo',
  537. expectedPath: [ emitterB, emitterA ],
  538. expectedData: [ dataA, dataB ]
  539. } );
  540. done();
  541. } );
  542. emitterB.fire( 'foo', dataA, dataB );
  543. } );
  544. it( 'forwards multiple events to another emitter', () => {
  545. const emitterA = getEmitterInstance();
  546. const emitterB = getEmitterInstance();
  547. const spyFoo = sinon.spy();
  548. const spyBar = sinon.spy();
  549. const spyBaz = sinon.spy();
  550. const dataA = {};
  551. const dataB = {};
  552. emitterB.delegate( 'foo', 'bar', 'baz' ).to( emitterA );
  553. emitterA.on( 'foo', spyFoo );
  554. emitterA.on( 'bar', spyBar );
  555. emitterA.on( 'baz', spyBaz );
  556. emitterB.fire( 'foo', dataA, dataB );
  557. sinon.assert.calledOnce( spyFoo );
  558. sinon.assert.notCalled( spyBar );
  559. sinon.assert.notCalled( spyBaz );
  560. assertDelegated( spyFoo.args[ 0 ], {
  561. expectedSource: emitterB,
  562. expectedName: 'foo',
  563. expectedPath: [ emitterB, emitterA ],
  564. expectedData: [ dataA, dataB ]
  565. } );
  566. emitterB.fire( 'bar' );
  567. sinon.assert.calledOnce( spyFoo );
  568. sinon.assert.calledOnce( spyBar );
  569. sinon.assert.notCalled( spyBaz );
  570. assertDelegated( spyBar.args[ 0 ], {
  571. expectedSource: emitterB,
  572. expectedName: 'bar',
  573. expectedPath: [ emitterB, emitterA ],
  574. expectedData: []
  575. } );
  576. emitterB.fire( 'baz' );
  577. sinon.assert.calledOnce( spyFoo );
  578. sinon.assert.calledOnce( spyBar );
  579. sinon.assert.calledOnce( spyBaz );
  580. assertDelegated( spyBaz.args[ 0 ], {
  581. expectedSource: emitterB,
  582. expectedName: 'baz',
  583. expectedPath: [ emitterB, emitterA ],
  584. expectedData: []
  585. } );
  586. emitterB.fire( 'not-delegated' );
  587. sinon.assert.calledOnce( spyFoo );
  588. sinon.assert.calledOnce( spyBar );
  589. sinon.assert.calledOnce( spyBaz );
  590. } );
  591. it( 'does not forward events which are not supposed to be delegated', () => {
  592. const emitterA = getEmitterInstance();
  593. const emitterB = getEmitterInstance();
  594. const spyFoo = sinon.spy();
  595. const spyBar = sinon.spy();
  596. const spyBaz = sinon.spy();
  597. emitterB.delegate( 'foo', 'bar', 'baz' ).to( emitterA );
  598. emitterA.on( 'foo', spyFoo );
  599. emitterA.on( 'bar', spyBar );
  600. emitterA.on( 'baz', spyBaz );
  601. emitterB.fire( 'foo' );
  602. emitterB.fire( 'bar' );
  603. emitterB.fire( 'baz' );
  604. emitterB.fire( 'not-delegated' );
  605. sinon.assert.callOrder( spyFoo, spyBar, spyBaz );
  606. sinon.assert.callCount( spyFoo, 1 );
  607. sinon.assert.callCount( spyBar, 1 );
  608. sinon.assert.callCount( spyBaz, 1 );
  609. } );
  610. it( 'supports deep chain event delegation', done => {
  611. const emitterA = getEmitterInstance();
  612. const emitterB = getEmitterInstance();
  613. const emitterC = getEmitterInstance();
  614. const data = {};
  615. emitterC.delegate( 'foo' ).to( emitterB );
  616. emitterB.delegate( 'foo' ).to( emitterA );
  617. emitterA.on( 'foo', ( ...args ) => {
  618. assertDelegated( args, {
  619. expectedSource: emitterC,
  620. expectedName: 'foo',
  621. expectedPath: [ emitterC, emitterB, emitterA ],
  622. expectedData: [ data ]
  623. } );
  624. done();
  625. } );
  626. emitterC.fire( 'foo', data );
  627. } );
  628. it( 'preserves path in event delegation', done => {
  629. const data = {};
  630. const emitterA = getEmitterInstance();
  631. const emitterB = getEmitterInstance();
  632. const emitterC = getEmitterInstance();
  633. const emitterD = getEmitterInstance();
  634. emitterB.delegate( 'foo' ).to( emitterA );
  635. emitterB.delegate( 'foo' ).to( emitterC );
  636. emitterB.delegate( 'foo' ).to( emitterD );
  637. emitterD.on( 'foo', ( ...args ) => {
  638. assertDelegated( args, {
  639. expectedSource: emitterB,
  640. expectedName: 'foo',
  641. expectedPath: [ emitterB, emitterD ],
  642. expectedData: [ data ]
  643. } );
  644. done();
  645. } );
  646. emitterB.fire( 'foo', data );
  647. emitterC.fire( 'foo', data );
  648. } );
  649. it( 'executes callbacks first, then delegates further', () => {
  650. const emitterA = getEmitterInstance();
  651. const emitterB = getEmitterInstance();
  652. const spyA = sinon.spy();
  653. const spyB = sinon.spy();
  654. emitterB.delegate( 'foo' ).to( emitterA );
  655. emitterA.on( 'foo', spyA );
  656. emitterB.on( 'foo', spyB );
  657. emitterB.fire( 'foo' );
  658. sinon.assert.callOrder( spyB, spyA );
  659. } );
  660. it( 'supports delegation under a different name', () => {
  661. const emitterA = getEmitterInstance();
  662. const emitterB = getEmitterInstance();
  663. const emitterC = getEmitterInstance();
  664. const emitterD = getEmitterInstance();
  665. const spyAFoo = sinon.spy();
  666. const spyABar = sinon.spy();
  667. const spyCBaz = sinon.spy();
  668. const spyDFoo = sinon.spy();
  669. emitterB.delegate( 'foo' ).to( emitterA, 'bar' );
  670. emitterB.delegate( 'foo' ).to( emitterC, name => name + '-baz' );
  671. emitterB.delegate( 'foo' ).to( emitterD );
  672. emitterA.on( 'foo', spyAFoo );
  673. emitterA.on( 'bar', spyABar );
  674. emitterC.on( 'foo-baz', spyCBaz );
  675. emitterD.on( 'foo', spyDFoo );
  676. emitterB.fire( 'foo' );
  677. sinon.assert.calledOnce( spyABar );
  678. sinon.assert.calledOnce( spyCBaz );
  679. sinon.assert.calledOnce( spyDFoo );
  680. sinon.assert.notCalled( spyAFoo );
  681. } );
  682. it( 'supports delegation under a different name with multiple events', () => {
  683. const emitterA = getEmitterInstance();
  684. const emitterB = getEmitterInstance();
  685. const spyAFoo = sinon.spy();
  686. const spyABar = sinon.spy();
  687. const spyABaz = sinon.spy();
  688. const spyAQux = sinon.spy();
  689. emitterB.delegate( 'foo', 'bar', 'baz' ).to( emitterA, 'qux' );
  690. emitterA.on( 'foo', spyAFoo );
  691. emitterA.on( 'bar', spyABar );
  692. emitterA.on( 'baz', spyABaz );
  693. emitterA.on( 'qux', spyAQux );
  694. emitterB.fire( 'foo' );
  695. emitterB.fire( 'baz' );
  696. emitterB.fire( 'bar' );
  697. sinon.assert.notCalled( spyAFoo );
  698. sinon.assert.notCalled( spyABar );
  699. sinon.assert.notCalled( spyABaz );
  700. sinon.assert.calledThrice( spyAQux );
  701. } );
  702. it( 'supports delegation with multiple events, each under a different name', () => {
  703. const emitterA = getEmitterInstance();
  704. const emitterB = getEmitterInstance();
  705. const spyAFoo = sinon.spy();
  706. const spyABar = sinon.spy();
  707. const spyABaz = sinon.spy();
  708. const spyAFooQux = sinon.spy();
  709. const spyABarQux = sinon.spy();
  710. const spyABazQux = sinon.spy();
  711. emitterB.delegate( 'foo', 'bar', 'baz' ).to( emitterA, name => name + '-qux' );
  712. emitterA.on( 'foo', spyAFoo );
  713. emitterA.on( 'bar', spyABar );
  714. emitterA.on( 'baz', spyABaz );
  715. emitterA.on( 'foo-qux', spyAFooQux );
  716. emitterA.on( 'bar-qux', spyABarQux );
  717. emitterA.on( 'baz-qux', spyABazQux );
  718. emitterB.fire( 'foo' );
  719. emitterB.fire( 'baz' );
  720. emitterB.fire( 'bar' );
  721. sinon.assert.notCalled( spyAFoo );
  722. sinon.assert.notCalled( spyABar );
  723. sinon.assert.notCalled( spyABaz );
  724. sinon.assert.calledOnce( spyAFooQux );
  725. sinon.assert.calledOnce( spyABarQux );
  726. sinon.assert.calledOnce( spyABazQux );
  727. sinon.assert.callOrder( spyAFooQux, spyABazQux, spyABarQux );
  728. } );
  729. it( 'preserves path in delegation under a different name', done => {
  730. const data = {};
  731. const emitterA = getEmitterInstance();
  732. const emitterB = getEmitterInstance();
  733. const emitterC = getEmitterInstance();
  734. const emitterD = getEmitterInstance();
  735. emitterB.delegate( 'foo' ).to( emitterA, 'bar' );
  736. emitterB.delegate( 'foo' ).to( emitterC, 'baz' );
  737. emitterB.delegate( 'foo' ).to( emitterD );
  738. emitterD.on( 'foo', ( ...args ) => {
  739. assertDelegated( args, {
  740. expectedSource: emitterB,
  741. expectedName: 'foo',
  742. expectedPath: [ emitterB, emitterD ],
  743. expectedData: [ data ]
  744. } );
  745. done();
  746. } );
  747. emitterB.fire( 'foo', data );
  748. } );
  749. it( 'supports delegation of all events', () => {
  750. const emitterA = getEmitterInstance();
  751. const emitterB = getEmitterInstance();
  752. const spyAFoo = sinon.spy();
  753. const spyABar = sinon.spy();
  754. const spyABaz = sinon.spy();
  755. emitterB.delegate( '*' ).to( emitterA );
  756. emitterA.on( 'foo', spyAFoo );
  757. emitterA.on( 'bar', spyABar );
  758. emitterA.on( 'baz', spyABaz );
  759. emitterB.fire( 'foo' );
  760. emitterB.fire( 'baz' );
  761. emitterB.fire( 'bar' );
  762. sinon.assert.callOrder( spyAFoo, spyABaz, spyABar );
  763. } );
  764. it( 'supports delegation of all events under different names', () => {
  765. const emitterA = getEmitterInstance();
  766. const emitterB = getEmitterInstance();
  767. const spyAFoo = sinon.spy();
  768. const spyABar = sinon.spy();
  769. const spyABaz = sinon.spy();
  770. const spyAFooDel = sinon.spy();
  771. const spyABarDel = sinon.spy();
  772. const spyABazDel = sinon.spy();
  773. emitterB.delegate( '*' ).to( emitterA, name => name + '-delegated' );
  774. emitterA.on( 'foo', spyAFoo );
  775. emitterA.on( 'bar', spyABar );
  776. emitterA.on( 'baz', spyABaz );
  777. emitterA.on( 'foo-delegated', spyAFooDel );
  778. emitterA.on( 'bar-delegated', spyABarDel );
  779. emitterA.on( 'baz-delegated', spyABazDel );
  780. emitterB.fire( 'foo' );
  781. emitterB.fire( 'baz' );
  782. emitterB.fire( 'bar' );
  783. sinon.assert.notCalled( spyAFoo );
  784. sinon.assert.notCalled( spyABar );
  785. sinon.assert.notCalled( spyABaz );
  786. sinon.assert.callOrder( spyAFooDel, spyABazDel, spyABarDel );
  787. } );
  788. } );
  789. } );
  790. describe( 'stopDelegating', () => {
  791. it( 'passes if no delegation was set', () => {
  792. expect( () => {
  793. getEmitterInstance().stopDelegating();
  794. } ).to.not.throw();
  795. } );
  796. it( 'stops delegating all events to all emitters', () => {
  797. const emitterA = getEmitterInstance();
  798. const emitterB = getEmitterInstance();
  799. const emitterC = getEmitterInstance();
  800. const spyFoo = sinon.spy();
  801. const spyBar = sinon.spy();
  802. emitterA.delegate( 'foo' ).to( emitterB );
  803. emitterA.delegate( 'bar' ).to( emitterC );
  804. emitterB.on( 'foo', spyFoo );
  805. emitterC.on( 'bar', spyBar );
  806. emitterA.fire( 'foo' );
  807. emitterA.fire( 'bar' );
  808. sinon.assert.callOrder( spyFoo, spyBar );
  809. emitterA.stopDelegating();
  810. emitterA.fire( 'foo' );
  811. emitterA.fire( 'bar' );
  812. sinon.assert.callOrder( spyFoo, spyBar );
  813. } );
  814. it( 'stops delegating a specific event to all emitters', () => {
  815. const emitterA = getEmitterInstance();
  816. const emitterB = getEmitterInstance();
  817. const emitterC = getEmitterInstance();
  818. const spyFooB = sinon.spy();
  819. const spyFooC = sinon.spy();
  820. const spyBarC = sinon.spy();
  821. emitterA.delegate( 'foo' ).to( emitterB );
  822. emitterA.delegate( 'foo' ).to( emitterC );
  823. emitterA.delegate( 'bar' ).to( emitterC );
  824. emitterB.on( 'foo', spyFooB );
  825. emitterC.on( 'foo', spyFooC );
  826. emitterC.on( 'bar', spyBarC );
  827. emitterA.fire( 'foo' );
  828. emitterA.fire( 'bar' );
  829. sinon.assert.callOrder( spyFooB, spyFooC, spyBarC );
  830. emitterA.stopDelegating( 'foo' );
  831. emitterA.fire( 'foo' );
  832. emitterA.fire( 'bar' );
  833. sinon.assert.callOrder( spyFooB, spyFooC, spyBarC, spyBarC );
  834. } );
  835. it( 'stops delegating a specific event to a specific emitter', () => {
  836. const emitterA = getEmitterInstance();
  837. const emitterB = getEmitterInstance();
  838. const emitterC = getEmitterInstance();
  839. const spyFooB = sinon.spy();
  840. const spyFooC = sinon.spy();
  841. emitterA.delegate( 'foo' ).to( emitterB );
  842. emitterA.delegate( 'foo' ).to( emitterC );
  843. emitterB.on( 'foo', spyFooB );
  844. emitterC.on( 'foo', spyFooC );
  845. emitterA.fire( 'foo' );
  846. sinon.assert.callOrder( spyFooB, spyFooC );
  847. emitterA.stopDelegating( 'foo', emitterC );
  848. emitterA.fire( 'foo' );
  849. sinon.assert.callOrder( spyFooB, spyFooC, spyFooB );
  850. } );
  851. it( 'stops delegating a specific event under a different name to a specific emitter', () => {
  852. const emitterA = getEmitterInstance();
  853. const emitterB = getEmitterInstance();
  854. const emitterC = getEmitterInstance();
  855. const spyFooB = sinon.spy();
  856. const spyFooC = sinon.spy();
  857. emitterA.delegate( 'foo' ).to( emitterB );
  858. emitterA.delegate( 'foo' ).to( emitterC, 'bar' );
  859. emitterB.on( 'foo', spyFooB );
  860. emitterC.on( 'bar', spyFooC );
  861. emitterA.fire( 'foo' );
  862. sinon.assert.callOrder( spyFooB, spyFooC );
  863. emitterA.stopDelegating( 'foo', emitterC );
  864. emitterA.fire( 'foo' );
  865. sinon.assert.callOrder( spyFooB, spyFooC, spyFooB );
  866. } );
  867. it( 'stops delegating all ("*")', () => {
  868. const emitterA = getEmitterInstance();
  869. const emitterB = getEmitterInstance();
  870. const emitterC = getEmitterInstance();
  871. const spyAFoo = sinon.spy();
  872. const spyABar = sinon.spy();
  873. const spyCFoo = sinon.spy();
  874. const spyCBar = sinon.spy();
  875. emitterB.delegate( '*' ).to( emitterA );
  876. emitterB.delegate( '*' ).to( emitterC );
  877. emitterA.on( 'foo', spyAFoo );
  878. emitterA.on( 'bar', spyABar );
  879. emitterC.on( 'foo', spyCFoo );
  880. emitterC.on( 'bar', spyCBar );
  881. emitterB.fire( 'foo' );
  882. emitterB.fire( 'bar' );
  883. sinon.assert.calledOnce( spyAFoo );
  884. sinon.assert.calledOnce( spyABar );
  885. sinon.assert.calledOnce( spyCFoo );
  886. sinon.assert.calledOnce( spyCBar );
  887. emitterB.stopDelegating( '*' );
  888. emitterB.fire( 'foo' );
  889. emitterB.fire( 'bar' );
  890. sinon.assert.calledOnce( spyAFoo );
  891. sinon.assert.calledOnce( spyABar );
  892. sinon.assert.calledOnce( spyCFoo );
  893. sinon.assert.calledOnce( spyCBar );
  894. } );
  895. it( 'stops delegating all ("*") to a specific emitter', () => {
  896. const emitterA = getEmitterInstance();
  897. const emitterB = getEmitterInstance();
  898. const emitterC = getEmitterInstance();
  899. const spyAFoo = sinon.spy();
  900. const spyABar = sinon.spy();
  901. const spyCFoo = sinon.spy();
  902. const spyCBar = sinon.spy();
  903. emitterB.delegate( '*' ).to( emitterA );
  904. emitterB.delegate( 'foo' ).to( emitterC );
  905. emitterA.on( 'foo', spyAFoo );
  906. emitterA.on( 'bar', spyABar );
  907. emitterC.on( 'foo', spyCFoo );
  908. emitterC.on( 'bar', spyCBar );
  909. emitterB.fire( 'foo' );
  910. emitterB.fire( 'bar' );
  911. sinon.assert.calledOnce( spyAFoo );
  912. sinon.assert.calledOnce( spyABar );
  913. sinon.assert.calledOnce( spyCFoo );
  914. sinon.assert.notCalled( spyCBar );
  915. emitterB.stopDelegating( '*', emitterA );
  916. emitterB.fire( 'foo' );
  917. emitterB.fire( 'bar' );
  918. sinon.assert.calledOnce( spyAFoo );
  919. sinon.assert.calledOnce( spyABar );
  920. sinon.assert.calledTwice( spyCFoo );
  921. sinon.assert.notCalled( spyCBar );
  922. } );
  923. it( 'passes when stopping delegation of a specific event which has never been delegated', () => {
  924. const emitterA = getEmitterInstance();
  925. const emitterB = getEmitterInstance();
  926. expect( () => {
  927. emitterA.stopDelegating( 'bar' );
  928. emitterA.stopDelegating( 'bar', emitterB );
  929. } ).to.not.throw();
  930. } );
  931. it( 'passes when stopping delegation of a specific event to an emitter which wasn\'t a destination', () => {
  932. const emitterA = getEmitterInstance();
  933. const emitterB = getEmitterInstance();
  934. const emitterC = getEmitterInstance();
  935. emitterA.delegate( 'foo' ).to( emitterB );
  936. expect( () => {
  937. emitterA.stopDelegating( 'foo', emitterC );
  938. } ).to.not.throw();
  939. } );
  940. it( 'passes when stopping delegation of a specific event to a specific emitter which has never been delegated', () => {
  941. const emitterA = getEmitterInstance();
  942. const emitterB = getEmitterInstance();
  943. const emitterC = getEmitterInstance();
  944. emitterA.delegate( 'foo' ).to( emitterB );
  945. expect( () => {
  946. emitterA.stopDelegating( 'bar', emitterC );
  947. } ).to.not.throw();
  948. } );
  949. } );
  950. function assertDelegated( evtArgs, { expectedName, expectedSource, expectedPath, expectedData } ) {
  951. const evtInfo = evtArgs[ 0 ];
  952. expect( evtInfo.name ).to.equal( expectedName );
  953. expect( evtInfo.source ).to.equal( expectedSource );
  954. expect( evtInfo.path ).to.deep.equal( expectedPath );
  955. expect( evtArgs.slice( 1 ) ).to.deep.equal( expectedData );
  956. }
  957. } );
  958. describe( 'emitter id', () => {
  959. let emitter;
  960. beforeEach( () => {
  961. emitter = getEmitterInstance();
  962. } );
  963. it( 'should be undefined before it is set', () => {
  964. expect( _getEmitterId( emitter ) ).to.be.undefined;
  965. } );
  966. it( 'should be settable but only once', () => {
  967. _setEmitterId( emitter, 'abc' );
  968. expect( _getEmitterId( emitter ) ).to.equal( 'abc' );
  969. _setEmitterId( emitter, 'xyz' );
  970. expect( _getEmitterId( emitter ) ).to.equal( 'abc' );
  971. } );
  972. } );
  973. describe( '_getEmitterListenedTo', () => {
  974. let emitter, listener;
  975. beforeEach( () => {
  976. emitter = getEmitterInstance();
  977. listener = getEmitterInstance();
  978. } );
  979. it( 'should return null if listener do not listen to emitter with given id', () => {
  980. expect( _getEmitterListenedTo( listener, 'abc' ) ).to.be.null;
  981. } );
  982. it( 'should return emitter with given id', () => {
  983. listener.listenTo( emitter, 'eventName', () => {} );
  984. const emitterId = _getEmitterId( emitter );
  985. expect( _getEmitterListenedTo( listener, emitterId ) ).to.equal( emitter );
  986. } );
  987. } );
  988. function getEmitterInstance() {
  989. return Object.create( EmitterMixin );
  990. }