labeledfieldview.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 LabeledFieldView from '../../src/labeledfield/labeledfieldview';
  7. import LabelView from '../../src/label/labelview';
  8. describe( 'LabeledFieldView', () => {
  9. const locale = {};
  10. let labeledField, fieldView;
  11. beforeEach( () => {
  12. labeledField = new LabeledFieldView( locale, ( labeledField, viewUid, statusUid ) => {
  13. fieldView = new View( locale );
  14. fieldView.setTemplate( { tag: 'div' } );
  15. fieldView.focus = () => {};
  16. fieldView.viewUid = viewUid;
  17. fieldView.statusUid = statusUid;
  18. return fieldView;
  19. } );
  20. labeledField.render();
  21. } );
  22. afterEach( () => {
  23. labeledField.destroy();
  24. } );
  25. describe( 'constructor()', () => {
  26. it( 'should set labeledField#locale', () => {
  27. expect( labeledField.locale ).to.deep.equal( locale );
  28. } );
  29. it( 'should set labeledField#fieldView', () => {
  30. expect( labeledField.fieldView ).to.equal( fieldView );
  31. } );
  32. it( 'should set labeledField#label', () => {
  33. expect( labeledField.label ).to.be.undefined;
  34. } );
  35. it( 'should set labeledField#isEnabled', () => {
  36. expect( labeledField.isEnabled ).to.be.true;
  37. } );
  38. it( 'should set labeledField#errorText', () => {
  39. expect( labeledField.errorText ).to.be.null;
  40. } );
  41. it( 'should set labeledField#infoText', () => {
  42. expect( labeledField.infoText ).to.be.null;
  43. } );
  44. it( 'should set labeledField#class', () => {
  45. expect( labeledField.class ).to.be.undefined;
  46. } );
  47. it( 'should set labeledField#isEmpty', () => {
  48. expect( labeledField.isEmpty ).to.be.true;
  49. } );
  50. it( 'should set labeledField#isFocused', () => {
  51. expect( labeledField.isFocused ).to.be.false;
  52. } );
  53. it( 'should create labeledField#labelView', () => {
  54. expect( labeledField.labelView ).to.instanceOf( LabelView );
  55. } );
  56. it( 'should create labeledField#statusView', () => {
  57. expect( labeledField.statusView ).to.instanceOf( View );
  58. expect( labeledField.statusView.element.tagName ).to.equal( 'DIV' );
  59. expect( labeledField.statusView.element.classList.contains( 'ck' ) ).to.be.true;
  60. expect( labeledField.statusView.element.classList.contains( 'ck-labeled-field-view__status' ) ).to.be.true;
  61. } );
  62. it( 'should allow pairing #view and #labelView by unique id', () => {
  63. expect( labeledField.labelView.for ).to.equal( fieldView.viewUid );
  64. } );
  65. it( 'should allow pairing #view and #statusView by unique id', () => {
  66. expect( fieldView.statusUid ).to.equal( labeledField.statusView.element.id );
  67. } );
  68. } );
  69. describe( 'template', () => {
  70. it( 'should have the CSS class', () => {
  71. expect( labeledField.element.classList.contains( 'ck' ) ).to.be.true;
  72. expect( labeledField.element.classList.contains( 'ck-labeled-field-view' ) ).to.be.true;
  73. } );
  74. it( 'should have a wrapper for internals', () => {
  75. expect( labeledField.element.firstChild.classList.contains( 'ck' ) ).to.be.true;
  76. expect( labeledField.element.firstChild.classList.contains( 'ck-labeled-field-view__input-wrapper' ) ).to.be.true;
  77. } );
  78. it( 'should have #fieldView', () => {
  79. expect( labeledField.template.children[ 0 ].children[ 0 ] ).to.equal( fieldView );
  80. } );
  81. it( 'should have #labelView', () => {
  82. expect( labeledField.template.children[ 0 ].children[ 1 ] ).to.equal( labeledField.labelView );
  83. } );
  84. it( 'should have the #statusView container', () => {
  85. expect( labeledField.template.children[ 1 ] ).to.equal( labeledField.statusView );
  86. } );
  87. describe( 'DOM bindings', () => {
  88. describe( 'class', () => {
  89. it( 'should react on labeledField#class', () => {
  90. labeledField.class = 'foo';
  91. expect( labeledField.element.classList.contains( 'foo' ) ).to.be.true;
  92. labeledField.class = 'bar';
  93. expect( labeledField.element.classList.contains( 'foo' ) ).to.be.false;
  94. expect( labeledField.element.classList.contains( 'bar' ) ).to.be.true;
  95. } );
  96. it( 'should react on labeledField#isEnabled', () => {
  97. labeledField.isEnabled = true;
  98. expect( labeledField.element.classList.contains( 'ck-disabled' ) ).to.be.false;
  99. labeledField.isEnabled = false;
  100. expect( labeledField.element.classList.contains( 'ck-disabled' ) ).to.be.true;
  101. } );
  102. it( 'should react on labeledField#isEmpty', () => {
  103. labeledField.isEmpty = true;
  104. expect( labeledField.element.classList.contains( 'ck-labeled-field-view_empty' ) ).to.be.true;
  105. labeledField.isEmpty = false;
  106. expect( labeledField.element.classList.contains( 'ck-labeled-field-view_empty' ) ).to.be.false;
  107. } );
  108. it( 'should react on labeledField#isFocused', () => {
  109. labeledField.isFocused = true;
  110. expect( labeledField.element.classList.contains( 'ck-labeled-field-view_focused' ) ).to.be.true;
  111. labeledField.isFocused = false;
  112. expect( labeledField.element.classList.contains( 'ck-labeled-field-view_focused' ) ).to.be.false;
  113. } );
  114. it( 'should react on labeledField#placeholder', () => {
  115. labeledField.placeholder = 'asd';
  116. expect( labeledField.element.classList.contains( 'ck-labeled-field-view_placeholder' ) ).to.be.true;
  117. labeledField.placeholder = null;
  118. expect( labeledField.element.classList.contains( 'ck-labeled-field-view_placeholder' ) ).to.be.false;
  119. } );
  120. } );
  121. describe( 'status container', () => {
  122. it( 'should react on labeledField#errorText', () => {
  123. const statusElement = labeledField.statusView.element;
  124. labeledField.errorText = '';
  125. expect( statusElement.classList.contains( 'ck-hidden' ) ).to.be.true;
  126. expect( statusElement.classList.contains( 'ck-labeled-field-view__status_error' ) ).to.be.false;
  127. expect( statusElement.hasAttribute( 'role' ) ).to.be.false;
  128. expect( statusElement.innerHTML ).to.equal( '' );
  129. labeledField.errorText = 'foo';
  130. expect( statusElement.classList.contains( 'ck-hidden' ) ).to.be.false;
  131. expect( statusElement.classList.contains( 'ck-labeled-field-view__status_error' ) ).to.be.true;
  132. expect( statusElement.getAttribute( 'role' ) ).to.equal( 'alert' );
  133. expect( statusElement.innerHTML ).to.equal( 'foo' );
  134. } );
  135. it( 'should react on labeledField#infoText', () => {
  136. const statusElement = labeledField.statusView.element;
  137. labeledField.infoText = '';
  138. expect( statusElement.classList.contains( 'ck-hidden' ) ).to.be.true;
  139. expect( statusElement.classList.contains( 'ck-labeled-field-view__status_error' ) ).to.be.false;
  140. expect( statusElement.hasAttribute( 'role' ) ).to.be.false;
  141. expect( statusElement.innerHTML ).to.equal( '' );
  142. labeledField.infoText = 'foo';
  143. expect( statusElement.classList.contains( 'ck-hidden' ) ).to.be.false;
  144. expect( statusElement.classList.contains( 'ck-labeled-field-view__status_error' ) ).to.be.false;
  145. expect( statusElement.hasAttribute( 'role' ) ).to.be.false;
  146. expect( statusElement.innerHTML ).to.equal( 'foo' );
  147. } );
  148. } );
  149. } );
  150. } );
  151. describe( 'binding', () => {
  152. it( 'should bind labeledField#label to labeledField.labelView#label', () => {
  153. labeledField.label = 'Foo bar';
  154. expect( labeledField.labelView.text ).to.equal( 'Foo bar' );
  155. } );
  156. } );
  157. describe( 'focus()', () => {
  158. it( 'should focus the #view in DOM', () => {
  159. const spy = sinon.spy( fieldView, 'focus' );
  160. labeledField.focus();
  161. sinon.assert.calledOnce( spy );
  162. } );
  163. } );
  164. } );