view.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document, setTimeout, HTMLElement */
  6. /* bender-tags: ui */
  7. import testUtils from 'tests/core/_utils/utils.js';
  8. import View from 'ckeditor5/ui/view.js';
  9. import Template from 'ckeditor5/ui/template.js';
  10. import CKEditorError from 'ckeditor5/utils/ckeditorerror.js';
  11. import Collection from 'ckeditor5/utils/collection.js';
  12. import ViewCollection from 'ckeditor5/ui/viewcollection.js';
  13. let TestView, view, childA, childB;
  14. testUtils.createSinonSandbox();
  15. describe( 'View', () => {
  16. describe( 'constructor()', () => {
  17. beforeEach( () => {
  18. setTestViewClass();
  19. setTestViewInstance();
  20. } );
  21. it( 'defines basic view properties', () => {
  22. view = new View();
  23. expect( view.t ).to.be.undefined;
  24. expect( view.locale ).to.be.undefined;
  25. expect( view.ready ).to.be.false;
  26. expect( view.template ).to.be.undefined;
  27. expect( view._viewCollections ).to.be.instanceOf( Collection );
  28. expect( view._unboundChildren ).to.be.instanceOf( ViewCollection );
  29. } );
  30. it( 'defines the locale property and the "t" function', () => {
  31. const locale = { t() {} };
  32. view = new View( locale );
  33. expect( view.locale ).to.equal( locale );
  34. expect( view.t ).to.equal( locale.t );
  35. } );
  36. describe( '_viewCollections', () => {
  37. it( 'manages #locale property', () => {
  38. const locale = {
  39. t() {}
  40. };
  41. const view = new View( locale );
  42. const collection = new ViewCollection();
  43. expect( view.locale ).to.equal( locale );
  44. expect( collection.locale ).to.be.undefined;
  45. view._viewCollections.add( collection );
  46. expect( collection.locale ).to.equal( view.locale );
  47. } );
  48. } );
  49. } );
  50. describe( 'createCollection', () => {
  51. beforeEach( () => {
  52. setTestViewClass();
  53. setTestViewInstance();
  54. } );
  55. it( 'returns an instance of view collection', () => {
  56. expect( view.createCollection() ).to.be.instanceOf( ViewCollection );
  57. } );
  58. it( 'adds a new collection to the #_viewCollections', () => {
  59. expect( view._viewCollections ).to.have.length( 1 );
  60. const collection = view.createCollection();
  61. expect( view._viewCollections ).to.have.length( 2 );
  62. expect( view._viewCollections.get( 1 ) ).to.equal( collection );
  63. } );
  64. } );
  65. describe( 'addChild', () => {
  66. beforeEach( () => {
  67. setTestViewClass();
  68. setTestViewInstance();
  69. } );
  70. it( 'should add a view to #_unboundChildren', () => {
  71. expect( view._unboundChildren ).to.have.length( 0 );
  72. const child = {};
  73. view.addChild( child );
  74. expect( view._unboundChildren ).to.have.length( 1 );
  75. expect( view._unboundChildren.get( 0 ) ).to.equal( child );
  76. } );
  77. it( 'should support multiple ...arguments', () => {
  78. expect( view._unboundChildren ).to.have.length( 0 );
  79. const children = [ {}, {}, {} ];
  80. view.addChild( ...children );
  81. expect( view._unboundChildren ).to.have.length( 3 );
  82. } );
  83. } );
  84. describe( 'init()', () => {
  85. beforeEach( createViewWithChildren );
  86. it( 'should throw if already initialized', () => {
  87. return view.init()
  88. .then( () => {
  89. view.init();
  90. throw new Error( 'This should not be executed.' );
  91. } )
  92. .catch( ( err ) => {
  93. expect( err ).to.be.instanceof( CKEditorError );
  94. expect( err.message ).to.match( /ui-view-init-re/ );
  95. } );
  96. } );
  97. it( 'returns a promise', () => {
  98. expect( view.init() ).to.be.instanceof( Promise );
  99. } );
  100. it( 'should set view#ready', () => {
  101. expect( view.ready ).to.be.false;
  102. return view.init().then( () => {
  103. expect( view.ready ).to.be.true;
  104. } );
  105. } );
  106. it( 'calls init() on all view#_viewCollections', () => {
  107. const collectionA = view.createCollection();
  108. const collectionB = view.createCollection();
  109. const spyA = testUtils.sinon.spy( collectionA, 'init' );
  110. const spyB = testUtils.sinon.spy( collectionB, 'init' );
  111. return view.init().then( () => {
  112. sinon.assert.calledOnce( spyA );
  113. sinon.assert.calledOnce( spyB );
  114. sinon.assert.callOrder( spyA, spyB );
  115. } );
  116. } );
  117. } );
  118. describe( 'bind', () => {
  119. beforeEach( () => {
  120. setTestViewClass();
  121. setTestViewInstance();
  122. } );
  123. it( 'returns a shorthand for Template binding', () => {
  124. expect( view.bindTemplate.to ).to.be.a( 'function' );
  125. expect( view.bindTemplate.if ).to.be.a( 'function' );
  126. const binding = view.bindTemplate.to( 'a' );
  127. expect( binding.observable ).to.equal( view );
  128. expect( binding.emitter ).to.equal( view );
  129. } );
  130. } );
  131. describe( 'element', () => {
  132. beforeEach( createViewInstanceWithTemplate );
  133. it( 'invokes out of #template', () => {
  134. setTestViewInstance();
  135. expect( view.element ).to.be.an.instanceof( HTMLElement );
  136. expect( view.element.nodeName ).to.equal( 'A' );
  137. } );
  138. it( 'can be explicitly declared', () => {
  139. class CustomView extends View {
  140. constructor() {
  141. super();
  142. this.element = document.createElement( 'span' );
  143. }
  144. }
  145. view = new CustomView();
  146. expect( view.element ).to.be.an.instanceof( HTMLElement );
  147. } );
  148. it( 'is null when there is no template', () => {
  149. expect( new View().element ).to.be.null;
  150. } );
  151. } );
  152. describe( 'destroy()', () => {
  153. beforeEach( createViewWithChildren );
  154. it( 'should return a promise', () => {
  155. expect( view.destroy() ).to.be.instanceof( Promise );
  156. } );
  157. it( 'should set basic properties null', () => {
  158. return view.destroy().then( () => {
  159. expect( view.element ).to.be.null;
  160. expect( view.template ).to.be.null;
  161. expect( view.locale ).to.be.null;
  162. expect( view.t ).to.be.null;
  163. expect( view._unboundChildren ).to.be.null;
  164. expect( view._viewCollections ).to.be.null;
  165. } );
  166. } );
  167. it( 'clears #_unboundChildren', () => {
  168. const cached = view._unboundChildren;
  169. view.addChild( new View(), new View() );
  170. expect( cached ).to.have.length.above( 1 );
  171. return view.destroy().then( () => {
  172. expect( cached ).to.have.length( 0 );
  173. } );
  174. } );
  175. it( 'clears #_viewCollections', () => {
  176. const cached = view._viewCollections;
  177. expect( cached ).to.have.length( 1 );
  178. return view.destroy().then( () => {
  179. expect( cached ).to.have.length( 0 );
  180. } );
  181. } );
  182. it( 'detaches the element from DOM', () => {
  183. const elRef = view.element;
  184. document.createElement( 'div' ).appendChild( view.element );
  185. expect( elRef.parentNode ).to.be.not.null;
  186. return view.destroy().then( () => {
  187. expect( elRef.parentNode ).to.be.null;
  188. } );
  189. } );
  190. it( 'calls destroy() on all view#_viewCollections', () => {
  191. const collectionA = view.createCollection();
  192. const collectionB = view.createCollection();
  193. const spyA = testUtils.sinon.spy( collectionA, 'destroy' );
  194. const spyB = testUtils.sinon.spy( collectionB, 'destroy' );
  195. return view.destroy().then( () => {
  196. sinon.assert.calledOnce( spyA );
  197. sinon.assert.calledOnce( spyB );
  198. sinon.assert.callOrder( spyA, spyB );
  199. } );
  200. } );
  201. it( 'destroy a template–less view', () => {
  202. view = new View();
  203. expect( () => {
  204. view.destroy();
  205. } ).to.not.throw();
  206. } );
  207. } );
  208. } );
  209. function createViewInstanceWithTemplate() {
  210. setTestViewClass( { tag: 'a' } );
  211. setTestViewInstance();
  212. }
  213. function setTestViewClass( templateDef ) {
  214. TestView = class V extends View {
  215. constructor() {
  216. super();
  217. if ( templateDef ) {
  218. this.template = new Template( templateDef );
  219. }
  220. }
  221. };
  222. }
  223. function setTestViewInstance() {
  224. view = new TestView();
  225. if ( view.template ) {
  226. document.body.appendChild( view.element );
  227. }
  228. }
  229. function createViewWithChildren() {
  230. class ChildView extends View {
  231. constructor() {
  232. super();
  233. this.template = new Template( {
  234. tag: 'span'
  235. } );
  236. }
  237. }
  238. class ChildViewA extends ChildView {
  239. init() {
  240. const promise = new Promise( resolve => {
  241. setTimeout( resolve, 50 );
  242. } );
  243. return super.init().then( promise );
  244. }
  245. destroy() {
  246. const promise = new Promise( resolve => {
  247. setTimeout( resolve, 10 );
  248. } );
  249. return super.destroy().then( promise );
  250. }
  251. }
  252. class ChildViewB extends ChildView {
  253. init() {
  254. const promise = new Promise( resolve => {
  255. setTimeout( resolve, 10 );
  256. } );
  257. return super.init().then( promise );
  258. }
  259. destroy() {
  260. const promise = new Promise( resolve => {
  261. setTimeout( resolve, 50 );
  262. } );
  263. return super.destroy().then( promise );
  264. }
  265. }
  266. childA = new ChildViewA();
  267. childB = new ChildViewB();
  268. setTestViewClass( {
  269. tag: 'p',
  270. children: [ childA, childB ]
  271. } );
  272. setTestViewInstance();
  273. view.addChild( childA, childB );
  274. }