dropdownpanelview.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 ViewCollection from '../../src/viewcollection';
  7. import DropdownPanelView from '../../src/dropdown/dropdownpanelview';
  8. import View from '../../src/view';
  9. describe( 'DropdownPanelView', () => {
  10. let view, locale;
  11. beforeEach( () => {
  12. locale = { t() {} };
  13. view = new DropdownPanelView( locale );
  14. view.render();
  15. } );
  16. describe( 'constructor()', () => {
  17. it( 'sets view#locale', () => {
  18. expect( view.locale ).to.equal( locale );
  19. } );
  20. it( 'sets view#isVisible false', () => {
  21. expect( view.isVisible ).to.be.false;
  22. } );
  23. it( 'creates view#children collection', () => {
  24. expect( view.children ).to.be.instanceOf( ViewCollection );
  25. } );
  26. it( 'creates element from template', () => {
  27. expect( view.element.classList.contains( 'ck' ) ).to.be.true;
  28. expect( view.element.classList.contains( 'ck-reset' ) ).to.be.true;
  29. expect( view.element.classList.contains( 'ck-dropdown__panel' ) ).to.be.true;
  30. } );
  31. describe( 'template bindings', () => {
  32. describe( 'class', () => {
  33. it( 'reacts on view#isVisible', () => {
  34. expect( view.element.classList.contains( 'ck-dropdown__panel-visible' ) ).to.be.false;
  35. view.isVisible = true;
  36. expect( view.element.classList.contains( 'ck-dropdown__panel-visible' ) ).to.be.true;
  37. view.isVisible = false;
  38. expect( view.element.classList.contains( 'ck-dropdown__panel-visible' ) ).to.be.false;
  39. } );
  40. it( 'reacts on view#position', () => {
  41. expect( view.element.classList.contains( 'ck-dropdown__panel_se' ) ).to.be.true;
  42. view.position = 'nw';
  43. expect( view.element.classList.contains( 'ck-dropdown__panel_se' ) ).to.be.false;
  44. expect( view.element.classList.contains( 'ck-dropdown__panel_nw' ) ).to.be.true;
  45. } );
  46. } );
  47. describe( 'listeners', () => {
  48. describe( 'selectstart', () => {
  49. // https://github.com/ckeditor/ckeditor5-ui/issues/228
  50. it( 'gets preventDefault called', () => {
  51. const event = new Event( 'selectstart' );
  52. const spy = sinon.spy( event, 'preventDefault' );
  53. view.element.dispatchEvent( event );
  54. sinon.assert.calledOnce( spy );
  55. } );
  56. } );
  57. } );
  58. } );
  59. } );
  60. describe( 'focus()', () => {
  61. it( 'does nothing for empty panel', () => {
  62. expect( () => view.focus() ).to.not.throw();
  63. } );
  64. it( 'focuses first child view', () => {
  65. const firstChildView = new View();
  66. firstChildView.focus = sinon.spy();
  67. view.children.add( firstChildView );
  68. view.children.add( new View() );
  69. view.focus();
  70. sinon.assert.calledOnce( firstChildView.focus );
  71. } );
  72. } );
  73. describe( 'focusLast()', () => {
  74. it( 'does nothing for empty panel', () => {
  75. expect( () => view.focusLast() ).to.not.throw();
  76. } );
  77. it( 'focuses last child view', () => {
  78. const lastChildView = new View();
  79. lastChildView.focusLast = sinon.spy();
  80. view.children.add( new View() );
  81. view.children.add( lastChildView );
  82. view.focusLast();
  83. sinon.assert.calledOnce( lastChildView.focusLast );
  84. } );
  85. it( 'focuses last child view even if it does not have focusLast() method', () => {
  86. const lastChildView = new View();
  87. lastChildView.focus = sinon.spy();
  88. view.children.add( new View() );
  89. view.children.add( lastChildView );
  90. view.focusLast();
  91. sinon.assert.calledOnce( lastChildView.focus );
  92. } );
  93. } );
  94. } );