view.js 8.3 KB

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