8
0

view.js 8.3 KB

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