labeledinputview.js 6.1 KB

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