view.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document, HTMLElement */
  6. /* bender-tags: ui */
  7. 'use strict';
  8. import testUtils from '/tests/ckeditor5/_utils/utils.js';
  9. import View from '/ckeditor5/ui/view.js';
  10. import Template from '/ckeditor5/ui/template.js';
  11. import Region from '/ckeditor5/ui/region.js';
  12. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  13. let TestView, view;
  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.regions.length ).to.equal( 0 );
  25. } );
  26. it( 'defines the locale property and the "t" function', () => {
  27. const locale = { t() {} };
  28. view = new View( locale );
  29. expect( view.locale ).to.equal( locale );
  30. expect( view.t ).to.equal( locale.t );
  31. } );
  32. } );
  33. describe( 'init', () => {
  34. beforeEach( () => {
  35. setTestViewClass( {
  36. tag: 'p',
  37. children: [
  38. { tag: 'span' },
  39. { tag: 'strong' }
  40. ]
  41. } );
  42. } );
  43. it( 'calls child regions #init', () => {
  44. setTestViewInstance();
  45. const region1 = new Region( 'x' );
  46. const region2 = new Region( 'y' );
  47. view.register( region1, el => el );
  48. view.register( region2, el => el );
  49. const spy1 = testUtils.sinon.spy( region1, 'init' );
  50. const spy2 = testUtils.sinon.spy( region2, 'init' );
  51. view.init();
  52. sinon.assert.calledOnce( spy1 );
  53. sinon.assert.calledOnce( spy2 );
  54. } );
  55. it( 'initializes view regions with string selector', () => {
  56. setTestViewInstance();
  57. const region1 = new Region( 'x' );
  58. const region2 = new Region( 'y' );
  59. view.register( region1, 'span' );
  60. view.register( region2, 'strong' );
  61. view.init();
  62. expect( region1.element ).to.equal( view.element.firstChild );
  63. expect( region2.element ).to.equal( view.element.lastChild );
  64. } );
  65. it( 'initializes view regions with function selector', () => {
  66. setTestViewInstance();
  67. const region1 = new Region( 'x' );
  68. const region2 = new Region( 'y' );
  69. view.register( region1, el => el.firstChild );
  70. view.register( region2, el => el.lastChild );
  71. view.init();
  72. expect( region1.element ).to.equal( view.element.firstChild );
  73. expect( region2.element ).to.equal( view.element.lastChild );
  74. } );
  75. it( 'initializes view regions with boolean selector', () => {
  76. setTestViewInstance();
  77. const region1 = new Region( 'x' );
  78. const region2 = new Region( 'y' );
  79. view.register( region1, true );
  80. view.register( region2, true );
  81. view.init();
  82. expect( region1.element ).to.be.null;
  83. expect( region2.element ).to.be.null;
  84. } );
  85. } );
  86. describe( 'bind', () => {
  87. beforeEach( () => {
  88. setTestViewClass();
  89. setTestViewInstance();
  90. } );
  91. it( 'returns a shorthand for Template binding', () => {
  92. expect( view.bind.to ).to.be.a( 'function' );
  93. expect( view.bind.if ).to.be.a( 'function' );
  94. const binding = view.bind.to( 'a' );
  95. expect( binding.observable ).to.equal( view.model );
  96. expect( binding.emitter ).to.equal( view );
  97. } );
  98. } );
  99. describe( 'register', () => {
  100. beforeEach( () => {
  101. setTestViewClass();
  102. setTestViewInstance();
  103. } );
  104. it( 'should throw when first argument is neither Region instance nor string', () => {
  105. expect( () => {
  106. view.register( new Date() );
  107. } ).to.throw( CKEditorError, /ui-view-register-wrongtype/ );
  108. } );
  109. it( 'should throw when missing the selector argument', () => {
  110. expect( () => {
  111. view.register( 'x' );
  112. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  113. } );
  114. it( 'should throw when selector argument is of a wrong type', () => {
  115. expect( () => {
  116. view.register( 'x', new Date() );
  117. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  118. expect( () => {
  119. view.register( 'x', false );
  120. } ).to.throw( CKEditorError, /ui-view-register-badselector/ );
  121. } );
  122. it( 'should throw when overriding an existing region but without override flag set', () => {
  123. expect( () => {
  124. view.register( 'x', true );
  125. view.register( new Region( 'x' ), true );
  126. } ).to.throw( CKEditorError, /ui-view-register-override/ );
  127. } );
  128. it( 'should register a new region with region name as a first argument', () => {
  129. view.register( 'x', true );
  130. expect( view.regions.get( 'x' ) ).to.be.an.instanceof( Region );
  131. } );
  132. it( 'should register a new region with Region instance as a first argument', () => {
  133. view.register( new Region( 'y' ), true );
  134. expect( view.regions.get( 'y' ) ).to.be.an.instanceof( Region );
  135. } );
  136. it( 'should override an existing region with override flag', () => {
  137. view.template = new Template( {
  138. tag: 'div',
  139. children: [
  140. { tag: 'span' }
  141. ]
  142. } );
  143. const region1 = new Region( 'x' );
  144. const region2 = new Region( 'x' );
  145. view.register( region1, true );
  146. view.register( region2, true, true );
  147. view.register( 'x', 'span', true );
  148. view.init();
  149. expect( view.regions.get( 'x' ) ).to.equal( region2 );
  150. expect( view.regions.get( 'x' ).element ).to.equal( view.element.firstChild );
  151. } );
  152. it( 'should not override an existing region with the same region with override flag', () => {
  153. const region = new Region( 'x' );
  154. const spy = testUtils.sinon.spy( view.regions, 'remove' );
  155. view.register( region, true );
  156. view.register( region, true, true );
  157. sinon.assert.notCalled( spy );
  158. } );
  159. } );
  160. describe( 'element', () => {
  161. beforeEach( createViewInstanceWithTemplate );
  162. it( 'invokes out of #template', () => {
  163. setTestViewInstance();
  164. view.model.set( 'a', 1 );
  165. expect( view.element ).to.be.an.instanceof( HTMLElement );
  166. expect( view.element.nodeName ).to.equal( 'A' );
  167. } );
  168. it( 'can be explicitly declared', () => {
  169. class CustomView extends View {
  170. constructor() {
  171. super();
  172. this.element = document.createElement( 'span' );
  173. }
  174. }
  175. view = new CustomView();
  176. expect( view.element ).to.be.an.instanceof( HTMLElement );
  177. } );
  178. it( 'is null when there is no template', () => {
  179. expect( new View().element ).to.be.null;
  180. } );
  181. } );
  182. describe( 'destroy', () => {
  183. beforeEach( createViewInstanceWithTemplate );
  184. it( 'should destroy the view', () => {
  185. view.destroy();
  186. expect( view.model ).to.be.null;
  187. expect( view.regions ).to.be.null;
  188. expect( view.template ).to.be.null;
  189. expect( view.locale ).to.be.null;
  190. expect( view.t ).to.be.null;
  191. } );
  192. it( 'detaches the element from DOM', () => {
  193. const elRef = view.element;
  194. document.createElement( 'div' ).appendChild( view.element );
  195. view.destroy();
  196. expect( elRef.parentNode ).to.be.null;
  197. } );
  198. it( 'destroys child regions', () => {
  199. const region = new Region( 'x' );
  200. const spy = testUtils.sinon.spy( region, 'destroy' );
  201. const regionsRef = view.regions;
  202. const regionViewsRef = region.views;
  203. view.register( region, true );
  204. view.regions.get( 'x' ).views.add( new View() );
  205. view.destroy();
  206. expect( regionsRef.length ).to.equal( 0 );
  207. expect( regionViewsRef.length ).to.equal( 0 );
  208. expect( spy.calledOnce ).to.be.true;
  209. } );
  210. it( 'destroy a template–less view', () => {
  211. view = new View();
  212. expect( () => {
  213. view.destroy();
  214. } ).to.not.throw();
  215. } );
  216. } );
  217. } );
  218. function createViewInstanceWithTemplate() {
  219. setTestViewClass( { tag: 'a' } );
  220. setTestViewInstance();
  221. }
  222. function setTestViewClass( templateDef, regionsFn ) {
  223. TestView = class V extends View {
  224. constructor() {
  225. super();
  226. if ( templateDef ) {
  227. this.template = new Template( templateDef );
  228. }
  229. if ( templateDef && regionsFn ) {
  230. regionsFn.call( this );
  231. }
  232. }
  233. };
  234. }
  235. function setTestViewInstance() {
  236. view = new TestView();
  237. if ( view.template ) {
  238. document.body.appendChild( view.element );
  239. }
  240. }