dropdownpanelview.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 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-reset' ) ).to.be.true;
  28. expect( view.element.classList.contains( 'ck-dropdown__panel' ) ).to.be.true;
  29. } );
  30. describe( 'template bindings', () => {
  31. describe( 'class', () => {
  32. it( 'reacts on view#isVisible', () => {
  33. expect( view.element.classList.contains( 'ck-dropdown__panel-visible' ) ).to.be.false;
  34. view.isVisible = true;
  35. expect( view.element.classList.contains( 'ck-dropdown__panel-visible' ) ).to.be.true;
  36. view.isVisible = false;
  37. expect( view.element.classList.contains( 'ck-dropdown__panel-visible' ) ).to.be.false;
  38. } );
  39. } );
  40. describe( 'listeners', () => {
  41. describe( 'selectstart', () => {
  42. // https://github.com/ckeditor/ckeditor5-ui/issues/228
  43. it( 'gets preventDefault called', () => {
  44. const event = new Event( 'selectstart' );
  45. const spy = sinon.spy( event, 'preventDefault' );
  46. view.element.dispatchEvent( event );
  47. sinon.assert.calledOnce( spy );
  48. } );
  49. } );
  50. } );
  51. } );
  52. } );
  53. describe( 'focus()', () => {
  54. it( 'does nothing for empty panel', () => {
  55. expect( () => view.focus() ).to.not.throw();
  56. } );
  57. it( 'focuses first child view', () => {
  58. const firstChildView = new View();
  59. firstChildView.focus = sinon.spy();
  60. view.children.add( firstChildView );
  61. view.children.add( new View() );
  62. view.focus();
  63. sinon.assert.calledOnce( firstChildView.focus );
  64. } );
  65. } );
  66. describe( 'focusLast()', () => {
  67. it( 'does nothing for empty panel', () => {
  68. expect( () => view.focusLast() ).to.not.throw();
  69. } );
  70. it( 'focuses last child view', () => {
  71. const lastChildView = new View();
  72. lastChildView.focusLast = sinon.spy();
  73. view.children.add( new View() );
  74. view.children.add( lastChildView );
  75. view.focusLast();
  76. sinon.assert.calledOnce( lastChildView.focusLast );
  77. } );
  78. it( 'focuses last child view even if it does not have focusLast() method', () => {
  79. const lastChildView = new View();
  80. lastChildView.focus = sinon.spy();
  81. view.children.add( new View() );
  82. view.children.add( lastChildView );
  83. view.focusLast();
  84. sinon.assert.calledOnce( lastChildView.focus );
  85. } );
  86. } );
  87. } );