emittermixin.js 36 KB

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