8
0

emittermixin.js 36 KB

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