focusdropdowncontentsonarrows.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  7. import View from '../../../src/view';
  8. import Model from '../../../src/model';
  9. import focusDropdownContentsOnArrows from '../../../src/dropdown/helpers/focusdropdowncontentsonarrows';
  10. import { createDropdown } from './../../../src/dropdown/utils';
  11. describe( 'focusDropdownContentsOnArrows()', () => {
  12. let dropdownView;
  13. let panelChildView;
  14. beforeEach( () => {
  15. dropdownView = createDropdown( new Model(), {} );
  16. panelChildView = new View();
  17. panelChildView.setTemplate( { tag: 'div' } );
  18. panelChildView.focus = () => {};
  19. panelChildView.focusLast = () => {};
  20. // TODO: describe this as #contentView instead of #listView and #toolbarView
  21. dropdownView.panelView.children.add( panelChildView );
  22. focusDropdownContentsOnArrows( dropdownView );
  23. dropdownView.render();
  24. document.body.appendChild( dropdownView.element );
  25. } );
  26. afterEach( () => {
  27. dropdownView.element.remove();
  28. } );
  29. it( '"arrowdown" focuses the #innerPanelView if dropdown is open', () => {
  30. const keyEvtData = {
  31. keyCode: keyCodes.arrowdown,
  32. preventDefault: sinon.spy(),
  33. stopPropagation: sinon.spy()
  34. };
  35. const spy = sinon.spy( panelChildView, 'focus' );
  36. dropdownView.isOpen = false;
  37. dropdownView.keystrokes.press( keyEvtData );
  38. sinon.assert.notCalled( spy );
  39. dropdownView.isOpen = true;
  40. dropdownView.keystrokes.press( keyEvtData );
  41. sinon.assert.calledOnce( spy );
  42. } );
  43. it( '"arrowup" focuses the last #item in #innerPanelView if dropdown is open', () => {
  44. const keyEvtData = {
  45. keyCode: keyCodes.arrowup,
  46. preventDefault: sinon.spy(),
  47. stopPropagation: sinon.spy()
  48. };
  49. const spy = sinon.spy( panelChildView, 'focusLast' );
  50. dropdownView.isOpen = false;
  51. dropdownView.keystrokes.press( keyEvtData );
  52. sinon.assert.notCalled( spy );
  53. dropdownView.isOpen = true;
  54. dropdownView.keystrokes.press( keyEvtData );
  55. sinon.assert.calledOnce( spy );
  56. } );
  57. } );