labeledfieldview.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. import View from '../../src/view';
  6. import LabeledFieldView from '../../src/labeledfield/labeledfieldview';
  7. import LabelView from '../../src/label/labelview';
  8. describe( 'LabeledFieldView', () => {
  9. const locale = {};
  10. let labeledInput, view;
  11. beforeEach( () => {
  12. labeledInput = new LabeledFieldView( locale, ( labeledInput, viewUid, statusUid ) => {
  13. view = new View( locale );
  14. view.setTemplate( { tag: 'div' } );
  15. view.focus = () => {};
  16. view.viewUid = viewUid;
  17. view.statusUid = statusUid;
  18. return view;
  19. } );
  20. labeledInput.render();
  21. } );
  22. afterEach( () => {
  23. labeledInput.destroy();
  24. } );
  25. describe( 'constructor()', () => {
  26. it( 'should set labeledInput#locale', () => {
  27. expect( labeledInput.locale ).to.deep.equal( locale );
  28. } );
  29. it( 'should set labeledInput#fieldView', () => {
  30. expect( labeledInput.fieldView ).to.equal( view );
  31. } );
  32. it( 'should set labeledInput#label', () => {
  33. expect( labeledInput.label ).to.be.undefined;
  34. } );
  35. it( 'should set labeledInput#isEnabled', () => {
  36. expect( labeledInput.isEnabled ).to.be.true;
  37. } );
  38. it( 'should set labeledInput#errorText', () => {
  39. expect( labeledInput.errorText ).to.be.null;
  40. } );
  41. it( 'should set labeledInput#infoText', () => {
  42. expect( labeledInput.infoText ).to.be.null;
  43. } );
  44. it( 'should set labeledInput#class', () => {
  45. expect( labeledInput.class ).to.be.undefined;
  46. } );
  47. it( 'should create labeledInput#labelView', () => {
  48. expect( labeledInput.labelView ).to.instanceOf( LabelView );
  49. } );
  50. it( 'should create labeledInput#statusView', () => {
  51. expect( labeledInput.statusView ).to.instanceOf( View );
  52. expect( labeledInput.statusView.element.tagName ).to.equal( 'DIV' );
  53. expect( labeledInput.statusView.element.classList.contains( 'ck' ) ).to.be.true;
  54. expect( labeledInput.statusView.element.classList.contains( 'ck-labeled-field-view__status' ) ).to.be.true;
  55. } );
  56. it( 'should allow pairing #view and #labelView by unique id', () => {
  57. expect( labeledInput.labelView.for ).to.equal( view.viewUid );
  58. } );
  59. it( 'should allow pairing #view and #statusView by unique id', () => {
  60. expect( view.statusUid ).to.equal( labeledInput.statusView.element.id );
  61. } );
  62. } );
  63. describe( 'template', () => {
  64. it( 'should have the CSS class', () => {
  65. expect( labeledInput.element.classList.contains( 'ck' ) ).to.be.true;
  66. expect( labeledInput.element.classList.contains( 'ck-labeled-field-view' ) ).to.be.true;
  67. } );
  68. it( 'should have #labeledInput', () => {
  69. expect( labeledInput.template.children[ 0 ] ).to.equal( labeledInput.labelView );
  70. } );
  71. it( 'should have #view', () => {
  72. expect( labeledInput.template.children[ 1 ] ).to.equal( view );
  73. } );
  74. it( 'should have the #statusView container', () => {
  75. expect( labeledInput.template.children[ 2 ] ).to.equal( labeledInput.statusView );
  76. } );
  77. describe( 'DOM bindings', () => {
  78. describe( 'class', () => {
  79. it( 'should react on labeledInput#class', () => {
  80. labeledInput.class = 'foo';
  81. expect( labeledInput.element.classList.contains( 'foo' ) ).to.be.true;
  82. labeledInput.class = 'bar';
  83. expect( labeledInput.element.classList.contains( 'foo' ) ).to.be.false;
  84. expect( labeledInput.element.classList.contains( 'bar' ) ).to.be.true;
  85. } );
  86. } );
  87. describe( 'status container', () => {
  88. it( 'should react on labeledInput#errorText', () => {
  89. const statusElement = labeledInput.statusView.element;
  90. labeledInput.errorText = '';
  91. expect( statusElement.classList.contains( 'ck-hidden' ) ).to.be.true;
  92. expect( statusElement.classList.contains( 'ck-labeled-field-view__status_error' ) ).to.be.false;
  93. expect( statusElement.hasAttribute( 'role' ) ).to.be.false;
  94. expect( statusElement.innerHTML ).to.equal( '' );
  95. labeledInput.errorText = 'foo';
  96. expect( statusElement.classList.contains( 'ck-hidden' ) ).to.be.false;
  97. expect( statusElement.classList.contains( 'ck-labeled-field-view__status_error' ) ).to.be.true;
  98. expect( statusElement.getAttribute( 'role' ) ).to.equal( 'alert' );
  99. expect( statusElement.innerHTML ).to.equal( 'foo' );
  100. } );
  101. it( 'should react on labeledInput#infoText', () => {
  102. const statusElement = labeledInput.statusView.element;
  103. labeledInput.infoText = '';
  104. expect( statusElement.classList.contains( 'ck-hidden' ) ).to.be.true;
  105. expect( statusElement.classList.contains( 'ck-labeled-field-view__status_error' ) ).to.be.false;
  106. expect( statusElement.hasAttribute( 'role' ) ).to.be.false;
  107. expect( statusElement.innerHTML ).to.equal( '' );
  108. labeledInput.infoText = 'foo';
  109. expect( statusElement.classList.contains( 'ck-hidden' ) ).to.be.false;
  110. expect( statusElement.classList.contains( 'ck-labeled-field-view__status_error' ) ).to.be.false;
  111. expect( statusElement.hasAttribute( 'role' ) ).to.be.false;
  112. expect( statusElement.innerHTML ).to.equal( 'foo' );
  113. } );
  114. } );
  115. } );
  116. } );
  117. describe( 'binding', () => {
  118. it( 'should bind labeledInput#label to labeledInput.labelView#label', () => {
  119. labeledInput.label = 'Foo bar';
  120. expect( labeledInput.labelView.text ).to.equal( 'Foo bar' );
  121. } );
  122. } );
  123. describe( 'focus()', () => {
  124. it( 'should focus the #view in DOM', () => {
  125. const spy = sinon.spy( view, 'focus' );
  126. labeledInput.focus();
  127. sinon.assert.calledOnce( spy );
  128. } );
  129. } );
  130. } );