listitemview.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.init();
  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( 'init()', () => {
  27. it( 'starts listening for #keystrokes coming from #element', () => {
  28. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  29. view.init();
  30. sinon.assert.calledOnce( spy );
  31. sinon.assert.calledWithExactly( spy, view.element );
  32. } );
  33. // https://github.com/ckeditor/ckeditor5-ui/issues/153
  34. it( 'triggers view#execute event when Enter or Space key is pressed', () => {
  35. const spy = sinon.spy();
  36. const evtData = {
  37. keyCode: 10,
  38. preventDefault: sinon.spy(),
  39. stopPropagation: sinon.spy()
  40. };
  41. view.on( 'execute', spy );
  42. view.keystrokes.press( evtData );
  43. sinon.assert.notCalled( spy );
  44. sinon.assert.notCalled( evtData.preventDefault );
  45. evtData.keyCode = 13;
  46. view.keystrokes.press( evtData );
  47. sinon.assert.calledOnce( spy );
  48. sinon.assert.calledOnce( evtData.preventDefault );
  49. evtData.keyCode = 32;
  50. view.keystrokes.press( evtData );
  51. sinon.assert.calledTwice( spy );
  52. sinon.assert.calledTwice( evtData.preventDefault );
  53. } );
  54. } );
  55. describe( 'DOM bindings', () => {
  56. describe( '"class" attribute', () => {
  57. it( 'reacts on view#class', () => {
  58. expect( view.element.classList ).to.have.length( 1 );
  59. view.set( 'class', 'foo' );
  60. expect( view.element.classList.contains( 'foo' ) ).to.be.true;
  61. } );
  62. it( 'reacts on view#isActive', () => {
  63. expect( view.element.classList ).to.have.length( 1 );
  64. view.set( 'isActive', true );
  65. expect( view.element.classList.contains( 'ck-list__item_active' ) ).to.be.true;
  66. } );
  67. } );
  68. describe( '"style" attribute', () => {
  69. it( 'reacts on view#style', () => {
  70. expect( view.element.attributes.getNamedItem( 'style' ).value ).to.equal( 'foo' );
  71. view.style = 'color: red';
  72. expect( view.element.attributes.getNamedItem( 'style' ).value ).to.equal( 'color: red' );
  73. } );
  74. } );
  75. describe( 'text content', () => {
  76. it( 'reacts on view#label', () => {
  77. expect( view.element.innerHTML ).to.equal( 'bar' );
  78. view.label = 'baz';
  79. expect( view.element.innerHTML ).to.equal( 'baz' );
  80. } );
  81. } );
  82. describe( 'tabindex', () => {
  83. it( 'is initially set ', () => {
  84. expect( view.element.attributes.tabindex.value ).to.equal( '-1' );
  85. } );
  86. it( 'reacts on view#tabindex', () => {
  87. view.tabindex = 3;
  88. expect( view.element.attributes.tabindex.value ).to.equal( '3' );
  89. } );
  90. } );
  91. describe( 'view#execute event', () => {
  92. it( 'triggers view#execute event when "click" is fired in DOM', () => {
  93. const spy = sinon.spy();
  94. view.on( 'execute', spy );
  95. view.element.dispatchEvent( new Event( 'click' ) );
  96. expect( spy.calledOnce ).to.be.true;
  97. } );
  98. } );
  99. } );
  100. describe( 'focus()', () => {
  101. it( 'focuses the item in DOM', () => {
  102. const spy = sinon.spy( view.element, 'focus' );
  103. view.focus();
  104. sinon.assert.calledOnce( spy );
  105. } );
  106. } );
  107. } );