8
0

view.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 views in #_viewCollections', () => {
  118. const spy = testUtils.sinon.spy( view, 'init' );
  119. const spyA = testUtils.sinon.spy( childA, 'init' );
  120. const spyB = testUtils.sinon.spy( childB, 'init' );
  121. expect( childA.ready ).to.be.false;
  122. expect( childB.ready ).to.be.false;
  123. return view.init().then( () => {
  124. expect( childA.ready ).to.be.true;
  125. expect( childB.ready ).to.be.true;
  126. sinon.assert.calledOnce( spyA );
  127. sinon.assert.calledOnce( spyB );
  128. sinon.assert.callOrder( spy, spyA, spyB );
  129. } );
  130. } );
  131. } );
  132. describe( 'bind', () => {
  133. beforeEach( () => {
  134. setTestViewClass();
  135. setTestViewInstance();
  136. } );
  137. it( 'returns a shorthand for Template binding', () => {
  138. expect( view.bindTemplate.to ).to.be.a( 'function' );
  139. expect( view.bindTemplate.if ).to.be.a( 'function' );
  140. const binding = view.bindTemplate.to( 'a' );
  141. expect( binding.observable ).to.equal( view );
  142. expect( binding.emitter ).to.equal( view );
  143. } );
  144. } );
  145. describe( 'element', () => {
  146. beforeEach( createViewInstanceWithTemplate );
  147. it( 'invokes out of #template', () => {
  148. setTestViewInstance();
  149. expect( view.element ).to.be.an.instanceof( HTMLElement );
  150. expect( view.element.nodeName ).to.equal( 'A' );
  151. } );
  152. it( 'can be explicitly declared', () => {
  153. class CustomView extends View {
  154. constructor() {
  155. super();
  156. this.element = document.createElement( 'span' );
  157. }
  158. }
  159. view = new CustomView();
  160. expect( view.element ).to.be.an.instanceof( HTMLElement );
  161. } );
  162. it( 'is null when there is no template', () => {
  163. expect( new View().element ).to.be.null;
  164. } );
  165. } );
  166. describe( 'destroy', () => {
  167. beforeEach( createViewWithChildren );
  168. it( 'should return a promise', () => {
  169. expect( view.destroy() ).to.be.instanceof( Promise );
  170. } );
  171. it( 'should set basic properties null', () => {
  172. return view.destroy().then( () => {
  173. expect( view.element ).to.be.null;
  174. expect( view.template ).to.be.null;
  175. expect( view.locale ).to.be.null;
  176. expect( view.t ).to.be.null;
  177. expect( view._unboundChildren ).to.be.null;
  178. expect( view._viewCollections ).to.be.null;
  179. } );
  180. } );
  181. it( 'clears #_unboundChildren', () => {
  182. const cached = view._unboundChildren;
  183. view.addChild( new View(), new View() );
  184. expect( cached ).to.have.length.above( 1 );
  185. return view.destroy().then( () => {
  186. expect( cached ).to.have.length( 0 );
  187. } );
  188. } );
  189. it( 'clears #_viewCollections', () => {
  190. const cached = view._viewCollections;
  191. expect( cached ).to.have.length( 1 );
  192. return view.destroy().then( () => {
  193. expect( cached ).to.have.length( 0 );
  194. } );
  195. } );
  196. it( 'detaches the element from DOM', () => {
  197. const elRef = view.element;
  198. document.createElement( 'div' ).appendChild( view.element );
  199. expect( elRef.parentNode ).to.be.not.null;
  200. return view.destroy().then( () => {
  201. expect( elRef.parentNode ).to.be.null;
  202. } );
  203. } );
  204. it( 'calls destroy() on all views in #_viewCollections', () => {
  205. const spy = testUtils.sinon.spy( view, 'destroy' );
  206. const spyA = testUtils.sinon.spy( childA, 'destroy' );
  207. const spyB = testUtils.sinon.spy( childB, 'destroy' );
  208. return view.init().then( () => {
  209. expect( view.ready ).to.be.true;
  210. expect( childA.ready ).to.be.true;
  211. expect( childB.ready ).to.be.true;
  212. return view.destroy().then( () => {
  213. sinon.assert.calledOnce( spyA );
  214. sinon.assert.calledOnce( spyB );
  215. sinon.assert.callOrder( spy, spyA, spyB );
  216. } );
  217. } );
  218. } );
  219. it( 'destroy a template–less view', () => {
  220. view = new View();
  221. expect( () => {
  222. view.destroy();
  223. } ).to.not.throw();
  224. } );
  225. } );
  226. } );
  227. function createViewInstanceWithTemplate() {
  228. setTestViewClass( { tag: 'a' } );
  229. setTestViewInstance();
  230. }
  231. function setTestViewClass( templateDef ) {
  232. TestView = class V extends View {
  233. constructor() {
  234. super();
  235. if ( templateDef ) {
  236. this.template = new Template( templateDef );
  237. }
  238. }
  239. };
  240. }
  241. function setTestViewInstance() {
  242. view = new TestView();
  243. if ( view.template ) {
  244. document.body.appendChild( view.element );
  245. }
  246. }
  247. function createViewWithChildren() {
  248. class ChildView extends View {
  249. constructor() {
  250. super();
  251. this.template = new Template( {
  252. tag: 'span'
  253. } );
  254. }
  255. }
  256. class ChildViewA extends ChildView {
  257. init() {
  258. const promise = new Promise( resolve => {
  259. setTimeout( resolve, 50 );
  260. } );
  261. return super.init().then( promise );
  262. }
  263. destroy() {
  264. const promise = new Promise( resolve => {
  265. setTimeout( resolve, 10 );
  266. } );
  267. return super.destroy().then( promise );
  268. }
  269. }
  270. class ChildViewB extends ChildView {
  271. init() {
  272. const promise = new Promise( resolve => {
  273. setTimeout( resolve, 10 );
  274. } );
  275. return super.init().then( promise );
  276. }
  277. destroy() {
  278. const promise = new Promise( resolve => {
  279. setTimeout( resolve, 50 );
  280. } );
  281. return super.destroy().then( promise );
  282. }
  283. }
  284. childA = new ChildViewA();
  285. childB = new ChildViewB();
  286. setTestViewClass( {
  287. tag: 'p',
  288. children: [ childA, childB ]
  289. } );
  290. setTestViewInstance();
  291. view.addChild( childA, childB );
  292. }