view.js 8.8 KB

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