labeledinputview.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import LabeledInputView from '../../src/labeledinput/labeledinputview';
  6. import InputView from '../../src/inputtext/inputtextview';
  7. import LabelView from '../../src/label/labelview';
  8. describe( 'LabeledInputView', () => {
  9. const locale = {};
  10. let view;
  11. beforeEach( () => {
  12. view = new LabeledInputView( locale, InputView );
  13. view.render();
  14. } );
  15. describe( 'constructor()', () => {
  16. it( 'should set view#locale', () => {
  17. expect( view.locale ).to.deep.equal( locale );
  18. } );
  19. it( 'should set view#errorText', () => {
  20. expect( view.errorText ).to.be.null;
  21. } );
  22. it( 'should create view#inputView', () => {
  23. expect( view.inputView ).to.instanceOf( InputView );
  24. } );
  25. it( 'should create view#labelView', () => {
  26. expect( view.labelView ).to.instanceOf( LabelView );
  27. } );
  28. it( 'should pair inputView and labelView by unique id', () => {
  29. expect( view.labelView.for ).to.equal( view.inputView.id ).to.ok;
  30. } );
  31. } );
  32. describe( 'template', () => {
  33. it( 'should have the CSS class', () => {
  34. expect( view.element.classList.contains( 'ck' ) ).to.be.true;
  35. expect( view.element.classList.contains( 'ck-labeled-input' ) ).to.be.true;
  36. } );
  37. it( 'should have label view', () => {
  38. expect( view.template.children[ 0 ] ).to.equal( view.labelView );
  39. } );
  40. it( 'should have input view', () => {
  41. expect( view.template.children[ 1 ] ).to.equal( view.inputView );
  42. } );
  43. it( 'should have the error container', () => {
  44. const errorContainer = view.element.lastChild;
  45. expect( errorContainer.tagName ).to.equal( 'DIV' );
  46. expect( errorContainer.classList.contains( 'ck' ) ).to.be.true;
  47. expect( errorContainer.classList.contains( 'ck-labeled-input__error' ) ).to.be.true;
  48. } );
  49. describe( 'DOM bindings', () => {
  50. describe( 'class', () => {
  51. it( 'should react on view#isReadOnly', () => {
  52. view.isReadOnly = false;
  53. expect( view.element.classList.contains( 'ck-disabled' ) ).to.be.false;
  54. view.isReadOnly = true;
  55. expect( view.element.classList.contains( 'ck-disabled' ) ).to.be.true;
  56. } );
  57. } );
  58. describe( 'error container', () => {
  59. it( 'should react on view#errorText', () => {
  60. const errorContainer = view.element.lastChild;
  61. view.errorText = '';
  62. expect( errorContainer.classList.contains( 'ck-hidden' ) ).to.be.true;
  63. expect( errorContainer.innerHTML ).to.equal( '' );
  64. view.errorText = 'foo';
  65. expect( errorContainer.classList.contains( 'ck-hidden' ) ).to.be.false;
  66. expect( errorContainer.innerHTML ).to.equal( 'foo' );
  67. } );
  68. } );
  69. } );
  70. } );
  71. describe( 'binding', () => {
  72. it( 'should bind view#text to view.labelView#label', () => {
  73. view.label = 'Foo bar';
  74. expect( view.labelView.text ).to.equal( 'Foo bar' );
  75. } );
  76. it( 'should bind view#value to view.inputView#value', () => {
  77. view.value = 'Lorem ipsum';
  78. expect( view.inputView.value ).to.equal( 'Lorem ipsum' );
  79. } );
  80. it( 'should bind view#isreadOnly to view.inputView#isReadOnly', () => {
  81. view.isReadOnly = false;
  82. expect( view.inputView.isReadOnly ).to.be.false;
  83. view.isReadOnly = true;
  84. expect( view.inputView.isReadOnly ).to.be.true;
  85. } );
  86. it( 'should bind view#errorText to view.inputView#hasError', () => {
  87. view.errorText = '';
  88. expect( view.inputView.hasError ).to.be.false;
  89. view.errorText = 'foo';
  90. expect( view.inputView.hasError ).to.be.true;
  91. } );
  92. it( 'should clear view#errorText upon view.inputView#input', () => {
  93. view.errorText = 'foo';
  94. view.inputView.fire( 'input' );
  95. expect( view.errorText ).to.be.null;
  96. } );
  97. } );
  98. describe( 'select()', () => {
  99. it( 'should select input value', () => {
  100. const spy = sinon.spy( view.inputView, 'select' );
  101. view.select();
  102. sinon.assert.calledOnce( spy );
  103. } );
  104. } );
  105. describe( 'focus()', () => {
  106. it( 'focuses the input in DOM', () => {
  107. const spy = sinon.spy( view.inputView, 'focus' );
  108. view.focus();
  109. sinon.assert.calledOnce( spy );
  110. } );
  111. } );
  112. } );