view.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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.bind.to ).to.be.a( 'function' );
  92. expect( view.bind.if ).to.be.a( 'function' );
  93. const binding = view.bind.to( 'a' );
  94. expect( binding.observable ).to.equal( view.model );
  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. view.model.set( 'a', 1 );
  164. expect( view.element ).to.be.an.instanceof( HTMLElement );
  165. expect( view.element.nodeName ).to.equal( 'A' );
  166. } );
  167. it( 'can be explicitly declared', () => {
  168. class CustomView extends View {
  169. constructor() {
  170. super();
  171. this.element = document.createElement( 'span' );
  172. }
  173. }
  174. view = new CustomView();
  175. expect( view.element ).to.be.an.instanceof( HTMLElement );
  176. } );
  177. it( 'is null when there is no template', () => {
  178. expect( new View().element ).to.be.null;
  179. } );
  180. } );
  181. describe( 'destroy', () => {
  182. beforeEach( createViewInstanceWithTemplate );
  183. it( 'should destroy the view', () => {
  184. view.destroy();
  185. expect( view.model ).to.be.null;
  186. expect( view.regions ).to.be.null;
  187. expect( view.template ).to.be.null;
  188. expect( view.locale ).to.be.null;
  189. expect( view.t ).to.be.null;
  190. } );
  191. it( 'detaches the element from DOM', () => {
  192. const elRef = view.element;
  193. document.createElement( 'div' ).appendChild( view.element );
  194. view.destroy();
  195. expect( elRef.parentNode ).to.be.null;
  196. } );
  197. it( 'destroys child regions', () => {
  198. const region = new Region( 'x' );
  199. const spy = testUtils.sinon.spy( region, 'destroy' );
  200. const regionsRef = view.regions;
  201. const regionViewsRef = region.views;
  202. view.register( region, true );
  203. view.regions.get( 'x' ).views.add( new View() );
  204. view.destroy();
  205. expect( regionsRef.length ).to.equal( 0 );
  206. expect( regionViewsRef.length ).to.equal( 0 );
  207. expect( spy.calledOnce ).to.be.true;
  208. } );
  209. it( 'destroy a template–less view', () => {
  210. view = new View();
  211. expect( () => {
  212. view.destroy();
  213. } ).to.not.throw();
  214. } );
  215. } );
  216. } );
  217. function createViewInstanceWithTemplate() {
  218. setTestViewClass( { tag: 'a' } );
  219. setTestViewInstance();
  220. }
  221. function setTestViewClass( templateDef, regionsFn ) {
  222. TestView = class V extends View {
  223. constructor() {
  224. super();
  225. if ( templateDef ) {
  226. this.template = new Template( templateDef );
  227. }
  228. if ( templateDef && regionsFn ) {
  229. regionsFn.call( this );
  230. }
  231. }
  232. };
  233. }
  234. function setTestViewInstance() {
  235. view = new TestView();
  236. if ( view.template ) {
  237. document.body.appendChild( view.element );
  238. }
  239. }