8
0

viewcollection.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. /* bender-tags: ui */
  7. import CKEditorError from 'ckeditor5/utils/ckeditorerror.js';
  8. import Collection from 'ckeditor5/utils/collection.js';
  9. import testUtils from 'tests/core/_utils/utils.js';
  10. import View from 'ckeditor5/ui/view.js';
  11. import ViewCollection from 'ckeditor5/ui/viewcollection.js';
  12. import Template from 'ckeditor5/ui/template.js';
  13. import normalizeHtml from 'tests/utils/_utils/normalizehtml.js';
  14. let collection;
  15. testUtils.createSinonSandbox();
  16. describe( 'ViewCollection', () => {
  17. beforeEach( createTestCollection );
  18. describe( 'constructor()', () => {
  19. it( 'sets basic properties and attributes', () => {
  20. expect( collection.locale ).to.be.undefined;
  21. expect( collection.ready ).to.be.false;
  22. expect( collection._parentElement ).to.be.null;
  23. expect( collection._boundItemsToViewsMap ).to.be.instanceOf( Map );
  24. } );
  25. it( 'accepts locale and defines the locale property', () => {
  26. const locale = { t() {} };
  27. expect( new ViewCollection( locale ).locale ).to.equal( locale );
  28. } );
  29. it( 'manages view#element of the children in DOM', () => {
  30. const parentElement = document.createElement( 'p' );
  31. collection.setParent( parentElement );
  32. const viewA = new View();
  33. expect( () => {
  34. collection.add( viewA );
  35. collection.remove( viewA );
  36. } ).to.not.throw();
  37. expect( () => {
  38. collection.ready = true;
  39. collection.add( viewA );
  40. collection.remove( viewA );
  41. } ).to.not.throw();
  42. viewA.element = document.createElement( 'b' );
  43. collection.add( viewA );
  44. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<p><b></b></p>' );
  45. const viewB = new View();
  46. viewB.element = document.createElement( 'i' );
  47. collection.add( viewB, 0 );
  48. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<p><i></i><b></b></p>' );
  49. collection.remove( viewA );
  50. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<p><i></i></p>' );
  51. collection.remove( viewB );
  52. expect( normalizeHtml( parentElement.outerHTML ) ).to.equal( '<p></p>' );
  53. } );
  54. } );
  55. describe( 'init()', () => {
  56. it( 'should return a promise', () => {
  57. expect( collection.init() ).to.be.instanceof( Promise );
  58. } );
  59. it( 'should throw if already initialized', () => {
  60. return collection.init()
  61. .then( () => {
  62. collection.init();
  63. throw new Error( 'This should not be executed.' );
  64. } )
  65. .catch( err => {
  66. expect( err ).to.be.instanceof( CKEditorError );
  67. expect( err.message ).to.match( /ui-viewcollection-init-reinit/ );
  68. } );
  69. } );
  70. it( 'calls #init on all views in the collection', () => {
  71. const viewA = new View();
  72. const viewB = new View();
  73. viewA.element = document.createElement( 'a' );
  74. viewB.element = document.createElement( 'b' );
  75. const spyA = testUtils.sinon.spy( viewA, 'init' );
  76. const spyB = testUtils.sinon.spy( viewB, 'init' );
  77. collection.setParent( document.body );
  78. collection.add( viewA );
  79. collection.add( viewB );
  80. return collection.init().then( () => {
  81. sinon.assert.calledOnce( spyA );
  82. sinon.assert.calledOnce( spyB );
  83. sinon.assert.callOrder( spyA, spyB );
  84. expect( viewA.element.parentNode ).to.equal( collection._parentElement );
  85. expect( viewA.element.nextSibling ).to.equal( viewB.element );
  86. expect( collection.ready ).to.be.true;
  87. } );
  88. } );
  89. } );
  90. describe( 'destroy()', () => {
  91. it( 'should return a promise', () => {
  92. expect( collection.destroy() ).to.be.instanceof( Promise );
  93. } );
  94. it( 'calls #destroy on all views in the collection', () => {
  95. const viewA = new View();
  96. const viewB = new View();
  97. const spyA = testUtils.sinon.spy( viewA, 'destroy' );
  98. const spyB = testUtils.sinon.spy( viewB, 'destroy' );
  99. collection.add( viewA );
  100. collection.add( viewB );
  101. return collection.destroy().then( () => {
  102. sinon.assert.calledOnce( spyA );
  103. sinon.assert.calledOnce( spyB );
  104. sinon.assert.callOrder( spyA, spyB );
  105. } );
  106. } );
  107. } );
  108. describe( 'add', () => {
  109. it( 'returns a promise', () => {
  110. expect( collection.add( {} ) ).to.be.instanceof( Promise );
  111. } );
  112. it( 'initializes the new view in the collection', () => {
  113. let view = new View();
  114. let spy = testUtils.sinon.spy( view, 'init' );
  115. expect( collection.ready ).to.be.false;
  116. expect( view.ready ).to.be.false;
  117. return collection.add( view ).then( () => {
  118. expect( collection.ready ).to.be.false;
  119. expect( view.ready ).to.be.false;
  120. sinon.assert.notCalled( spy );
  121. view = new View();
  122. spy = testUtils.sinon.spy( view, 'init' );
  123. collection.ready = true;
  124. return collection.add( view ).then( () => {
  125. expect( view.ready ).to.be.true;
  126. sinon.assert.calledOnce( spy );
  127. } );
  128. } );
  129. } );
  130. } );
  131. describe( 'setParent', () => {
  132. it( 'sets #_parentElement', () => {
  133. const el = {};
  134. expect( collection._parentElement ).to.be.null;
  135. collection.setParent( el );
  136. expect( collection._parentElement ).to.equal( el );
  137. } );
  138. } );
  139. describe( 'bindTo', () => {
  140. class ViewClass extends View {
  141. constructor( locale, data ) {
  142. super( locale );
  143. this.template = new Template( {
  144. tag: 'b'
  145. } );
  146. this.data = data;
  147. }
  148. }
  149. it( 'provides "as" interface', () => {
  150. const returned = collection.bindTo( {} );
  151. expect( returned ).to.have.keys( 'as' );
  152. expect( returned.as ).to.be.a( 'function' );
  153. } );
  154. describe( 'as', () => {
  155. it( 'does not chain', () => {
  156. const returned = collection.bindTo( new Collection() ).as( ViewClass );
  157. expect( returned ).to.be.undefined;
  158. } );
  159. it( 'binds collection as a view factory – initial content', () => {
  160. const locale = {};
  161. const items = new Collection();
  162. items.add( { id: '1' } );
  163. items.add( { id: '2' } );
  164. collection = new ViewCollection( locale );
  165. collection.bindTo( items ).as( ViewClass );
  166. expect( collection ).to.have.length( 2 );
  167. expect( collection.get( 0 ) ).to.be.instanceOf( ViewClass );
  168. expect( collection.get( 1 ) ).to.be.instanceOf( ViewClass );
  169. expect( collection.get( 0 ).locale ).to.equal( locale );
  170. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  171. } );
  172. it( 'binds collection as a view factory – new content', () => {
  173. const locale = {};
  174. const items = new Collection();
  175. collection = new ViewCollection( locale );
  176. collection.bindTo( items ).as( ViewClass );
  177. expect( collection ).to.have.length( 0 );
  178. items.add( { id: '1' } );
  179. items.add( { id: '2' } );
  180. expect( collection ).to.have.length( 2 );
  181. expect( collection.get( 0 ) ).to.be.instanceOf( ViewClass );
  182. expect( collection.get( 1 ) ).to.be.instanceOf( ViewClass );
  183. expect( collection.get( 0 ).locale ).to.equal( locale );
  184. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  185. } );
  186. it( 'binds collection as a view factory – item removal', () => {
  187. const locale = {};
  188. const items = new Collection();
  189. collection = new ViewCollection( locale );
  190. collection.bindTo( items ).as( ViewClass );
  191. expect( collection ).to.have.length( 0 );
  192. items.add( { id: '1' } );
  193. items.add( { id: '2' } );
  194. expect( collection ).to.have.length( 2 );
  195. expect( collection.get( 0 ) ).to.be.instanceOf( ViewClass );
  196. expect( collection.get( 1 ) ).to.be.instanceOf( ViewClass );
  197. expect( collection.get( 0 ).locale ).to.equal( locale );
  198. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  199. items.remove( 1 );
  200. expect( collection.get( 0 ).data ).to.equal( items.get( 0 ) );
  201. items.remove( 0 );
  202. expect( collection ).to.have.length( 0 );
  203. } );
  204. it( 'binds collection as a view factory – custom factory', () => {
  205. const locale = {};
  206. const items = new Collection();
  207. collection = new ViewCollection( locale );
  208. collection.bindTo( items ).as( ( item ) => {
  209. return new ViewClass( locale, item );
  210. } );
  211. expect( collection ).to.have.length( 0 );
  212. items.add( { id: '1' } );
  213. items.add( { id: '2' } );
  214. expect( collection ).to.have.length( 2 );
  215. expect( collection.get( 0 ) ).to.be.instanceOf( ViewClass );
  216. expect( collection.get( 1 ) ).to.be.instanceOf( ViewClass );
  217. expect( collection.get( 0 ).locale ).to.equal( locale );
  218. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  219. } );
  220. } );
  221. } );
  222. describe( 'delegate', () => {
  223. it( 'should throw when event names are not strings', () => {
  224. expect( () => {
  225. collection.delegate();
  226. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  227. expect( () => {
  228. collection.delegate( new Date() );
  229. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  230. expect( () => {
  231. collection.delegate( 'color', new Date() );
  232. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  233. } );
  234. it( 'returns object', () => {
  235. expect( collection.delegate( 'foo' ) ).to.be.an( 'object' );
  236. } );
  237. it( 'provides "to" interface', () => {
  238. const delegate = collection.delegate( 'foo' );
  239. expect( delegate ).to.have.keys( 'to' );
  240. expect( delegate.to ).to.be.a( 'function' );
  241. } );
  242. describe( 'to', () => {
  243. it( 'does not chain', () => {
  244. const returned = collection.delegate( 'foo' ).to( {} );
  245. expect( returned ).to.be.undefined;
  246. } );
  247. it( 'forwards an event to another observable – existing view', ( done ) => {
  248. const target = new View();
  249. const view = new View();
  250. collection.add( view );
  251. collection.delegate( 'foo' ).to( target );
  252. target.on( 'foo', ( ...args ) => {
  253. assertDelegated( args, {
  254. expectedName: 'foo',
  255. expectedSource: view,
  256. expectedPath: [ view, target ],
  257. expectedData: []
  258. } );
  259. done();
  260. } );
  261. view.fire( 'foo' );
  262. } );
  263. it( 'forwards an event to another observable – new view', ( done ) => {
  264. const target = new View();
  265. const view = new View();
  266. collection.delegate( 'foo' ).to( target );
  267. collection.add( view );
  268. target.on( 'foo', ( ...args ) => {
  269. assertDelegated( args, {
  270. expectedName: 'foo',
  271. expectedSource: view,
  272. expectedPath: [ view, target ],
  273. expectedData: []
  274. } );
  275. done();
  276. } );
  277. view.fire( 'foo' );
  278. } );
  279. it( 'forwards multiple events to another observable', () => {
  280. const target = new View();
  281. const viewA = new View();
  282. const viewB = new View();
  283. const viewC = new View();
  284. const spyFoo = sinon.spy();
  285. const spyBar = sinon.spy();
  286. const spyBaz = sinon.spy();
  287. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  288. collection.add( viewA );
  289. collection.add( viewB );
  290. collection.add( viewC );
  291. target.on( 'foo', spyFoo );
  292. target.on( 'bar', spyBar );
  293. target.on( 'baz', spyBaz );
  294. viewA.fire( 'foo' );
  295. sinon.assert.calledOnce( spyFoo );
  296. sinon.assert.notCalled( spyBar );
  297. sinon.assert.notCalled( spyBaz );
  298. assertDelegated( spyFoo.args[ 0 ], {
  299. expectedName: 'foo',
  300. expectedSource: viewA,
  301. expectedPath: [ viewA, target ],
  302. expectedData: []
  303. } );
  304. viewB.fire( 'bar' );
  305. sinon.assert.calledOnce( spyFoo );
  306. sinon.assert.calledOnce( spyBar );
  307. sinon.assert.notCalled( spyBaz );
  308. assertDelegated( spyBar.args[ 0 ], {
  309. expectedName: 'bar',
  310. expectedSource: viewB,
  311. expectedPath: [ viewB, target ],
  312. expectedData: []
  313. } );
  314. viewC.fire( 'baz' );
  315. sinon.assert.calledOnce( spyFoo );
  316. sinon.assert.calledOnce( spyBar );
  317. sinon.assert.calledOnce( spyBaz );
  318. assertDelegated( spyBaz.args[ 0 ], {
  319. expectedName: 'baz',
  320. expectedSource: viewC,
  321. expectedPath: [ viewC, target ],
  322. expectedData: []
  323. } );
  324. viewC.fire( 'not-delegated' );
  325. sinon.assert.calledOnce( spyFoo );
  326. sinon.assert.calledOnce( spyBar );
  327. sinon.assert.calledOnce( spyBaz );
  328. } );
  329. it( 'does not forward events which are not supposed to be delegated', () => {
  330. const target = new View();
  331. const view = new View();
  332. const spyFoo = sinon.spy();
  333. const spyBar = sinon.spy();
  334. const spyBaz = sinon.spy();
  335. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  336. collection.add( view );
  337. target.on( 'foo', spyFoo );
  338. target.on( 'bar', spyBar );
  339. target.on( 'baz', spyBaz );
  340. view.fire( 'foo' );
  341. view.fire( 'bar' );
  342. view.fire( 'baz' );
  343. view.fire( 'not-delegated' );
  344. sinon.assert.callOrder( spyFoo, spyBar, spyBaz );
  345. sinon.assert.callCount( spyFoo, 1 );
  346. sinon.assert.callCount( spyBar, 1 );
  347. sinon.assert.callCount( spyBaz, 1 );
  348. } );
  349. it( 'stops forwarding when view removed from the collection', () => {
  350. const target = new View();
  351. const view = new View();
  352. const spy = sinon.spy();
  353. collection.delegate( 'foo' ).to( target );
  354. target.on( 'foo', spy );
  355. collection.add( view );
  356. view.fire( 'foo' );
  357. sinon.assert.callCount( spy, 1 );
  358. collection.remove( 0 );
  359. view.fire( 'foo' );
  360. sinon.assert.callCount( spy, 1 );
  361. } );
  362. it( 'supports deep event delegation', ( done ) => {
  363. const target = new View();
  364. const viewA = new View();
  365. const viewAA = new View();
  366. const data = {};
  367. const deepCollection = viewA.createCollection();
  368. collection.add( viewA );
  369. collection.delegate( 'foo' ).to( target );
  370. deepCollection.add( viewAA );
  371. deepCollection.delegate( 'foo' ).to( viewA );
  372. target.on( 'foo', ( ...args ) => {
  373. assertDelegated( args, {
  374. expectedName: 'foo',
  375. expectedSource: viewAA,
  376. expectedPath: [ viewAA, viewA, target ],
  377. expectedData: [ data ]
  378. } );
  379. done();
  380. } );
  381. viewAA.fire( 'foo', data );
  382. } );
  383. } );
  384. } );
  385. } );
  386. function createTestCollection() {
  387. collection = new ViewCollection();
  388. }
  389. function assertDelegated( evtArgs, { expectedName, expectedSource, expectedPath, expectedData } ) {
  390. const evtInfo = evtArgs[ 0 ];
  391. expect( evtInfo.name ).to.equal( expectedName );
  392. expect( evtInfo.source ).to.equal( expectedSource );
  393. expect( evtInfo.path ).to.deep.equal( expectedPath );
  394. expect( evtArgs.slice( 1 ) ).to.deep.equal( expectedData );
  395. }