8
0

view.js 9.0 KB

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