labeledinputview.js 4.3 KB

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