view.js 9.5 KB

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