8
0

labeledinputview.js 6.0 KB

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