view.js 11 KB

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