labeledview.js 5.3 KB

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