emittermixin.js 36 KB

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