inputtextview.js 5.2 KB

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