listitemview.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global Event */
  6. import ListItemView from '../../src/list/listitemview';
  7. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  8. describe( 'ListItemView', () => {
  9. let view;
  10. beforeEach( () => {
  11. view = new ListItemView();
  12. view.set( {
  13. style: 'foo',
  14. label: 'bar'
  15. } );
  16. return view.render();
  17. } );
  18. describe( 'constructor()', () => {
  19. it( 'creates element from template', () => {
  20. expect( view.element.classList.contains( 'ck-list__item' ) ).to.be.true;
  21. } );
  22. it( 'should create #keystrokes instance', () => {
  23. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  24. } );
  25. } );
  26. describe( 'render()', () => {
  27. it( 'starts listening for #keystrokes coming from #element', () => {
  28. view = new ListItemView();
  29. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  30. view.render();
  31. sinon.assert.calledOnce( spy );
  32. sinon.assert.calledWithExactly( spy, view.element );
  33. } );
  34. // https://github.com/ckeditor/ckeditor5-ui/issues/153
  35. it( 'triggers view#execute event when Enter or Space key is pressed', () => {
  36. const spy = sinon.spy();
  37. const evtData = {
  38. keyCode: 10,
  39. preventDefault: sinon.spy(),
  40. stopPropagation: sinon.spy()
  41. };
  42. view.on( 'execute', spy );
  43. view.keystrokes.press( evtData );
  44. sinon.assert.notCalled( spy );
  45. sinon.assert.notCalled( evtData.preventDefault );
  46. evtData.keyCode = 13;
  47. view.keystrokes.press( evtData );
  48. sinon.assert.calledOnce( spy );
  49. sinon.assert.calledOnce( evtData.preventDefault );
  50. evtData.keyCode = 32;
  51. view.keystrokes.press( evtData );
  52. sinon.assert.calledTwice( spy );
  53. sinon.assert.calledTwice( evtData.preventDefault );
  54. } );
  55. } );
  56. describe( 'DOM bindings', () => {
  57. describe( '"class" attribute', () => {
  58. it( 'reacts on view#class', () => {
  59. expect( view.element.classList ).to.have.length( 1 );
  60. view.set( 'class', 'foo' );
  61. expect( view.element.classList.contains( 'foo' ) ).to.be.true;
  62. } );
  63. it( 'reacts on view#isActive', () => {
  64. expect( view.element.classList ).to.have.length( 1 );
  65. view.set( 'isActive', true );
  66. expect( view.element.classList.contains( 'ck-list__item_active' ) ).to.be.true;
  67. } );
  68. } );
  69. describe( '"style" attribute', () => {
  70. it( 'reacts on view#style', () => {
  71. expect( view.element.attributes.getNamedItem( 'style' ).value ).to.equal( 'foo' );
  72. view.style = 'color: red';
  73. expect( view.element.attributes.getNamedItem( 'style' ).value ).to.equal( 'color: red' );
  74. } );
  75. } );
  76. describe( 'text content', () => {
  77. it( 'reacts on view#label', () => {
  78. expect( view.element.innerHTML ).to.equal( 'bar' );
  79. view.label = 'baz';
  80. expect( view.element.innerHTML ).to.equal( 'baz' );
  81. } );
  82. } );
  83. describe( 'tabindex', () => {
  84. it( 'is initially set ', () => {
  85. expect( view.element.attributes.tabindex.value ).to.equal( '-1' );
  86. } );
  87. it( 'reacts on view#tabindex', () => {
  88. view.tabindex = 3;
  89. expect( view.element.attributes.tabindex.value ).to.equal( '3' );
  90. } );
  91. } );
  92. describe( 'view#execute event', () => {
  93. it( 'triggers view#execute event when "click" is fired in DOM', () => {
  94. const spy = sinon.spy();
  95. view.on( 'execute', spy );
  96. view.element.dispatchEvent( new Event( 'click' ) );
  97. expect( spy.calledOnce ).to.be.true;
  98. } );
  99. } );
  100. } );
  101. describe( 'focus()', () => {
  102. it( 'focuses the item in DOM', () => {
  103. const spy = sinon.spy( view.element, 'focus' );
  104. view.focus();
  105. sinon.assert.calledOnce( spy );
  106. } );
  107. } );
  108. } );