8
0

view.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document, HTMLElement */
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import View from '../src/view';
  8. import Template from '../src/template';
  9. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  10. import ViewCollection from '../src/viewcollection';
  11. import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
  12. import { assertCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  13. let TestView, view, childA, childB;
  14. describe( 'View', () => {
  15. testUtils.createSinonSandbox();
  16. afterEach( () => {
  17. if ( view.element ) {
  18. view.element.remove();
  19. }
  20. view.destroy();
  21. } );
  22. describe( 'constructor()', () => {
  23. beforeEach( () => {
  24. setTestViewClass();
  25. setTestViewInstance();
  26. } );
  27. it( 'defines basic view properties', () => {
  28. view = new View();
  29. expect( view.t ).to.be.undefined;
  30. expect( view.locale ).to.be.undefined;
  31. expect( view.isRendered ).to.be.false;
  32. expect( view.template ).to.be.undefined;
  33. expect( view._viewCollections ).to.be.instanceOf( Collection );
  34. expect( view._unboundChildren ).to.be.instanceOf( ViewCollection );
  35. } );
  36. it( 'defines the locale property and the "t" function', () => {
  37. const locale = { t() {} };
  38. view = new View( locale );
  39. expect( view.locale ).to.equal( locale );
  40. expect( view.t ).to.equal( locale.t );
  41. } );
  42. describe( '_viewCollections', () => {
  43. it( 'manages #locale property', () => {
  44. const locale = {
  45. t() {}
  46. };
  47. const view = new View( locale );
  48. const collection = new ViewCollection();
  49. expect( view.locale ).to.equal( locale );
  50. expect( collection.locale ).to.be.undefined;
  51. view._viewCollections.add( collection );
  52. expect( collection.locale ).to.equal( view.locale );
  53. } );
  54. } );
  55. } );
  56. describe( 'createCollection()', () => {
  57. beforeEach( () => {
  58. setTestViewClass();
  59. setTestViewInstance();
  60. } );
  61. it( 'returns an instance of view collection', () => {
  62. expect( view.createCollection() ).to.be.instanceOf( ViewCollection );
  63. } );
  64. it( 'adds a new collection to the #_viewCollections', () => {
  65. expect( view._viewCollections ).to.have.length( 1 );
  66. const collection = view.createCollection();
  67. expect( view._viewCollections ).to.have.length( 2 );
  68. expect( view._viewCollections.get( 1 ) ).to.equal( collection );
  69. } );
  70. } );
  71. describe( 'registerChild()', () => {
  72. beforeEach( () => {
  73. setTestViewClass();
  74. setTestViewInstance();
  75. } );
  76. it( 'should add a single view to #_unboundChildren', () => {
  77. expect( view._unboundChildren ).to.have.length( 0 );
  78. const child = new View();
  79. view.registerChild( child );
  80. expect( view._unboundChildren ).to.have.length( 1 );
  81. expect( view._unboundChildren.get( 0 ) ).to.equal( child );
  82. } );
  83. it( 'should support iterables', () => {
  84. expect( view._unboundChildren ).to.have.length( 0 );
  85. view.registerChild( [ new View(), new View(), new View() ] );
  86. expect( view._unboundChildren ).to.have.length( 3 );
  87. } );
  88. } );
  89. describe( 'deregisterChild()', () => {
  90. beforeEach( () => {
  91. setTestViewClass();
  92. setTestViewInstance();
  93. } );
  94. it( 'should remove a single view from #_unboundChildren', () => {
  95. const child1 = new View();
  96. const child2 = new View();
  97. view.registerChild( child1 );
  98. view.registerChild( child2 );
  99. expect( view._unboundChildren ).to.have.length( 2 );
  100. view.deregisterChild( child2 );
  101. expect( view._unboundChildren ).to.have.length( 1 );
  102. expect( view._unboundChildren.get( 0 ) ).to.equal( child1 );
  103. } );
  104. it( 'should support iterables', () => {
  105. const child1 = new View();
  106. const child2 = new View();
  107. const child3 = new View();
  108. view.registerChild( [ child1, child2, child3 ] );
  109. expect( view._unboundChildren ).to.have.length( 3 );
  110. view.deregisterChild( [ child2, child3 ] );
  111. expect( view._unboundChildren ).to.have.length( 1 );
  112. expect( view._unboundChildren.get( 0 ) ).to.equal( child1 );
  113. } );
  114. } );
  115. describe( 'setTemplate()', () => {
  116. it( 'sets the template', () => {
  117. const view = new View();
  118. const bind = view.bindTemplate;
  119. view.set( 'foo', 'bar' );
  120. view.setTemplate( {
  121. tag: 'div',
  122. attributes: {
  123. class: [
  124. bind.to( 'foo' )
  125. ]
  126. }
  127. } );
  128. view.render();
  129. expect( normalizeHtml( view.element.outerHTML ) ).to.equal( '<div class="bar"></div>' );
  130. } );
  131. } );
  132. describe( 'extendTemplate()', () => {
  133. it( 'extends the template', () => {
  134. const view = new View();
  135. const bind = view.bindTemplate;
  136. view.set( 'foo', 'bar' );
  137. view.setTemplate( {
  138. tag: 'div'
  139. } );
  140. view.extendTemplate( {
  141. attributes: {
  142. class: [
  143. bind.to( 'foo' )
  144. ]
  145. }
  146. } );
  147. view.render();
  148. expect( normalizeHtml( view.element.outerHTML ) ).to.equal( '<div class="bar"></div>' );
  149. } );
  150. } );
  151. describe( 'render()', () => {
  152. it( 'is decorated', done => {
  153. const view = new View();
  154. view.on( 'render', () => {
  155. expect( view.isRendered ).to.be.true;
  156. done();
  157. } );
  158. view.render();
  159. } );
  160. it( 'should throw if already rendered', () => {
  161. const view = new View();
  162. view.render();
  163. try {
  164. view.render();
  165. throw new Error( 'This should not be executed.' );
  166. } catch ( err ) {
  167. // TODO
  168. assertCKEditorError( err, /^ui-view-render-already-rendered:/, view );
  169. }
  170. } );
  171. it( 'should set view#isRendered', () => {
  172. const view = new View();
  173. view.setTemplate( {
  174. tag: 'div'
  175. } );
  176. expect( view.isRendered ).to.be.false;
  177. view.render();
  178. expect( view.isRendered ).to.be.true;
  179. } );
  180. } );
  181. describe( 'bind', () => {
  182. beforeEach( () => {
  183. setTestViewClass();
  184. setTestViewInstance();
  185. } );
  186. it( 'returns a shorthand for Template binding', () => {
  187. expect( view.bindTemplate.to ).to.be.a( 'function' );
  188. expect( view.bindTemplate.if ).to.be.a( 'function' );
  189. const binding = view.bindTemplate.to( 'a' );
  190. expect( binding.observable ).to.equal( view );
  191. expect( binding.emitter ).to.equal( view );
  192. } );
  193. } );
  194. describe( 'element', () => {
  195. beforeEach( createViewInstanceWithTemplate );
  196. it( 'invokes out of #template', () => {
  197. expect( view.element ).to.be.an.instanceof( HTMLElement );
  198. expect( view.element.nodeName ).to.equal( 'A' );
  199. } );
  200. it( 'can be explicitly declared', () => {
  201. class CustomView extends View {
  202. constructor() {
  203. super();
  204. this.element = document.createElement( 'span' );
  205. }
  206. }
  207. const view = new CustomView();
  208. expect( view.element ).to.be.an.instanceof( HTMLElement );
  209. } );
  210. it( 'is null when there is no template', () => {
  211. const view = new View();
  212. view.render();
  213. expect( view.element ).to.be.null;
  214. } );
  215. it( 'registers child views found in the template', () => {
  216. const view = new View();
  217. const viewA = new View();
  218. const viewB = new View();
  219. const viewC = new View();
  220. viewA.setTemplate( { tag: 'a' } );
  221. viewB.setTemplate( { tag: 'b' } );
  222. viewC.setTemplate( { tag: 'c' } );
  223. view.setTemplate( {
  224. tag: 'div',
  225. children: [
  226. viewA,
  227. viewB,
  228. {
  229. tag: 'p',
  230. children: [
  231. viewC
  232. ]
  233. },
  234. {
  235. text: 'foo'
  236. }
  237. ]
  238. } );
  239. expect( view._unboundChildren ).to.have.length( 0 );
  240. view.render();
  241. expect( view._unboundChildren ).to.have.length( 3 );
  242. expect( view._unboundChildren.get( 0 ) ).to.equal( viewA );
  243. expect( view._unboundChildren.get( 1 ) ).to.equal( viewB );
  244. expect( view._unboundChildren.get( 2 ) ).to.equal( viewC );
  245. } );
  246. } );
  247. describe( 'destroy()', () => {
  248. beforeEach( createViewWithChildren );
  249. it( 'can be called multiple times', () => {
  250. expect( () => {
  251. view.destroy();
  252. view.destroy();
  253. view.destroy();
  254. } ).to.not.throw();
  255. } );
  256. it( 'should not touch the basic properties', () => {
  257. view.destroy();
  258. expect( view.element ).to.be.an.instanceof( HTMLElement );
  259. expect( view.template ).to.be.an.instanceof( Template );
  260. expect( view.locale ).to.be.an( 'object' );
  261. expect( view.locale.t ).to.be.a( 'function' );
  262. expect( view._viewCollections ).to.be.instanceOf( Collection );
  263. expect( view._unboundChildren ).to.be.instanceOf( ViewCollection );
  264. } );
  265. it( 'should not clear the #_unboundChildren', () => {
  266. const cached = view._unboundChildren;
  267. view.registerChild( [ new View(), new View() ] );
  268. expect( cached ).to.have.length( 4 );
  269. view.destroy();
  270. expect( cached ).to.have.length( 4 );
  271. } );
  272. it( 'should not clear the #_viewCollections', () => {
  273. const cached = view._viewCollections;
  274. expect( cached ).to.have.length( 1 );
  275. view.destroy();
  276. expect( cached ).to.have.length( 1 );
  277. } );
  278. it( 'leaves the #element in DOM', () => {
  279. const elRef = view.element;
  280. const parentEl = document.createElement( 'div' );
  281. parentEl.appendChild( view.element );
  282. view.destroy();
  283. expect( elRef.parentNode ).to.equal( parentEl );
  284. } );
  285. it( 'calls destroy() on all view#_viewCollections', () => {
  286. const collectionA = view.createCollection();
  287. const collectionB = view.createCollection();
  288. const spyA = testUtils.sinon.spy( collectionA, 'destroy' );
  289. const spyB = testUtils.sinon.spy( collectionB, 'destroy' );
  290. view.destroy();
  291. sinon.assert.calledOnce( spyA );
  292. sinon.assert.calledOnce( spyB );
  293. sinon.assert.callOrder( spyA, spyB );
  294. } );
  295. it( 'destroy a template–less view', () => {
  296. const view = new View();
  297. expect( () => {
  298. view.destroy();
  299. } ).to.not.throw();
  300. } );
  301. } );
  302. } );
  303. function createViewInstanceWithTemplate() {
  304. setTestViewClass( { tag: 'a' } );
  305. setTestViewInstance();
  306. }
  307. function setTestViewClass( templateDef ) {
  308. TestView = class V extends View {
  309. constructor() {
  310. super();
  311. this.locale = { t() {} };
  312. if ( templateDef ) {
  313. this.setTemplate( templateDef );
  314. }
  315. }
  316. };
  317. }
  318. function setTestViewInstance() {
  319. view = new TestView();
  320. if ( view.template ) {
  321. view.render();
  322. document.body.appendChild( view.element );
  323. }
  324. }
  325. function createViewWithChildren() {
  326. class ChildView extends View {
  327. constructor() {
  328. super();
  329. this.setTemplate( {
  330. tag: 'span'
  331. } );
  332. }
  333. }
  334. childA = new ChildView();
  335. childB = new ChildView();
  336. setTestViewClass( {
  337. tag: 'p',
  338. children: [ childA, childB ]
  339. } );
  340. setTestViewInstance();
  341. }