view.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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.ready ).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( 'addChildren()', () => {
  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 = {};
  72. view.addChildren( 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.addChildren( [ {}, {}, {} ] );
  79. expect( view._unboundChildren ).to.have.length( 3 );
  80. } );
  81. } );
  82. describe( 'init()', () => {
  83. beforeEach( createViewWithChildren );
  84. it( 'should throw if already initialized', () => {
  85. view.init();
  86. try {
  87. view.init();
  88. throw new Error( 'This should not be executed.' );
  89. } catch ( err ) {
  90. expect( err ).to.be.instanceof( CKEditorError );
  91. expect( err.message ).to.match( /ui-view-init-re/ );
  92. }
  93. } );
  94. it( 'should set view#ready', () => {
  95. expect( view.ready ).to.be.false;
  96. view.init();
  97. expect( view.ready ).to.be.true;
  98. } );
  99. it( 'calls init() on all view#_viewCollections', () => {
  100. const collectionA = view.createCollection();
  101. const collectionB = view.createCollection();
  102. const spyA = testUtils.sinon.spy( collectionA, 'init' );
  103. const spyB = testUtils.sinon.spy( collectionB, 'init' );
  104. view.init();
  105. sinon.assert.calledOnce( spyA );
  106. sinon.assert.calledOnce( spyB );
  107. sinon.assert.callOrder( spyA, spyB );
  108. } );
  109. } );
  110. describe( 'bind', () => {
  111. beforeEach( () => {
  112. setTestViewClass();
  113. setTestViewInstance();
  114. } );
  115. it( 'returns a shorthand for Template binding', () => {
  116. expect( view.bindTemplate.to ).to.be.a( 'function' );
  117. expect( view.bindTemplate.if ).to.be.a( 'function' );
  118. const binding = view.bindTemplate.to( 'a' );
  119. expect( binding.observable ).to.equal( view );
  120. expect( binding.emitter ).to.equal( view );
  121. } );
  122. } );
  123. describe( 'element', () => {
  124. beforeEach( createViewInstanceWithTemplate );
  125. it( 'invokes out of #template', () => {
  126. setTestViewInstance();
  127. expect( view.element ).to.be.an.instanceof( HTMLElement );
  128. expect( view.element.nodeName ).to.equal( 'A' );
  129. } );
  130. it( 'can be explicitly declared', () => {
  131. class CustomView extends View {
  132. constructor() {
  133. super();
  134. this.element = document.createElement( 'span' );
  135. }
  136. }
  137. view = new CustomView();
  138. expect( view.element ).to.be.an.instanceof( HTMLElement );
  139. } );
  140. it( 'is null when there is no template', () => {
  141. expect( new View().element ).to.be.null;
  142. } );
  143. it( 'registers child views found in the template', () => {
  144. const view = new View();
  145. const viewA = new View();
  146. const viewB = new View();
  147. const viewC = new View();
  148. viewA.template = new Template( { tag: 'a' } );
  149. viewB.template = new Template( { tag: 'b' } );
  150. viewC.template = new Template( { tag: 'c' } );
  151. view.template = new Template( {
  152. tag: 'div',
  153. children: [
  154. viewA,
  155. viewB,
  156. {
  157. tag: 'p',
  158. children: [
  159. viewC
  160. ]
  161. },
  162. {
  163. text: 'foo'
  164. }
  165. ]
  166. } );
  167. expect( view._unboundChildren ).to.have.length( 0 );
  168. // Render the view.
  169. view.element;
  170. expect( view._unboundChildren ).to.have.length( 3 );
  171. expect( view._unboundChildren.get( 0 ) ).to.equal( viewA );
  172. expect( view._unboundChildren.get( 1 ) ).to.equal( viewB );
  173. expect( view._unboundChildren.get( 2 ) ).to.equal( viewC );
  174. } );
  175. } );
  176. describe( 'destroy()', () => {
  177. beforeEach( createViewWithChildren );
  178. it( 'can be called multiple times', () => {
  179. expect( () => {
  180. view.destroy();
  181. } ).to.not.throw();
  182. } );
  183. it( 'should not touch the basic properties', () => {
  184. view.destroy();
  185. expect( view.element ).to.be.an.instanceof( HTMLElement );
  186. expect( view.template ).to.be.an.instanceof( Template );
  187. expect( view.locale ).to.be.an( 'object' );
  188. expect( view.locale.t ).to.be.a( 'function' );
  189. expect( view._viewCollections ).to.be.instanceOf( Collection );
  190. expect( view._unboundChildren ).to.be.instanceOf( ViewCollection );
  191. } );
  192. it( 'should not clear the #_unboundChildren', () => {
  193. const cached = view._unboundChildren;
  194. view.addChildren( [ new View(), new View() ] );
  195. expect( cached ).to.have.length( 4 );
  196. view.destroy();
  197. expect( cached ).to.have.length( 4 );
  198. } );
  199. it( 'should not clear the #_viewCollections', () => {
  200. const cached = view._viewCollections;
  201. expect( cached ).to.have.length( 1 );
  202. view.destroy();
  203. expect( cached ).to.have.length( 1 );
  204. } );
  205. it( 'leaves the #element in DOM', () => {
  206. const elRef = view.element;
  207. const parentEl = document.createElement( 'div' );
  208. parentEl.appendChild( view.element );
  209. view.destroy();
  210. expect( elRef.parentNode ).to.equal( parentEl );
  211. } );
  212. it( 'calls destroy() on all view#_viewCollections', () => {
  213. const collectionA = view.createCollection();
  214. const collectionB = view.createCollection();
  215. const spyA = testUtils.sinon.spy( collectionA, 'destroy' );
  216. const spyB = testUtils.sinon.spy( collectionB, 'destroy' );
  217. view.destroy();
  218. sinon.assert.calledOnce( spyA );
  219. sinon.assert.calledOnce( spyB );
  220. sinon.assert.callOrder( spyA, spyB );
  221. } );
  222. it( 'destroy a template–less view', () => {
  223. view = new View();
  224. expect( () => {
  225. view.destroy();
  226. } ).to.not.throw();
  227. } );
  228. } );
  229. } );
  230. function createViewInstanceWithTemplate() {
  231. setTestViewClass( { tag: 'a' } );
  232. setTestViewInstance();
  233. }
  234. function setTestViewClass( templateDef ) {
  235. TestView = class V extends View {
  236. constructor() {
  237. super();
  238. this.locale = { t() {} };
  239. if ( templateDef ) {
  240. this.template = new Template( templateDef );
  241. }
  242. }
  243. };
  244. }
  245. function setTestViewInstance() {
  246. view = new TestView();
  247. if ( view.template ) {
  248. document.body.appendChild( view.element );
  249. }
  250. }
  251. function createViewWithChildren() {
  252. class ChildView extends View {
  253. constructor() {
  254. super();
  255. this.template = new Template( {
  256. tag: 'span'
  257. } );
  258. }
  259. }
  260. childA = new ChildView();
  261. childB = new ChildView();
  262. setTestViewClass( {
  263. tag: 'p',
  264. children: [ childA, childB ]
  265. } );
  266. setTestViewInstance();
  267. }