8
0

utils.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 {
  6. createLabeledInputText,
  7. createLabeledDropdown
  8. } from '../../src/labeledfield/utils';
  9. import LabeledFieldView from '../../src/labeledfield/labeledfieldview';
  10. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  11. import InputTextView from '../../src/inputtext/inputtextview';
  12. import DropdownView from '../../src/dropdown/dropdownview';
  13. describe( 'LabeledFieldView utils', () => {
  14. let locale;
  15. testUtils.createSinonSandbox();
  16. beforeEach( () => {
  17. locale = { t: val => val };
  18. } );
  19. describe( 'createLabeledInputText()', () => {
  20. let labeledInput;
  21. beforeEach( () => {
  22. labeledInput = new LabeledFieldView( locale, createLabeledInputText );
  23. } );
  24. afterEach( () => {
  25. labeledInput.destroy();
  26. } );
  27. it( 'should create an InputTextView instance', () => {
  28. expect( labeledInput.fieldView ).to.be.instanceOf( InputTextView );
  29. } );
  30. it( 'should pass the Locale to the input', () => {
  31. expect( labeledInput.fieldView.locale ).to.equal( locale );
  32. } );
  33. it( 'should set input #id and #ariaDescribedById', () => {
  34. labeledInput.render();
  35. expect( labeledInput.fieldView.id ).to.equal( labeledInput.labelView.for );
  36. expect( labeledInput.fieldView.ariaDescribedById ).to.equal( labeledInput.statusView.element.id );
  37. } );
  38. it( 'should bind input\'s #isReadOnly to labeledInput#isEnabled', () => {
  39. labeledInput.isEnabled = true;
  40. expect( labeledInput.fieldView.isReadOnly ).to.be.false;
  41. labeledInput.isEnabled = false;
  42. expect( labeledInput.fieldView.isReadOnly ).to.be.true;
  43. } );
  44. it( 'should bind input\'s #hasError to labeledInput#errorText', () => {
  45. labeledInput.errorText = 'some error';
  46. expect( labeledInput.fieldView.hasError ).to.be.true;
  47. labeledInput.errorText = null;
  48. expect( labeledInput.fieldView.hasError ).to.be.false;
  49. } );
  50. it( 'should bind labeledInput#isEmpty to input\'s #isEmpty', () => {
  51. labeledInput.fieldView.isEmpty = true;
  52. expect( labeledInput.isEmpty ).to.be.true;
  53. labeledInput.fieldView.isEmpty = false;
  54. expect( labeledInput.isEmpty ).to.be.false;
  55. } );
  56. it( 'should bind labeledInput#isFocused to input\'s #isFocused', () => {
  57. labeledInput.fieldView.isFocused = true;
  58. expect( labeledInput.isFocused ).to.be.true;
  59. labeledInput.fieldView.isFocused = false;
  60. expect( labeledInput.isFocused ).to.be.false;
  61. } );
  62. it( 'should bind labeledInput#placeholder to input\'s #placeholder', () => {
  63. labeledInput.fieldView.placeholder = null;
  64. expect( labeledInput.placeholder ).to.be.null;
  65. labeledInput.fieldView.placeholder = 'foo';
  66. expect( labeledInput.placeholder ).to.equal( 'foo' );
  67. } );
  68. it( 'should clean labeledInput#errorText upon input\'s DOM "update" event', () => {
  69. labeledInput.errorText = 'some error';
  70. labeledInput.fieldView.fire( 'input' );
  71. expect( labeledInput.errorText ).to.be.null;
  72. } );
  73. } );
  74. describe( 'createLabeledDropdown', () => {
  75. let labeledDropdown;
  76. beforeEach( () => {
  77. labeledDropdown = new LabeledFieldView( locale, createLabeledDropdown );
  78. } );
  79. afterEach( () => {
  80. labeledDropdown.destroy();
  81. } );
  82. it( 'should create a DropdownView', () => {
  83. expect( labeledDropdown.fieldView ).to.be.instanceOf( DropdownView );
  84. } );
  85. it( 'should pass the Locale to the dropdown', () => {
  86. expect( labeledDropdown.fieldView.locale ).to.equal( locale );
  87. } );
  88. it( 'should set dropdown\'s #id and #ariaDescribedById', () => {
  89. labeledDropdown.render();
  90. expect( labeledDropdown.fieldView.id ).to.equal( labeledDropdown.labelView.for );
  91. expect( labeledDropdown.fieldView.ariaDescribedById ).to.equal( labeledDropdown.statusView.element.id );
  92. } );
  93. it( 'should bind dropdown\'s #isEnabled to the labeled view', () => {
  94. labeledDropdown.isEnabled = true;
  95. expect( labeledDropdown.fieldView.isEnabled ).to.be.true;
  96. labeledDropdown.isEnabled = false;
  97. expect( labeledDropdown.fieldView.isEnabled ).to.be.false;
  98. } );
  99. } );
  100. } );