formheaderview.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import View from '../../src/view';
  6. import ViewCollection from '../../src/viewcollection';
  7. import FormHeaderView from '../../src/formheader/formheaderview';
  8. describe( 'FormHeaderView', () => {
  9. let view, locale;
  10. beforeEach( () => {
  11. locale = { t: val => val };
  12. view = new FormHeaderView( locale );
  13. view.render();
  14. } );
  15. afterEach( () => {
  16. if ( view.element ) {
  17. view.element.remove();
  18. }
  19. } );
  20. describe( 'constructor()', () => {
  21. it( 'should set view#locale', () => {
  22. expect( view.locale ).to.equal( locale );
  23. } );
  24. it( 'should create view#children collection', () => {
  25. expect( view.children ).to.be.instanceOf( ViewCollection );
  26. expect( view.children ).to.have.length( 0 );
  27. } );
  28. it( 'should contain label view only when view#label is defined', () => {
  29. view = new FormHeaderView( locale, {
  30. label: 'Foo label'
  31. } );
  32. view.render();
  33. expect( view.children ).to.be.instanceOf( ViewCollection );
  34. expect( view.children ).to.have.length( 1 );
  35. expect( view.children.first.element.classList.contains( 'ck-form__header__label' ) ).to.be.true;
  36. view.label = '';
  37. expect( view.children ).to.have.length( 0 );
  38. } );
  39. it( 'should set view#label', () => {
  40. expect( view.label ).to.equal( '' );
  41. } );
  42. it( 'should set view#class', () => {
  43. expect( view.class ).to.be.null;
  44. } );
  45. it( 'should set the template', () => {
  46. expect( view.element.classList.contains( 'ck' ) ).to.be.true;
  47. expect( view.element.classList.contains( 'ck-form__header' ) ).to.be.true;
  48. } );
  49. describe( 'options', () => {
  50. it( 'should set view#class when class was passed', () => {
  51. const view = new FormHeaderView( locale, {
  52. class: 'foo'
  53. } );
  54. expect( view.class ).to.equal( 'foo' );
  55. view.destroy();
  56. } );
  57. it( 'should use a label text when passed', () => {
  58. const view = new FormHeaderView( locale, {
  59. label: 'foo'
  60. } );
  61. view.render();
  62. expect( view.element.firstChild.classList.contains( 'ck' ) ).to.be.true;
  63. expect( view.element.firstChild.classList.contains( 'ck-form__header__label' ) ).to.be.true;
  64. expect( view.element.firstChild.textContent ).to.equal( 'foo' );
  65. view.destroy();
  66. } );
  67. } );
  68. describe( 'template bindings', () => {
  69. it( 'should bind #class to the template', () => {
  70. view.class = 'foo';
  71. expect( view.element.classList.contains( 'foo' ) ).to.be.true;
  72. } );
  73. it( 'should bind #label to the template', () => {
  74. view.label = 'bar';
  75. expect( view.element.firstChild.textContent ).to.equal( 'bar' );
  76. view.label = 'baz';
  77. expect( view.element.firstChild.textContent ).to.equal( 'baz' );
  78. } );
  79. it( 'should bind #children to the template', () => {
  80. const child = new View();
  81. child.setTemplate( { tag: 'div' } );
  82. view.children.add( child );
  83. expect( view.element.childNodes[ 0 ] ).to.equal( child.element );
  84. view.destroy();
  85. } );
  86. } );
  87. } );
  88. } );