emittermixin.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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. } );
  130. describe( 'on', () => {
  131. it( 'should stop()', () => {
  132. const spy1 = sinon.spy();
  133. const spy2 = sinon.spy();
  134. const spy3 = sinon.spy( event => {
  135. event.stop();
  136. } );
  137. emitter.on( 'test', spy1 );
  138. emitter.on( 'test', spy2 );
  139. emitter.on( 'test', spy3 );
  140. emitter.on( 'test', sinon.stub().throws() );
  141. emitter.on( 'test', sinon.stub().throws() );
  142. emitter.fire( 'test' );
  143. sinon.assert.called( spy1 );
  144. sinon.assert.called( spy2 );
  145. sinon.assert.called( spy3 );
  146. } );
  147. it( 'should take a callback off()', () => {
  148. const spy1 = sinon.spy();
  149. const spy2 = sinon.spy( event => {
  150. event.off();
  151. } );
  152. const spy3 = sinon.spy();
  153. emitter.on( 'test', spy1 );
  154. emitter.on( 'test', spy2 );
  155. emitter.on( 'test', spy3 );
  156. emitter.fire( 'test' );
  157. emitter.fire( 'test' );
  158. sinon.assert.calledTwice( spy1 );
  159. sinon.assert.calledOnce( spy2 );
  160. sinon.assert.calledTwice( spy3 );
  161. } );
  162. it( 'should take the callback off() even after stop()', () => {
  163. const spy1 = sinon.spy( event => {
  164. event.stop();
  165. event.off();
  166. } );
  167. const spy2 = sinon.spy();
  168. emitter.on( 'test', spy1 );
  169. emitter.on( 'test', spy2 );
  170. emitter.fire( 'test' );
  171. emitter.fire( 'test' );
  172. sinon.assert.calledOnce( spy1 );
  173. sinon.assert.calledOnce( spy2 );
  174. } );
  175. } );
  176. describe( 'once', () => {
  177. it( 'should be called just once', () => {
  178. const spy1 = sinon.spy();
  179. const spy2 = sinon.spy();
  180. const spy3 = sinon.spy();
  181. emitter.on( 'test', spy1 );
  182. emitter.once( 'test', spy2 );
  183. emitter.on( 'test', spy3 );
  184. emitter.fire( 'test' );
  185. emitter.fire( 'test' );
  186. sinon.assert.calledTwice( spy1 );
  187. sinon.assert.calledOnce( spy2 );
  188. sinon.assert.calledTwice( spy3 );
  189. } );
  190. it( 'should have proper scope', () => {
  191. const ctx = {};
  192. const spy1 = sinon.spy();
  193. const spy2 = sinon.spy();
  194. emitter.once( 'test', spy1, { context: ctx } );
  195. emitter.once( 'test', spy2 );
  196. emitter.fire( 'test' );
  197. sinon.assert.calledOn( spy1, ctx );
  198. sinon.assert.calledOn( spy2, emitter );
  199. } );
  200. it( 'should have proper arguments', () => {
  201. const spy = sinon.spy();
  202. emitter.once( 'test', spy );
  203. emitter.fire( 'test', 1, 2, 3 );
  204. sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), 1, 2, 3 );
  205. } );
  206. } );
  207. describe( 'off', () => {
  208. it( 'should get callbacks off()', () => {
  209. const spy1 = sinon.spy();
  210. const spy2 = sinon.spy();
  211. const spy3 = sinon.spy();
  212. emitter.on( 'test', spy1 );
  213. emitter.on( 'test', spy2 );
  214. emitter.on( 'test', spy3 );
  215. emitter.fire( 'test' );
  216. emitter.off( 'test', spy2 );
  217. emitter.fire( 'test' );
  218. emitter.fire( 'test' );
  219. sinon.assert.calledThrice( spy1 );
  220. sinon.assert.calledOnce( spy2 );
  221. sinon.assert.calledThrice( spy3 );
  222. } );
  223. it( 'should not fail with unknown events', () => {
  224. emitter.off( 'test', () => {} );
  225. } );
  226. it( 'should remove all entries for the same callback', () => {
  227. const spy1 = sinon.spy().named( 1 );
  228. const spy2 = sinon.spy().named( 2 );
  229. emitter.on( 'test', spy1 );
  230. emitter.on( 'test', spy2 );
  231. emitter.on( 'test', spy1 );
  232. emitter.on( 'test', spy2 );
  233. emitter.fire( 'test' );
  234. emitter.off( 'test', spy1 );
  235. emitter.fire( 'test' );
  236. sinon.assert.callCount( spy1, 2 );
  237. sinon.assert.callCount( spy2, 4 );
  238. } );
  239. it( 'should remove the callback for given context only', () => {
  240. const spy = sinon.spy().named( 1 );
  241. const ctx1 = { context: 1 };
  242. const ctx2 = { context: 2 };
  243. emitter.on( 'test', spy, { context: ctx1 } );
  244. emitter.on( 'test', spy, { context: ctx2 } );
  245. emitter.fire( 'test' );
  246. spy.reset();
  247. emitter.off( 'test', spy, ctx1 );
  248. emitter.fire( 'test' );
  249. sinon.assert.calledOnce( spy );
  250. sinon.assert.calledOn( spy, ctx2 );
  251. } );
  252. it( 'should properly remove callbacks for namespaced events', () => {
  253. const spyFoo = sinon.spy();
  254. const spyAbc = sinon.spy();
  255. const spyBar = sinon.spy();
  256. const spyFoo2 = sinon.spy();
  257. emitter.on( 'foo', spyFoo );
  258. emitter.on( 'foo:bar:abc', spyAbc );
  259. emitter.on( 'foo:bar', spyBar );
  260. emitter.on( 'foo', spyFoo2 );
  261. emitter.off( 'foo', spyFoo );
  262. emitter.fire( 'foo:bar:abc' );
  263. sinon.assert.calledOnce( spyAbc );
  264. sinon.assert.calledOnce( spyBar );
  265. sinon.assert.calledOnce( spyFoo2 );
  266. sinon.assert.notCalled( spyFoo );
  267. emitter.fire( 'foo:bar' );
  268. sinon.assert.notCalled( spyFoo );
  269. sinon.assert.calledOnce( spyAbc );
  270. sinon.assert.calledTwice( spyBar );
  271. sinon.assert.calledTwice( spyFoo2 );
  272. emitter.fire( 'foo' );
  273. sinon.assert.notCalled( spyFoo );
  274. sinon.assert.calledOnce( spyAbc );
  275. sinon.assert.calledTwice( spyBar );
  276. sinon.assert.calledThrice( spyFoo2 );
  277. } );
  278. } );
  279. describe( 'listenTo', () => {
  280. it( 'should properly register callbacks', () => {
  281. const spy = sinon.spy();
  282. listener.listenTo( emitter, 'test', spy );
  283. emitter.fire( 'test' );
  284. sinon.assert.called( spy );
  285. } );
  286. it( 'should correctly listen to namespaced events', () => {
  287. const spyFoo = sinon.spy();
  288. const spyBar = sinon.spy();
  289. listener.listenTo( emitter, 'foo', spyFoo );
  290. listener.listenTo( emitter, 'foo:bar', spyBar );
  291. emitter.fire( 'foo:bar' );
  292. sinon.assert.calledOnce( spyFoo );
  293. sinon.assert.calledOnce( spyBar );
  294. emitter.fire( 'foo' );
  295. sinon.assert.calledTwice( spyFoo );
  296. sinon.assert.calledOnce( spyBar );
  297. } );
  298. } );
  299. describe( 'stopListening', () => {
  300. it( 'should stop listening to given event callback', () => {
  301. const spy1 = sinon.spy();
  302. const spy2 = sinon.spy();
  303. listener.listenTo( emitter, 'event1', spy1 );
  304. listener.listenTo( emitter, 'event2', spy2 );
  305. emitter.fire( 'event1' );
  306. emitter.fire( 'event2' );
  307. listener.stopListening( emitter, 'event1', spy1 );
  308. emitter.fire( 'event1' );
  309. emitter.fire( 'event2' );
  310. sinon.assert.calledOnce( spy1 );
  311. sinon.assert.calledTwice( spy2 );
  312. } );
  313. it( 'should stop listening to given event', () => {
  314. const spy1a = sinon.spy();
  315. const spy1b = sinon.spy();
  316. const spy2 = sinon.spy();
  317. listener.listenTo( emitter, 'event1', spy1a );
  318. listener.listenTo( emitter, 'event1', spy1b );
  319. listener.listenTo( emitter, 'event2', spy2 );
  320. emitter.fire( 'event1' );
  321. emitter.fire( 'event2' );
  322. listener.stopListening( emitter, 'event1' );
  323. emitter.fire( 'event1' );
  324. emitter.fire( 'event2' );
  325. sinon.assert.calledOnce( spy1a );
  326. sinon.assert.calledOnce( spy1b );
  327. sinon.assert.calledTwice( spy2 );
  328. } );
  329. it( 'should stop listening to all events from given emitter', () => {
  330. const spy1 = sinon.spy();
  331. const spy2 = sinon.spy();
  332. listener.listenTo( emitter, 'event1', spy1 );
  333. listener.listenTo( emitter, 'event2', spy2 );
  334. emitter.fire( 'event1' );
  335. emitter.fire( 'event2' );
  336. listener.stopListening( emitter );
  337. emitter.fire( 'event1' );
  338. emitter.fire( 'event2' );
  339. sinon.assert.calledOnce( spy1 );
  340. sinon.assert.calledOnce( spy2 );
  341. } );
  342. it( 'should stop listening to everything', () => {
  343. const spy1 = sinon.spy();
  344. const spy2 = sinon.spy();
  345. const emitter1 = getEmitterInstance();
  346. const emitter2 = getEmitterInstance();
  347. listener.listenTo( emitter1, 'event1', spy1 );
  348. listener.listenTo( emitter2, 'event2', spy2 );
  349. emitter1.fire( 'event1' );
  350. emitter2.fire( 'event2' );
  351. listener.stopListening();
  352. emitter1.fire( 'event1' );
  353. emitter2.fire( 'event2' );
  354. sinon.assert.calledOnce( spy1 );
  355. sinon.assert.calledOnce( spy2 );
  356. } );
  357. it( 'should not stop other emitters when a non-listened emitter is provided', () => {
  358. const spy = sinon.spy();
  359. const emitter1 = getEmitterInstance();
  360. const emitter2 = getEmitterInstance();
  361. listener.listenTo( emitter1, 'test', spy );
  362. listener.stopListening( emitter2 );
  363. emitter1.fire( 'test' );
  364. sinon.assert.called( spy );
  365. } );
  366. it( 'should correctly stop listening to namespaced events', () => {
  367. const spyFoo = sinon.spy();
  368. const spyBar = sinon.spy();
  369. listener.listenTo( emitter, 'foo', spyFoo );
  370. listener.listenTo( emitter, 'foo:bar', spyBar );
  371. listener.stopListening( emitter, 'foo' );
  372. emitter.fire( 'foo:bar' );
  373. sinon.assert.notCalled( spyFoo );
  374. sinon.assert.calledOnce( spyBar );
  375. } );
  376. } );
  377. describe( 'delegate', () => {
  378. it( 'should chain for a single event', () => {
  379. const emitter = getEmitterInstance();
  380. expect( emitter.delegate( 'foo' ) ).to.contain.keys( 'to' );
  381. } );
  382. it( 'should chain for multiple events', () => {
  383. const emitter = getEmitterInstance();
  384. expect( emitter.delegate( 'foo', 'bar' ) ).to.contain.keys( 'to' );
  385. } );
  386. describe( 'to', () => {
  387. it( 'forwards an event to another emitter', done => {
  388. const emitterA = getEmitterInstance();
  389. const emitterB = getEmitterInstance();
  390. const dataA = {};
  391. const dataB = {};
  392. emitterB.delegate( 'foo' ).to( emitterA );
  393. emitterA.on( 'foo', ( ...args ) => {
  394. assertDelegated( args, {
  395. expectedSource: emitterB,
  396. expectedName: 'foo',
  397. expectedPath: [ emitterB, emitterA ],
  398. expectedData: [ dataA, dataB ]
  399. } );
  400. done();
  401. } );
  402. emitterB.fire( 'foo', dataA, dataB );
  403. } );
  404. it( 'forwards multiple events to another emitter', () => {
  405. const emitterA = getEmitterInstance();
  406. const emitterB = getEmitterInstance();
  407. const spyFoo = sinon.spy();
  408. const spyBar = sinon.spy();
  409. const spyBaz = sinon.spy();
  410. const dataA = {};
  411. const dataB = {};
  412. emitterB.delegate( 'foo', 'bar', 'baz' ).to( emitterA );
  413. emitterA.on( 'foo', spyFoo );
  414. emitterA.on( 'bar', spyBar );
  415. emitterA.on( 'baz', spyBaz );
  416. emitterB.fire( 'foo', dataA, dataB );
  417. sinon.assert.calledOnce( spyFoo );
  418. sinon.assert.notCalled( spyBar );
  419. sinon.assert.notCalled( spyBaz );
  420. assertDelegated( spyFoo.args[ 0 ], {
  421. expectedSource: emitterB,
  422. expectedName: 'foo',
  423. expectedPath: [ emitterB, emitterA ],
  424. expectedData: [ dataA, dataB ]
  425. } );
  426. emitterB.fire( 'bar' );
  427. sinon.assert.calledOnce( spyFoo );
  428. sinon.assert.calledOnce( spyBar );
  429. sinon.assert.notCalled( spyBaz );
  430. assertDelegated( spyBar.args[ 0 ], {
  431. expectedSource: emitterB,
  432. expectedName: 'bar',
  433. expectedPath: [ emitterB, emitterA ],
  434. expectedData: []
  435. } );
  436. emitterB.fire( 'baz' );
  437. sinon.assert.calledOnce( spyFoo );
  438. sinon.assert.calledOnce( spyBar );
  439. sinon.assert.calledOnce( spyBaz );
  440. assertDelegated( spyBaz.args[ 0 ], {
  441. expectedSource: emitterB,
  442. expectedName: 'baz',
  443. expectedPath: [ emitterB, emitterA ],
  444. expectedData: []
  445. } );
  446. emitterB.fire( 'not-delegated' );
  447. sinon.assert.calledOnce( spyFoo );
  448. sinon.assert.calledOnce( spyBar );
  449. sinon.assert.calledOnce( spyBaz );
  450. } );
  451. it( 'does not forward events which are not supposed to be delegated', () => {
  452. const emitterA = getEmitterInstance();
  453. const emitterB = getEmitterInstance();
  454. const spyFoo = sinon.spy();
  455. const spyBar = sinon.spy();
  456. const spyBaz = sinon.spy();
  457. emitterB.delegate( 'foo', 'bar', 'baz' ).to( emitterA );
  458. emitterA.on( 'foo', spyFoo );
  459. emitterA.on( 'bar', spyBar );
  460. emitterA.on( 'baz', spyBaz );
  461. emitterB.fire( 'foo' );
  462. emitterB.fire( 'bar' );
  463. emitterB.fire( 'baz' );
  464. emitterB.fire( 'not-delegated' );
  465. sinon.assert.callOrder( spyFoo, spyBar, spyBaz );
  466. sinon.assert.callCount( spyFoo, 1 );
  467. sinon.assert.callCount( spyBar, 1 );
  468. sinon.assert.callCount( spyBaz, 1 );
  469. } );
  470. it( 'supports deep chain event delegation', done => {
  471. const emitterA = getEmitterInstance();
  472. const emitterB = getEmitterInstance();
  473. const emitterC = getEmitterInstance();
  474. const data = {};
  475. emitterC.delegate( 'foo' ).to( emitterB );
  476. emitterB.delegate( 'foo' ).to( emitterA );
  477. emitterA.on( 'foo', ( ...args ) => {
  478. assertDelegated( args, {
  479. expectedSource: emitterC,
  480. expectedName: 'foo',
  481. expectedPath: [ emitterC, emitterB, emitterA ],
  482. expectedData: [ data ]
  483. } );
  484. done();
  485. } );
  486. emitterC.fire( 'foo', data );
  487. } );
  488. it( 'preserves path in event delegation', done => {
  489. const data = {};
  490. const emitterA = getEmitterInstance();
  491. const emitterB = getEmitterInstance();
  492. const emitterC = getEmitterInstance();
  493. const emitterD = getEmitterInstance();
  494. emitterB.delegate( 'foo' ).to( emitterA );
  495. emitterB.delegate( 'foo' ).to( emitterC );
  496. emitterB.delegate( 'foo' ).to( emitterD );
  497. emitterD.on( 'foo', ( ...args ) => {
  498. assertDelegated( args, {
  499. expectedSource: emitterB,
  500. expectedName: 'foo',
  501. expectedPath: [ emitterB, emitterD ],
  502. expectedData: [ data ]
  503. } );
  504. done();
  505. } );
  506. emitterB.fire( 'foo', data );
  507. emitterC.fire( 'foo', data );
  508. } );
  509. it( 'executes callbacks first, then delegates further', () => {
  510. const emitterA = getEmitterInstance();
  511. const emitterB = getEmitterInstance();
  512. const spyA = sinon.spy();
  513. const spyB = sinon.spy();
  514. emitterB.delegate( 'foo' ).to( emitterA );
  515. emitterA.on( 'foo', spyA );
  516. emitterB.on( 'foo', spyB );
  517. emitterB.fire( 'foo' );
  518. sinon.assert.callOrder( spyB, spyA );
  519. } );
  520. it( 'supports delegation under a different name', () => {
  521. const emitterA = getEmitterInstance();
  522. const emitterB = getEmitterInstance();
  523. const emitterC = getEmitterInstance();
  524. const emitterD = getEmitterInstance();
  525. const spyAFoo = sinon.spy();
  526. const spyABar = sinon.spy();
  527. const spyCBaz = sinon.spy();
  528. const spyDFoo = sinon.spy();
  529. emitterB.delegate( 'foo' ).to( emitterA, 'bar' );
  530. emitterB.delegate( 'foo' ).to( emitterC, name => name + '-baz' );
  531. emitterB.delegate( 'foo' ).to( emitterD );
  532. emitterA.on( 'foo', spyAFoo );
  533. emitterA.on( 'bar', spyABar );
  534. emitterC.on( 'foo-baz', spyCBaz );
  535. emitterD.on( 'foo', spyDFoo );
  536. emitterB.fire( 'foo' );
  537. sinon.assert.calledOnce( spyABar );
  538. sinon.assert.calledOnce( spyCBaz );
  539. sinon.assert.calledOnce( spyDFoo );
  540. sinon.assert.notCalled( spyAFoo );
  541. } );
  542. it( 'supports delegation under a different name with multiple events', () => {
  543. const emitterA = getEmitterInstance();
  544. const emitterB = getEmitterInstance();
  545. const spyAFoo = sinon.spy();
  546. const spyABar = sinon.spy();
  547. const spyABaz = sinon.spy();
  548. const spyAQux = sinon.spy();
  549. emitterB.delegate( 'foo', 'bar', 'baz' ).to( emitterA, 'qux' );
  550. emitterA.on( 'foo', spyAFoo );
  551. emitterA.on( 'bar', spyABar );
  552. emitterA.on( 'baz', spyABaz );
  553. emitterA.on( 'qux', spyAQux );
  554. emitterB.fire( 'foo' );
  555. emitterB.fire( 'baz' );
  556. emitterB.fire( 'bar' );
  557. sinon.assert.notCalled( spyAFoo );
  558. sinon.assert.notCalled( spyABar );
  559. sinon.assert.notCalled( spyABaz );
  560. sinon.assert.calledThrice( spyAQux );
  561. } );
  562. it( 'supports delegation with multiple events, each under a different name', () => {
  563. const emitterA = getEmitterInstance();
  564. const emitterB = getEmitterInstance();
  565. const spyAFoo = sinon.spy();
  566. const spyABar = sinon.spy();
  567. const spyABaz = sinon.spy();
  568. const spyAFooQux = sinon.spy();
  569. const spyABarQux = sinon.spy();
  570. const spyABazQux = sinon.spy();
  571. emitterB.delegate( 'foo', 'bar', 'baz' ).to( emitterA, name => name + '-qux' );
  572. emitterA.on( 'foo', spyAFoo );
  573. emitterA.on( 'bar', spyABar );
  574. emitterA.on( 'baz', spyABaz );
  575. emitterA.on( 'foo-qux', spyAFooQux );
  576. emitterA.on( 'bar-qux', spyABarQux );
  577. emitterA.on( 'baz-qux', spyABazQux );
  578. emitterB.fire( 'foo' );
  579. emitterB.fire( 'baz' );
  580. emitterB.fire( 'bar' );
  581. sinon.assert.notCalled( spyAFoo );
  582. sinon.assert.notCalled( spyABar );
  583. sinon.assert.notCalled( spyABaz );
  584. sinon.assert.calledOnce( spyAFooQux );
  585. sinon.assert.calledOnce( spyABarQux );
  586. sinon.assert.calledOnce( spyABazQux );
  587. sinon.assert.callOrder( spyAFooQux, spyABazQux, spyABarQux );
  588. } );
  589. it( 'preserves path in delegation under a different name', done => {
  590. const data = {};
  591. const emitterA = getEmitterInstance();
  592. const emitterB = getEmitterInstance();
  593. const emitterC = getEmitterInstance();
  594. const emitterD = getEmitterInstance();
  595. emitterB.delegate( 'foo' ).to( emitterA, 'bar' );
  596. emitterB.delegate( 'foo' ).to( emitterC, 'baz' );
  597. emitterB.delegate( 'foo' ).to( emitterD );
  598. emitterD.on( 'foo', ( ...args ) => {
  599. assertDelegated( args, {
  600. expectedSource: emitterB,
  601. expectedName: 'foo',
  602. expectedPath: [ emitterB, emitterD ],
  603. expectedData: [ data ]
  604. } );
  605. done();
  606. } );
  607. emitterB.fire( 'foo', data );
  608. } );
  609. it( 'supports delegation of all events', () => {
  610. const emitterA = getEmitterInstance();
  611. const emitterB = getEmitterInstance();
  612. const spyAFoo = sinon.spy();
  613. const spyABar = sinon.spy();
  614. const spyABaz = sinon.spy();
  615. emitterB.delegate( '*' ).to( emitterA );
  616. emitterA.on( 'foo', spyAFoo );
  617. emitterA.on( 'bar', spyABar );
  618. emitterA.on( 'baz', spyABaz );
  619. emitterB.fire( 'foo' );
  620. emitterB.fire( 'baz' );
  621. emitterB.fire( 'bar' );
  622. sinon.assert.callOrder( spyAFoo, spyABaz, spyABar );
  623. } );
  624. it( 'supports delegation of all events under different names', () => {
  625. const emitterA = getEmitterInstance();
  626. const emitterB = getEmitterInstance();
  627. const spyAFoo = sinon.spy();
  628. const spyABar = sinon.spy();
  629. const spyABaz = sinon.spy();
  630. const spyAFooDel = sinon.spy();
  631. const spyABarDel = sinon.spy();
  632. const spyABazDel = sinon.spy();
  633. emitterB.delegate( '*' ).to( emitterA, name => name + '-delegated' );
  634. emitterA.on( 'foo', spyAFoo );
  635. emitterA.on( 'bar', spyABar );
  636. emitterA.on( 'baz', spyABaz );
  637. emitterA.on( 'foo-delegated', spyAFooDel );
  638. emitterA.on( 'bar-delegated', spyABarDel );
  639. emitterA.on( 'baz-delegated', spyABazDel );
  640. emitterB.fire( 'foo' );
  641. emitterB.fire( 'baz' );
  642. emitterB.fire( 'bar' );
  643. sinon.assert.notCalled( spyAFoo );
  644. sinon.assert.notCalled( spyABar );
  645. sinon.assert.notCalled( spyABaz );
  646. sinon.assert.callOrder( spyAFooDel, spyABazDel, spyABarDel );
  647. } );
  648. } );
  649. } );
  650. describe( 'stopDelegating', () => {
  651. it( 'passes if no delegation was set', () => {
  652. expect( () => {
  653. getEmitterInstance().stopDelegating();
  654. } ).to.not.throw();
  655. } );
  656. it( 'stops delegating all events to all emitters', () => {
  657. const emitterA = getEmitterInstance();
  658. const emitterB = getEmitterInstance();
  659. const emitterC = getEmitterInstance();
  660. const spyFoo = sinon.spy();
  661. const spyBar = sinon.spy();
  662. emitterA.delegate( 'foo' ).to( emitterB );
  663. emitterA.delegate( 'bar' ).to( emitterC );
  664. emitterB.on( 'foo', spyFoo );
  665. emitterC.on( 'bar', spyBar );
  666. emitterA.fire( 'foo' );
  667. emitterA.fire( 'bar' );
  668. sinon.assert.callOrder( spyFoo, spyBar );
  669. emitterA.stopDelegating();
  670. emitterA.fire( 'foo' );
  671. emitterA.fire( 'bar' );
  672. sinon.assert.callOrder( spyFoo, spyBar );
  673. } );
  674. it( 'stops delegating a specific event to all emitters', () => {
  675. const emitterA = getEmitterInstance();
  676. const emitterB = getEmitterInstance();
  677. const emitterC = getEmitterInstance();
  678. const spyFooB = sinon.spy();
  679. const spyFooC = sinon.spy();
  680. const spyBarC = sinon.spy();
  681. emitterA.delegate( 'foo' ).to( emitterB );
  682. emitterA.delegate( 'foo' ).to( emitterC );
  683. emitterA.delegate( 'bar' ).to( emitterC );
  684. emitterB.on( 'foo', spyFooB );
  685. emitterC.on( 'foo', spyFooC );
  686. emitterC.on( 'bar', spyBarC );
  687. emitterA.fire( 'foo' );
  688. emitterA.fire( 'bar' );
  689. sinon.assert.callOrder( spyFooB, spyFooC, spyBarC );
  690. emitterA.stopDelegating( 'foo' );
  691. emitterA.fire( 'foo' );
  692. emitterA.fire( 'bar' );
  693. sinon.assert.callOrder( spyFooB, spyFooC, spyBarC, spyBarC );
  694. } );
  695. it( 'stops delegating a specific event to a specific emitter', () => {
  696. const emitterA = getEmitterInstance();
  697. const emitterB = getEmitterInstance();
  698. const emitterC = getEmitterInstance();
  699. const spyFooB = sinon.spy();
  700. const spyFooC = sinon.spy();
  701. emitterA.delegate( 'foo' ).to( emitterB );
  702. emitterA.delegate( 'foo' ).to( emitterC );
  703. emitterB.on( 'foo', spyFooB );
  704. emitterC.on( 'foo', spyFooC );
  705. emitterA.fire( 'foo' );
  706. sinon.assert.callOrder( spyFooB, spyFooC );
  707. emitterA.stopDelegating( 'foo', emitterC );
  708. emitterA.fire( 'foo' );
  709. sinon.assert.callOrder( spyFooB, spyFooC, spyFooB );
  710. } );
  711. it( 'stops delegating a specific event under a different name to a specific emitter', () => {
  712. const emitterA = getEmitterInstance();
  713. const emitterB = getEmitterInstance();
  714. const emitterC = getEmitterInstance();
  715. const spyFooB = sinon.spy();
  716. const spyFooC = sinon.spy();
  717. emitterA.delegate( 'foo' ).to( emitterB );
  718. emitterA.delegate( 'foo' ).to( emitterC, 'bar' );
  719. emitterB.on( 'foo', spyFooB );
  720. emitterC.on( 'bar', spyFooC );
  721. emitterA.fire( 'foo' );
  722. sinon.assert.callOrder( spyFooB, spyFooC );
  723. emitterA.stopDelegating( 'foo', emitterC );
  724. emitterA.fire( 'foo' );
  725. sinon.assert.callOrder( spyFooB, spyFooC, spyFooB );
  726. } );
  727. it( 'stops delegating all ("*")', () => {
  728. const emitterA = getEmitterInstance();
  729. const emitterB = getEmitterInstance();
  730. const emitterC = getEmitterInstance();
  731. const spyAFoo = sinon.spy();
  732. const spyABar = sinon.spy();
  733. const spyCFoo = sinon.spy();
  734. const spyCBar = sinon.spy();
  735. emitterB.delegate( '*' ).to( emitterA );
  736. emitterB.delegate( '*' ).to( emitterC );
  737. emitterA.on( 'foo', spyAFoo );
  738. emitterA.on( 'bar', spyABar );
  739. emitterC.on( 'foo', spyCFoo );
  740. emitterC.on( 'bar', spyCBar );
  741. emitterB.fire( 'foo' );
  742. emitterB.fire( 'bar' );
  743. sinon.assert.calledOnce( spyAFoo );
  744. sinon.assert.calledOnce( spyABar );
  745. sinon.assert.calledOnce( spyCFoo );
  746. sinon.assert.calledOnce( spyCBar );
  747. emitterB.stopDelegating( '*' );
  748. emitterB.fire( 'foo' );
  749. emitterB.fire( 'bar' );
  750. sinon.assert.calledOnce( spyAFoo );
  751. sinon.assert.calledOnce( spyABar );
  752. sinon.assert.calledOnce( spyCFoo );
  753. sinon.assert.calledOnce( spyCBar );
  754. } );
  755. it( 'stops delegating all ("*") to a specific emitter', () => {
  756. const emitterA = getEmitterInstance();
  757. const emitterB = getEmitterInstance();
  758. const emitterC = getEmitterInstance();
  759. const spyAFoo = sinon.spy();
  760. const spyABar = sinon.spy();
  761. const spyCFoo = sinon.spy();
  762. const spyCBar = sinon.spy();
  763. emitterB.delegate( '*' ).to( emitterA );
  764. emitterB.delegate( 'foo' ).to( emitterC );
  765. emitterA.on( 'foo', spyAFoo );
  766. emitterA.on( 'bar', spyABar );
  767. emitterC.on( 'foo', spyCFoo );
  768. emitterC.on( 'bar', spyCBar );
  769. emitterB.fire( 'foo' );
  770. emitterB.fire( 'bar' );
  771. sinon.assert.calledOnce( spyAFoo );
  772. sinon.assert.calledOnce( spyABar );
  773. sinon.assert.calledOnce( spyCFoo );
  774. sinon.assert.notCalled( spyCBar );
  775. emitterB.stopDelegating( '*', emitterA );
  776. emitterB.fire( 'foo' );
  777. emitterB.fire( 'bar' );
  778. sinon.assert.calledOnce( spyAFoo );
  779. sinon.assert.calledOnce( spyABar );
  780. sinon.assert.calledTwice( spyCFoo );
  781. sinon.assert.notCalled( spyCBar );
  782. } );
  783. it( 'passes when stopping delegation of a specific event which has never been delegated', () => {
  784. const emitterA = getEmitterInstance();
  785. const emitterB = getEmitterInstance();
  786. expect( () => {
  787. emitterA.stopDelegating( 'bar' );
  788. emitterA.stopDelegating( 'bar', emitterB );
  789. } ).to.not.throw();
  790. } );
  791. it( 'passes when stopping delegation of a specific event to an emitter which wasn\'t a destination', () => {
  792. const emitterA = getEmitterInstance();
  793. const emitterB = getEmitterInstance();
  794. const emitterC = getEmitterInstance();
  795. emitterA.delegate( 'foo' ).to( emitterB );
  796. expect( () => {
  797. emitterA.stopDelegating( 'foo', emitterC );
  798. } ).to.not.throw();
  799. } );
  800. it( 'passes when stopping delegation of a specific event to a specific emitter which has never been delegated', () => {
  801. const emitterA = getEmitterInstance();
  802. const emitterB = getEmitterInstance();
  803. const emitterC = getEmitterInstance();
  804. emitterA.delegate( 'foo' ).to( emitterB );
  805. expect( () => {
  806. emitterA.stopDelegating( 'bar', emitterC );
  807. } ).to.not.throw();
  808. } );
  809. } );
  810. function assertDelegated( evtArgs, { expectedName, expectedSource, expectedPath, expectedData } ) {
  811. const evtInfo = evtArgs[ 0 ];
  812. expect( evtInfo.name ).to.equal( expectedName );
  813. expect( evtInfo.source ).to.equal( expectedSource );
  814. expect( evtInfo.path ).to.deep.equal( expectedPath );
  815. expect( evtArgs.slice( 1 ) ).to.deep.equal( expectedData );
  816. }
  817. } );
  818. describe( 'emitter id', () => {
  819. let emitter;
  820. beforeEach( () => {
  821. emitter = getEmitterInstance();
  822. } );
  823. it( 'should be undefined before it is set', () => {
  824. expect( _getEmitterId( emitter ) ).to.be.undefined;
  825. } );
  826. it( 'should be settable but only once', () => {
  827. _setEmitterId( emitter, 'abc' );
  828. expect( _getEmitterId( emitter ) ).to.equal( 'abc' );
  829. _setEmitterId( emitter, 'xyz' );
  830. expect( _getEmitterId( emitter ) ).to.equal( 'abc' );
  831. } );
  832. } );
  833. describe( '_getEmitterListenedTo', () => {
  834. let emitter, listener;
  835. beforeEach( () => {
  836. emitter = getEmitterInstance();
  837. listener = getEmitterInstance();
  838. } );
  839. it( 'should return null if listener do not listen to emitter with given id', () => {
  840. expect( _getEmitterListenedTo( listener, 'abc' ) ).to.be.null;
  841. } );
  842. it( 'should return emitter with given id', () => {
  843. listener.listenTo( emitter, 'eventName', () => {} );
  844. const emitterId = _getEmitterId( emitter );
  845. expect( _getEmitterListenedTo( listener, emitterId ) ).to.equal( emitter );
  846. } );
  847. } );
  848. function getEmitterInstance() {
  849. return Object.create( EmitterMixin );
  850. }