view.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. let TestView, view;
  12. testUtils.createSinonSandbox();
  13. describe( 'View', () => {
  14. describe( 'constructor', () => {
  15. beforeEach( () => {
  16. setTestViewClass();
  17. setTestViewInstance();
  18. } );
  19. it( 'defines basic view properties', () => {
  20. view = new View();
  21. expect( view.t ).to.be.undefined;
  22. expect( view.regions.length ).to.equal( 0 );
  23. } );
  24. it( 'defines the locale property and the "t" function', () => {
  25. const locale = { t() {} };
  26. view = new View( locale );
  27. expect( view.locale ).to.equal( locale );
  28. expect( view.t ).to.equal( locale.t );
  29. } );
  30. } );
  31. describe( 'init', () => {
  32. beforeEach( () => {
  33. setTestViewClass( {
  34. tag: 'p',
  35. children: [
  36. { tag: 'span' },
  37. { tag: 'strong' }
  38. ]
  39. } );
  40. } );
  41. it( 'calls child regions #init', () => {
  42. setTestViewInstance();
  43. const region1 = new Region( 'x' );
  44. const region2 = new Region( 'y' );
  45. view.register( region1, el => el );
  46. view.register( region2, el => el );
  47. const spy1 = testUtils.sinon.spy( region1, 'init' );
  48. const spy2 = testUtils.sinon.spy( region2, 'init' );
  49. view.init();
  50. sinon.assert.calledOnce( spy1 );
  51. sinon.assert.calledOnce( spy2 );
  52. } );
  53. it( 'initializes view regions with string selector', () => {
  54. setTestViewInstance();
  55. const region1 = new Region( 'x' );
  56. const region2 = new Region( 'y' );
  57. view.register( region1, 'span' );
  58. view.register( region2, 'strong' );
  59. view.init();
  60. expect( region1.element ).to.equal( view.element.firstChild );
  61. expect( region2.element ).to.equal( view.element.lastChild );
  62. } );
  63. it( 'initializes view regions with function selector', () => {
  64. setTestViewInstance();
  65. const region1 = new Region( 'x' );
  66. const region2 = new Region( 'y' );
  67. view.register( region1, el => el.firstChild );
  68. view.register( region2, el => el.lastChild );
  69. view.init();
  70. expect( region1.element ).to.equal( view.element.firstChild );
  71. expect( region2.element ).to.equal( view.element.lastChild );
  72. } );
  73. it( 'initializes view regions with boolean selector', () => {
  74. setTestViewInstance();
  75. const region1 = new Region( 'x' );
  76. const region2 = new Region( 'y' );
  77. view.register( region1, true );
  78. view.register( region2, true );
  79. view.init();
  80. expect( region1.element ).to.be.null;
  81. expect( region2.element ).to.be.null;
  82. } );
  83. } );
  84. describe( 'bind', () => {
  85. beforeEach( () => {
  86. setTestViewClass();
  87. setTestViewInstance();
  88. } );
  89. it( 'returns a shorthand for Template binding', () => {
  90. expect( view.bind.to ).to.be.a( 'function' );
  91. expect( view.bind.if ).to.be.a( 'function' );
  92. const binding = view.bind.to( 'a' );
  93. expect( binding.observable ).to.equal( view.model );
  94. expect( binding.emitter ).to.equal( view );
  95. } );
  96. } );
  97. describe( 'register', () => {
  98. beforeEach( () => {
  99. setTestViewClass();
  100. setTestViewInstance();
  101. } );
  102. it( 'should throw when first argument is neither Region instance nor string', () => {
  103. expect( () => {
  104. view.register( new Date() );
  105. } ).to.throwCKEditorError( /ui-view-register-wrongtype/ );
  106. } );
  107. it( 'should throw when missing the selector argument', () => {
  108. expect( () => {
  109. view.register( 'x' );
  110. } ).to.throwCKEditorError( /ui-view-register-badselector/ );
  111. } );
  112. it( 'should throw when selector argument is of a wrong type', () => {
  113. expect( () => {
  114. view.register( 'x', new Date() );
  115. } ).to.throwCKEditorError( /ui-view-register-badselector/ );
  116. expect( () => {
  117. view.register( 'x', false );
  118. } ).to.throwCKEditorError( /ui-view-register-badselector/ );
  119. } );
  120. it( 'should throw when overriding an existing region but without override flag set', () => {
  121. expect( () => {
  122. view.register( 'x', true );
  123. view.register( new Region( 'x' ), true );
  124. } ).to.throwCKEditorError( /ui-view-register-override/ );
  125. } );
  126. it( 'should register a new region with region name as a first argument', () => {
  127. view.register( 'x', true );
  128. expect( view.regions.get( 'x' ) ).to.be.an.instanceof( Region );
  129. } );
  130. it( 'should register a new region with Region instance as a first argument', () => {
  131. view.register( new Region( 'y' ), true );
  132. expect( view.regions.get( 'y' ) ).to.be.an.instanceof( Region );
  133. } );
  134. it( 'should override an existing region with override flag', () => {
  135. view.template = new Template( {
  136. tag: 'div',
  137. children: [
  138. { tag: 'span' }
  139. ]
  140. } );
  141. const region1 = new Region( 'x' );
  142. const region2 = new Region( 'x' );
  143. view.register( region1, true );
  144. view.register( region2, true, true );
  145. view.register( 'x', 'span', true );
  146. view.init();
  147. expect( view.regions.get( 'x' ) ).to.equal( region2 );
  148. expect( view.regions.get( 'x' ).element ).to.equal( view.element.firstChild );
  149. } );
  150. it( 'should not override an existing region with the same region with override flag', () => {
  151. const region = new Region( 'x' );
  152. const spy = testUtils.sinon.spy( view.regions, 'remove' );
  153. view.register( region, true );
  154. view.register( region, true, true );
  155. sinon.assert.notCalled( spy );
  156. } );
  157. } );
  158. describe( 'element', () => {
  159. beforeEach( createViewInstanceWithTemplate );
  160. it( 'invokes out of #template', () => {
  161. setTestViewInstance();
  162. view.model.set( 'a', 1 );
  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. }