focusdropdowncontentsonarrows.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 ButtonView from '../../../src/button/buttonview';
  10. import focusDropdownContentsOnArrows from '../../../src/dropdown/helpers/focusdropdowncontentsonarrows';
  11. import { createDropdownView } from '../../../src/dropdown/utils';
  12. describe( 'focusDropdownContentsOnArrows()', () => {
  13. let dropdownView;
  14. let panelChildView;
  15. beforeEach( () => {
  16. dropdownView = createDropdownView( new Model(), new ButtonView(), {} );
  17. panelChildView = new View();
  18. panelChildView.setTemplate( { tag: 'div' } );
  19. panelChildView.focus = () => {};
  20. panelChildView.focusLast = () => {};
  21. // TODO: describe this as #contentView instead of #listView and #toolbarView
  22. dropdownView.panelView.children.add( panelChildView );
  23. focusDropdownContentsOnArrows( dropdownView );
  24. dropdownView.render();
  25. document.body.appendChild( dropdownView.element );
  26. } );
  27. afterEach( () => {
  28. dropdownView.element.remove();
  29. } );
  30. it( '"arrowdown" focuses the #innerPanelView if dropdown is open', () => {
  31. const keyEvtData = {
  32. keyCode: keyCodes.arrowdown,
  33. preventDefault: sinon.spy(),
  34. stopPropagation: sinon.spy()
  35. };
  36. const spy = sinon.spy( panelChildView, 'focus' );
  37. dropdownView.isOpen = false;
  38. dropdownView.keystrokes.press( keyEvtData );
  39. sinon.assert.notCalled( spy );
  40. dropdownView.isOpen = true;
  41. dropdownView.keystrokes.press( keyEvtData );
  42. sinon.assert.calledOnce( spy );
  43. } );
  44. it( '"arrowup" focuses the last #item in #innerPanelView if dropdown is open', () => {
  45. const keyEvtData = {
  46. keyCode: keyCodes.arrowup,
  47. preventDefault: sinon.spy(),
  48. stopPropagation: sinon.spy()
  49. };
  50. const spy = sinon.spy( panelChildView, 'focusLast' );
  51. dropdownView.isOpen = false;
  52. dropdownView.keystrokes.press( keyEvtData );
  53. sinon.assert.notCalled( spy );
  54. dropdownView.isOpen = true;
  55. dropdownView.keystrokes.press( keyEvtData );
  56. sinon.assert.calledOnce( spy );
  57. } );
  58. } );