inputtextview.js 3.4 KB

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