8
0

labeledinputview.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. /* globals console */
  6. import View from '../../src/view';
  7. import LabeledInputView from '../../src/labeledinput/labeledinputview';
  8. import InputView from '../../src/inputtext/inputtextview';
  9. import LabelView from '../../src/label/labelview';
  10. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  11. describe( 'LabeledInputView', () => {
  12. const locale = {};
  13. testUtils.createSinonSandbox();
  14. let view, deprecatedWarning;
  15. beforeEach( () => {
  16. deprecatedWarning = testUtils.sinon.stub( console, 'warn' );
  17. view = new LabeledInputView( locale, InputView );
  18. view.render();
  19. } );
  20. afterEach( () => {
  21. sinon.restore();
  22. } );
  23. describe( 'constructor()', () => {
  24. describe( 'deprecation warning', () => {
  25. it( 'should be shown in the console after component initialization', () => {
  26. expect( deprecatedWarning.called ).to.be.true;
  27. expect( deprecatedWarning.callCount ).to.be.equal( 1 );
  28. } );
  29. it( 'should inform about using LabeledFieldView instead of LabeledInputView', () => {
  30. expect( deprecatedWarning.calledWithMatch( 'Please use LabeledFieldView component instead' ) ).to.be.true;
  31. } );
  32. } );
  33. it( 'should set view#locale', () => {
  34. expect( view.locale ).to.deep.equal( locale );
  35. } );
  36. it( 'should set view#errorText', () => {
  37. expect( view.errorText ).to.be.null;
  38. } );
  39. it( 'should set view#infoText', () => {
  40. expect( view.infoText ).to.be.null;
  41. } );
  42. it( 'should create view#inputView', () => {
  43. expect( view.inputView ).to.instanceOf( InputView );
  44. } );
  45. it( 'should create view#labelView', () => {
  46. expect( view.labelView ).to.instanceOf( LabelView );
  47. } );
  48. it( 'should create view#statusView', () => {
  49. expect( view.statusView ).to.instanceOf( View );
  50. expect( view.statusView.element.tagName ).to.equal( 'DIV' );
  51. expect( view.statusView.element.classList.contains( 'ck' ) ).to.be.true;
  52. expect( view.statusView.element.classList.contains( 'ck-labeled-input__status' ) ).to.be.true;
  53. } );
  54. it( 'should pair #inputView and #labelView by unique id', () => {
  55. expect( view.labelView.for ).to.equal( view.inputView.id );
  56. } );
  57. it( 'should pair #inputView and #statusView by unique id', () => {
  58. expect( view.inputView.ariaDescribedById ).to.equal( view.statusView.element.id );
  59. } );
  60. } );
  61. describe( 'template', () => {
  62. it( 'should have the CSS class', () => {
  63. expect( view.element.classList.contains( 'ck' ) ).to.be.true;
  64. expect( view.element.classList.contains( 'ck-labeled-input' ) ).to.be.true;
  65. } );
  66. it( 'should have label view', () => {
  67. expect( view.template.children[ 0 ] ).to.equal( view.labelView );
  68. } );
  69. it( 'should have input view', () => {
  70. expect( view.template.children[ 1 ] ).to.equal( view.inputView );
  71. } );
  72. it( 'should have the status container', () => {
  73. expect( view.template.children[ 2 ] ).to.equal( view.statusView );
  74. } );
  75. describe( 'DOM bindings', () => {
  76. describe( 'class', () => {
  77. it( 'should react on view#isReadOnly', () => {
  78. view.isReadOnly = false;
  79. expect( view.element.classList.contains( 'ck-disabled' ) ).to.be.false;
  80. view.isReadOnly = true;
  81. expect( view.element.classList.contains( 'ck-disabled' ) ).to.be.true;
  82. } );
  83. } );
  84. describe( 'status container', () => {
  85. it( 'should react on view#errorText', () => {
  86. const statusElement = view.statusView.element;
  87. view.errorText = '';
  88. expect( statusElement.classList.contains( 'ck-hidden' ) ).to.be.true;
  89. expect( statusElement.classList.contains( 'ck-labeled-input__status_error' ) ).to.be.false;
  90. expect( statusElement.hasAttribute( 'role' ) ).to.be.false;
  91. expect( statusElement.innerHTML ).to.equal( '' );
  92. view.errorText = 'foo';
  93. expect( statusElement.classList.contains( 'ck-hidden' ) ).to.be.false;
  94. expect( statusElement.classList.contains( 'ck-labeled-input__status_error' ) ).to.be.true;
  95. expect( statusElement.getAttribute( 'role' ) ).to.equal( 'alert' );
  96. expect( statusElement.innerHTML ).to.equal( 'foo' );
  97. } );
  98. it( 'should react on view#infoText', () => {
  99. const statusElement = view.statusView.element;
  100. view.infoText = '';
  101. expect( statusElement.classList.contains( 'ck-hidden' ) ).to.be.true;
  102. expect( statusElement.classList.contains( 'ck-labeled-input__status_error' ) ).to.be.false;
  103. expect( statusElement.hasAttribute( 'role' ) ).to.be.false;
  104. expect( statusElement.innerHTML ).to.equal( '' );
  105. view.infoText = 'foo';
  106. expect( statusElement.classList.contains( 'ck-hidden' ) ).to.be.false;
  107. expect( statusElement.classList.contains( 'ck-labeled-input__status_error' ) ).to.be.false;
  108. expect( statusElement.hasAttribute( 'role' ) ).to.be.false;
  109. expect( statusElement.innerHTML ).to.equal( 'foo' );
  110. } );
  111. } );
  112. } );
  113. } );
  114. describe( 'binding', () => {
  115. it( 'should bind view#text to view.labelView#label', () => {
  116. view.label = 'Foo bar';
  117. expect( view.labelView.text ).to.equal( 'Foo bar' );
  118. } );
  119. it( 'should bind view#value to view.inputView#value', () => {
  120. view.value = 'Lorem ipsum';
  121. expect( view.inputView.value ).to.equal( 'Lorem ipsum' );
  122. } );
  123. it( 'should bind view#isreadOnly to view.inputView#isReadOnly', () => {
  124. view.isReadOnly = false;
  125. expect( view.inputView.isReadOnly ).to.be.false;
  126. view.isReadOnly = true;
  127. expect( view.inputView.isReadOnly ).to.be.true;
  128. } );
  129. it( 'should bind view#errorText to view.inputView#hasError', () => {
  130. view.errorText = '';
  131. expect( view.inputView.hasError ).to.be.false;
  132. view.errorText = 'foo';
  133. expect( view.inputView.hasError ).to.be.true;
  134. } );
  135. it( 'should clear view#errorText upon view.inputView#input', () => {
  136. view.errorText = 'foo';
  137. view.inputView.fire( 'input' );
  138. expect( view.errorText ).to.be.null;
  139. } );
  140. } );
  141. describe( 'select()', () => {
  142. it( 'should select input value', () => {
  143. const spy = sinon.spy( view.inputView, 'select' );
  144. view.select();
  145. sinon.assert.calledOnce( spy );
  146. } );
  147. } );
  148. describe( 'focus()', () => {
  149. it( 'focuses the input in DOM', () => {
  150. const spy = sinon.spy( view.inputView, 'focus' );
  151. view.focus();
  152. sinon.assert.calledOnce( spy );
  153. } );
  154. } );
  155. } );