view.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  10. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  11. import ViewCollection from '../src/viewcollection';
  12. import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
  13. let TestView, view, childA, childB;
  14. testUtils.createSinonSandbox();
  15. describe( 'View', () => {
  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( 'registerChildren()', () => {
  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.registerChildren( 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.registerChildren( [ new View(), new View(), new View() ] );
  86. expect( view._unboundChildren ).to.have.length( 3 );
  87. } );
  88. } );
  89. describe( 'deregisterChildren()', () => {
  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.registerChildren( child1 );
  98. view.registerChildren( child2 );
  99. expect( view._unboundChildren ).to.have.length( 2 );
  100. view.deregisterChildren( 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.registerChildren( [ child1, child2, child3 ] );
  109. expect( view._unboundChildren ).to.have.length( 3 );
  110. view.deregisterChildren( [ 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( 'should throw if already rendered', () => {
  153. const view = new View();
  154. view.render();
  155. try {
  156. view.render();
  157. throw new Error( 'This should not be executed.' );
  158. } catch ( err ) {
  159. expect( err ).to.be.instanceof( CKEditorError );
  160. expect( err.message ).to.match( /^ui-view-render-already-rendered:/ );
  161. }
  162. } );
  163. it( 'should set view#isRendered', () => {
  164. const view = new View();
  165. view.setTemplate( {
  166. tag: 'div'
  167. } );
  168. expect( view.isRendered ).to.be.false;
  169. view.render();
  170. expect( view.isRendered ).to.be.true;
  171. } );
  172. } );
  173. describe( 'bind', () => {
  174. beforeEach( () => {
  175. setTestViewClass();
  176. setTestViewInstance();
  177. } );
  178. it( 'returns a shorthand for Template binding', () => {
  179. expect( view.bindTemplate.to ).to.be.a( 'function' );
  180. expect( view.bindTemplate.if ).to.be.a( 'function' );
  181. const binding = view.bindTemplate.to( 'a' );
  182. expect( binding.observable ).to.equal( view );
  183. expect( binding.emitter ).to.equal( view );
  184. } );
  185. } );
  186. describe( 'element', () => {
  187. beforeEach( createViewInstanceWithTemplate );
  188. it( 'invokes out of #template', () => {
  189. expect( view.element ).to.be.an.instanceof( HTMLElement );
  190. expect( view.element.nodeName ).to.equal( 'A' );
  191. } );
  192. it( 'can be explicitly declared', () => {
  193. class CustomView extends View {
  194. constructor() {
  195. super();
  196. this.element = document.createElement( 'span' );
  197. }
  198. }
  199. const view = new CustomView();
  200. expect( view.element ).to.be.an.instanceof( HTMLElement );
  201. } );
  202. it( 'is null when there is no template', () => {
  203. const view = new View();
  204. view.render();
  205. expect( view.element ).to.be.null;
  206. } );
  207. it( 'registers child views found in the template', () => {
  208. const view = new View();
  209. const viewA = new View();
  210. const viewB = new View();
  211. const viewC = new View();
  212. viewA.setTemplate( { tag: 'a' } );
  213. viewB.setTemplate( { tag: 'b' } );
  214. viewC.setTemplate( { tag: 'c' } );
  215. view.setTemplate( {
  216. tag: 'div',
  217. children: [
  218. viewA,
  219. viewB,
  220. {
  221. tag: 'p',
  222. children: [
  223. viewC
  224. ]
  225. },
  226. {
  227. text: 'foo'
  228. }
  229. ]
  230. } );
  231. expect( view._unboundChildren ).to.have.length( 0 );
  232. view.render();
  233. expect( view._unboundChildren ).to.have.length( 3 );
  234. expect( view._unboundChildren.get( 0 ) ).to.equal( viewA );
  235. expect( view._unboundChildren.get( 1 ) ).to.equal( viewB );
  236. expect( view._unboundChildren.get( 2 ) ).to.equal( viewC );
  237. } );
  238. } );
  239. describe( 'destroy()', () => {
  240. beforeEach( createViewWithChildren );
  241. it( 'can be called multiple times', () => {
  242. expect( () => {
  243. view.destroy();
  244. } ).to.not.throw();
  245. } );
  246. it( 'should not touch the basic properties', () => {
  247. view.destroy();
  248. expect( view.element ).to.be.an.instanceof( HTMLElement );
  249. expect( view.template ).to.be.an.instanceof( Template );
  250. expect( view.locale ).to.be.an( 'object' );
  251. expect( view.locale.t ).to.be.a( 'function' );
  252. expect( view._viewCollections ).to.be.instanceOf( Collection );
  253. expect( view._unboundChildren ).to.be.instanceOf( ViewCollection );
  254. } );
  255. it( 'should not clear the #_unboundChildren', () => {
  256. const cached = view._unboundChildren;
  257. view.registerChildren( [ new View(), new View() ] );
  258. expect( cached ).to.have.length( 4 );
  259. view.destroy();
  260. expect( cached ).to.have.length( 4 );
  261. } );
  262. it( 'should not clear the #_viewCollections', () => {
  263. const cached = view._viewCollections;
  264. expect( cached ).to.have.length( 1 );
  265. view.destroy();
  266. expect( cached ).to.have.length( 1 );
  267. } );
  268. it( 'leaves the #element in DOM', () => {
  269. const elRef = view.element;
  270. const parentEl = document.createElement( 'div' );
  271. parentEl.appendChild( view.element );
  272. view.destroy();
  273. expect( elRef.parentNode ).to.equal( parentEl );
  274. } );
  275. it( 'calls destroy() on all view#_viewCollections', () => {
  276. const collectionA = view.createCollection();
  277. const collectionB = view.createCollection();
  278. const spyA = testUtils.sinon.spy( collectionA, 'destroy' );
  279. const spyB = testUtils.sinon.spy( collectionB, 'destroy' );
  280. view.destroy();
  281. sinon.assert.calledOnce( spyA );
  282. sinon.assert.calledOnce( spyB );
  283. sinon.assert.callOrder( spyA, spyB );
  284. } );
  285. it( 'destroy a template–less view', () => {
  286. const view = new View();
  287. expect( () => {
  288. view.destroy();
  289. } ).to.not.throw();
  290. } );
  291. } );
  292. } );
  293. function createViewInstanceWithTemplate() {
  294. setTestViewClass( { tag: 'a' } );
  295. setTestViewInstance();
  296. }
  297. function setTestViewClass( templateDef ) {
  298. TestView = class V extends View {
  299. constructor() {
  300. super();
  301. this.locale = { t() {} };
  302. if ( templateDef ) {
  303. this.setTemplate( templateDef );
  304. }
  305. }
  306. };
  307. }
  308. function setTestViewInstance() {
  309. view = new TestView();
  310. if ( view.template ) {
  311. view.render();
  312. document.body.appendChild( view.element );
  313. }
  314. }
  315. function createViewWithChildren() {
  316. class ChildView extends View {
  317. constructor() {
  318. super();
  319. this.setTemplate( {
  320. tag: 'span'
  321. } );
  322. }
  323. }
  324. childA = new ChildView();
  325. childB = new ChildView();
  326. setTestViewClass( {
  327. tag: 'p',
  328. children: [ childA, childB ]
  329. } );
  330. setTestViewInstance();
  331. }