8
0

inputtextview.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. /* global Event */
  6. import InputTextView from '../../src/inputtext/inputtextview';
  7. describe( 'InputTextView', () => {
  8. let view, ariaDescribedById;
  9. beforeEach( () => {
  10. ariaDescribedById = 'ck-error-1234567890';
  11. view = new InputTextView();
  12. view.render();
  13. } );
  14. afterEach( () => {
  15. view.destroy();
  16. } );
  17. describe( 'constructor()', () => {
  18. it( 'should creates element from template', () => {
  19. expect( view.element.tagName ).to.equal( 'INPUT' );
  20. expect( view.element.type ).to.equal( 'text' );
  21. expect( view.element.classList.contains( 'ck' ) ).to.be.true;
  22. expect( view.element.classList.contains( 'ck-input' ) ).to.be.true;
  23. expect( view.element.classList.contains( 'ck-input-text' ) ).to.be.true;
  24. } );
  25. } );
  26. describe( 'DOM bindings', () => {
  27. beforeEach( () => {
  28. view.value = 'foo';
  29. view.id = 'bar';
  30. } );
  31. describe( 'value', () => {
  32. it( 'should react on view#value', () => {
  33. expect( view.element.value ).to.equal( 'foo' );
  34. view.value = 'baz';
  35. expect( view.element.value ).to.equal( 'baz' );
  36. // To be sure that value can be changed multiple times using inline value attribute.
  37. // There was a related bug in Chrome.
  38. view.value = 'biz';
  39. expect( view.element.value ).to.equal( 'biz' );
  40. } );
  41. it( 'should set to empty string when using `falsy` values', () => {
  42. [ undefined, false, null ].forEach( value => {
  43. view.value = value;
  44. expect( view.element.value ).to.equal( '' );
  45. } );
  46. } );
  47. // See ckeditor5-ui/issues/335.
  48. it( 'should set element value when value was defined before view#render', () => {
  49. view = new InputTextView();
  50. view.value = 'baz';
  51. view.render();
  52. expect( view.element.value ).to.equal( 'baz' );
  53. } );
  54. } );
  55. describe( 'id', () => {
  56. it( 'should react on view#id', () => {
  57. expect( view.element.id ).to.equal( 'bar' );
  58. view.id = 'baz';
  59. expect( view.element.id ).to.equal( 'baz' );
  60. } );
  61. } );
  62. describe( 'placeholder', () => {
  63. it( 'should react on view#placeholder', () => {
  64. expect( view.element.placeholder ).to.equal( '' );
  65. view.placeholder = 'baz';
  66. expect( view.element.placeholder ).to.equal( 'baz' );
  67. } );
  68. } );
  69. describe( 'isReadOnly', () => {
  70. it( 'should react on view#isReadOnly', () => {
  71. expect( view.element.readOnly ).to.false;
  72. view.isReadOnly = true;
  73. expect( view.element.readOnly ).to.true;
  74. } );
  75. } );
  76. describe( 'class', () => {
  77. it( 'should react on view#hasErrors', () => {
  78. expect( view.element.classList.contains( 'ck-error' ) ).to.be.false;
  79. view.hasError = true;
  80. expect( view.element.classList.contains( 'ck-error' ) ).to.be.true;
  81. } );
  82. } );
  83. describe( 'aria-invalid', () => {
  84. it( 'should react on view#hasError', () => {
  85. expect( view.element.getAttribute( 'aria-invalid' ) ).to.be.null;
  86. view.hasError = true;
  87. expect( view.element.getAttribute( 'aria-invalid' ) ).to.equal( 'true' );
  88. } );
  89. } );
  90. describe( 'aria-describedby', () => {
  91. it( 'should react on view#hasError', () => {
  92. expect( view.element.getAttribute( 'aria-describedby' ) ).to.be.null;
  93. view.ariaDescribedById = ariaDescribedById;
  94. expect( view.element.getAttribute( 'aria-describedby' ) ).to.equal( ariaDescribedById );
  95. } );
  96. } );
  97. describe( 'input event', () => {
  98. it( 'triggers view#input', () => {
  99. const spy = sinon.spy();
  100. view.on( 'input', spy );
  101. view.element.dispatchEvent( new Event( 'input' ) );
  102. sinon.assert.calledOnce( spy );
  103. } );
  104. } );
  105. } );
  106. describe( 'select()', () => {
  107. it( 'should select input value', () => {
  108. const selectSpy = sinon.spy( view.element, 'select' );
  109. view.select();
  110. expect( selectSpy.calledOnce ).to.true;
  111. selectSpy.restore();
  112. } );
  113. } );
  114. describe( 'focus()', () => {
  115. it( 'focuses the input in DOM', () => {
  116. const spy = sinon.spy( view.element, 'focus' );
  117. view.focus();
  118. sinon.assert.calledOnce( spy );
  119. } );
  120. } );
  121. } );