8
0

view.js 7.7 KB

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