8
0

viewcollection.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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( 'add', () => {
  56. it( 'returns a promise', () => {
  57. expect( collection.add( {} ) ).to.be.instanceof( Promise );
  58. } );
  59. it( 'initializes the new view in the collection', () => {
  60. let view = new View();
  61. let spy = testUtils.sinon.spy( view, 'init' );
  62. expect( collection.ready ).to.be.false;
  63. expect( view.ready ).to.be.false;
  64. return collection.add( view ).then( () => {
  65. expect( collection.ready ).to.be.false;
  66. expect( view.ready ).to.be.false;
  67. sinon.assert.notCalled( spy );
  68. view = new View();
  69. spy = testUtils.sinon.spy( view, 'init' );
  70. collection.ready = true;
  71. return collection.add( view ).then( () => {
  72. expect( view.ready ).to.be.true;
  73. sinon.assert.calledOnce( spy );
  74. } );
  75. } );
  76. } );
  77. } );
  78. describe( 'setParent', () => {
  79. it( 'sets #_parentElement', () => {
  80. const el = {};
  81. expect( collection._parentElement ).to.be.null;
  82. collection.setParent( el );
  83. expect( collection._parentElement ).to.equal( el );
  84. } );
  85. } );
  86. describe( 'bindTo', () => {
  87. class ViewClass extends View {
  88. constructor( locale, data ) {
  89. super( locale );
  90. this.template = new Template( {
  91. tag: 'b'
  92. } );
  93. this.data = data;
  94. }
  95. }
  96. it( 'provides "as" interface', () => {
  97. const returned = collection.bindTo( {} );
  98. expect( returned ).to.have.keys( 'as' );
  99. expect( returned.as ).to.be.a( 'function' );
  100. } );
  101. describe( 'as', () => {
  102. it( 'does not chain', () => {
  103. const returned = collection.bindTo( new Collection() ).as( ViewClass );
  104. expect( returned ).to.be.undefined;
  105. } );
  106. it( 'binds collection as a view factory – initial content', () => {
  107. const locale = {};
  108. const items = new Collection();
  109. items.add( { id: '1' } );
  110. items.add( { id: '2' } );
  111. collection = new ViewCollection( locale );
  112. collection.bindTo( items ).as( ViewClass );
  113. expect( collection ).to.have.length( 2 );
  114. expect( collection.get( 0 ) ).to.be.instanceOf( ViewClass );
  115. expect( collection.get( 1 ) ).to.be.instanceOf( ViewClass );
  116. expect( collection.get( 0 ).locale ).to.equal( locale );
  117. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  118. } );
  119. it( 'binds collection as a view factory – new content', () => {
  120. const locale = {};
  121. const items = new Collection();
  122. collection = new ViewCollection( locale );
  123. collection.bindTo( items ).as( ViewClass );
  124. expect( collection ).to.have.length( 0 );
  125. items.add( { id: '1' } );
  126. items.add( { id: '2' } );
  127. expect( collection ).to.have.length( 2 );
  128. expect( collection.get( 0 ) ).to.be.instanceOf( ViewClass );
  129. expect( collection.get( 1 ) ).to.be.instanceOf( ViewClass );
  130. expect( collection.get( 0 ).locale ).to.equal( locale );
  131. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  132. } );
  133. it( 'binds collection as a view factory – item removal', () => {
  134. const locale = {};
  135. const items = new Collection();
  136. collection = new ViewCollection( locale );
  137. collection.bindTo( items ).as( ViewClass );
  138. expect( collection ).to.have.length( 0 );
  139. items.add( { id: '1' } );
  140. items.add( { id: '2' } );
  141. expect( collection ).to.have.length( 2 );
  142. expect( collection.get( 0 ) ).to.be.instanceOf( ViewClass );
  143. expect( collection.get( 1 ) ).to.be.instanceOf( ViewClass );
  144. expect( collection.get( 0 ).locale ).to.equal( locale );
  145. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  146. items.remove( 1 );
  147. expect( collection.get( 0 ).data ).to.equal( items.get( 0 ) );
  148. items.remove( 0 );
  149. expect( collection ).to.have.length( 0 );
  150. } );
  151. it( 'binds collection as a view factory – custom factory', () => {
  152. const locale = {};
  153. const items = new Collection();
  154. collection = new ViewCollection( locale );
  155. collection.bindTo( items ).as( ( item ) => {
  156. return new ViewClass( locale, item );
  157. } );
  158. expect( collection ).to.have.length( 0 );
  159. items.add( { id: '1' } );
  160. items.add( { id: '2' } );
  161. expect( collection ).to.have.length( 2 );
  162. expect( collection.get( 0 ) ).to.be.instanceOf( ViewClass );
  163. expect( collection.get( 1 ) ).to.be.instanceOf( ViewClass );
  164. expect( collection.get( 0 ).locale ).to.equal( locale );
  165. expect( collection.get( 1 ).data ).to.equal( items.get( 1 ) );
  166. } );
  167. } );
  168. } );
  169. describe( 'delegate', () => {
  170. it( 'should throw when event names are not strings', () => {
  171. expect( () => {
  172. collection.delegate();
  173. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  174. expect( () => {
  175. collection.delegate( new Date() );
  176. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  177. expect( () => {
  178. collection.delegate( 'color', new Date() );
  179. } ).to.throw( CKEditorError, /ui-viewcollection-delegate-wrong-events/ );
  180. } );
  181. it( 'returns object', () => {
  182. expect( collection.delegate( 'foo' ) ).to.be.an( 'object' );
  183. } );
  184. it( 'provides "to" interface', () => {
  185. const delegate = collection.delegate( 'foo' );
  186. expect( delegate ).to.have.keys( 'to' );
  187. expect( delegate.to ).to.be.a( 'function' );
  188. } );
  189. describe( 'to', () => {
  190. it( 'does not chain', () => {
  191. const returned = collection.delegate( 'foo' ).to( {} );
  192. expect( returned ).to.be.undefined;
  193. } );
  194. it( 'forwards an event to another observable – existing view', ( done ) => {
  195. const target = new View();
  196. const view = new View();
  197. collection.add( view );
  198. collection.delegate( 'foo' ).to( target );
  199. target.on( 'foo', ( ...args ) => {
  200. assertDelegated( args, {
  201. expectedName: 'foo',
  202. expectedSource: view,
  203. expectedPath: [ view, target ],
  204. expectedData: []
  205. } );
  206. done();
  207. } );
  208. view.fire( 'foo' );
  209. } );
  210. it( 'forwards an event to another observable – new view', ( done ) => {
  211. const target = new View();
  212. const view = new View();
  213. collection.delegate( 'foo' ).to( target );
  214. collection.add( view );
  215. target.on( 'foo', ( ...args ) => {
  216. assertDelegated( args, {
  217. expectedName: 'foo',
  218. expectedSource: view,
  219. expectedPath: [ view, target ],
  220. expectedData: []
  221. } );
  222. done();
  223. } );
  224. view.fire( 'foo' );
  225. } );
  226. it( 'forwards multiple events to another observable', () => {
  227. const target = new View();
  228. const viewA = new View();
  229. const viewB = new View();
  230. const viewC = new View();
  231. const spyFoo = sinon.spy();
  232. const spyBar = sinon.spy();
  233. const spyBaz = sinon.spy();
  234. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  235. collection.add( viewA );
  236. collection.add( viewB );
  237. collection.add( viewC );
  238. target.on( 'foo', spyFoo );
  239. target.on( 'bar', spyBar );
  240. target.on( 'baz', spyBaz );
  241. viewA.fire( 'foo' );
  242. sinon.assert.calledOnce( spyFoo );
  243. sinon.assert.notCalled( spyBar );
  244. sinon.assert.notCalled( spyBaz );
  245. assertDelegated( spyFoo.args[ 0 ], {
  246. expectedName: 'foo',
  247. expectedSource: viewA,
  248. expectedPath: [ viewA, target ],
  249. expectedData: []
  250. } );
  251. viewB.fire( 'bar' );
  252. sinon.assert.calledOnce( spyFoo );
  253. sinon.assert.calledOnce( spyBar );
  254. sinon.assert.notCalled( spyBaz );
  255. assertDelegated( spyBar.args[ 0 ], {
  256. expectedName: 'bar',
  257. expectedSource: viewB,
  258. expectedPath: [ viewB, target ],
  259. expectedData: []
  260. } );
  261. viewC.fire( 'baz' );
  262. sinon.assert.calledOnce( spyFoo );
  263. sinon.assert.calledOnce( spyBar );
  264. sinon.assert.calledOnce( spyBaz );
  265. assertDelegated( spyBaz.args[ 0 ], {
  266. expectedName: 'baz',
  267. expectedSource: viewC,
  268. expectedPath: [ viewC, target ],
  269. expectedData: []
  270. } );
  271. viewC.fire( 'not-delegated' );
  272. sinon.assert.calledOnce( spyFoo );
  273. sinon.assert.calledOnce( spyBar );
  274. sinon.assert.calledOnce( spyBaz );
  275. } );
  276. it( 'does not forward events which are not supposed to be delegated', () => {
  277. const target = new View();
  278. const view = new View();
  279. const spyFoo = sinon.spy();
  280. const spyBar = sinon.spy();
  281. const spyBaz = sinon.spy();
  282. collection.delegate( 'foo', 'bar', 'baz' ).to( target );
  283. collection.add( view );
  284. target.on( 'foo', spyFoo );
  285. target.on( 'bar', spyBar );
  286. target.on( 'baz', spyBaz );
  287. view.fire( 'foo' );
  288. view.fire( 'bar' );
  289. view.fire( 'baz' );
  290. view.fire( 'not-delegated' );
  291. sinon.assert.callOrder( spyFoo, spyBar, spyBaz );
  292. sinon.assert.callCount( spyFoo, 1 );
  293. sinon.assert.callCount( spyBar, 1 );
  294. sinon.assert.callCount( spyBaz, 1 );
  295. } );
  296. it( 'stops forwarding when view removed from the collection', () => {
  297. const target = new View();
  298. const view = new View();
  299. const spy = sinon.spy();
  300. collection.delegate( 'foo' ).to( target );
  301. target.on( 'foo', spy );
  302. collection.add( view );
  303. view.fire( 'foo' );
  304. sinon.assert.callCount( spy, 1 );
  305. collection.remove( 0 );
  306. view.fire( 'foo' );
  307. sinon.assert.callCount( spy, 1 );
  308. } );
  309. it( 'supports deep event delegation', ( done ) => {
  310. const target = new View();
  311. const viewA = new View();
  312. const viewAA = new View();
  313. const data = {};
  314. const deepCollection = viewA.createCollection();
  315. collection.add( viewA );
  316. collection.delegate( 'foo' ).to( target );
  317. deepCollection.add( viewAA );
  318. deepCollection.delegate( 'foo' ).to( viewA );
  319. target.on( 'foo', ( ...args ) => {
  320. assertDelegated( args, {
  321. expectedName: 'foo',
  322. expectedSource: viewAA,
  323. expectedPath: [ viewAA, viewA, target ],
  324. expectedData: [ data ]
  325. } );
  326. done();
  327. } );
  328. viewAA.fire( 'foo', data );
  329. } );
  330. } );
  331. } );
  332. } );
  333. function createTestCollection() {
  334. collection = new ViewCollection();
  335. }
  336. function assertDelegated( evtArgs, { expectedName, expectedSource, expectedPath, expectedData } ) {
  337. const evtInfo = evtArgs[ 0 ];
  338. expect( evtInfo.name ).to.equal( expectedName );
  339. expect( evtInfo.source ).to.equal( expectedSource );
  340. expect( evtInfo.path ).to.deep.equal( expectedPath );
  341. expect( evtArgs.slice( 1 ) ).to.deep.equal( expectedData );
  342. }