emittermixin.js 35 KB

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