view.js 8.1 KB

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