emittermixin.js 29 KB

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