emittermixin.js 32 KB

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