8
0

view.js 11 KB

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