keyboard.test.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { minimum } from '../data/html';
  2. import Splide from '../../src/js/splide';
  3. import { COMPLETE } from '../../src/js/components';
  4. describe( 'The Keyboard', () => {
  5. beforeEach( () => {
  6. document.body.innerHTML = minimum;
  7. } );
  8. test( 'should listen to the keydown event of the document if the keyboard option is "global" or true.', done => {
  9. const splide = new Splide( '#splide', {}, COMPLETE );
  10. splide.mount();
  11. splide.on( 'moved', () => {
  12. expect( splide.index ).toBe( 1 );
  13. done();
  14. } );
  15. document.dispatchEvent( new KeyboardEvent( 'keydown', { key: 'ArrowRight' } ) );
  16. splide.Components.Elements.list.dispatchEvent( new Event( 'transitionend' ) );
  17. } );
  18. test( 'should add tabindex to the root element if the keyboard option is "focused".', () => {
  19. const splide = new Splide( '#splide', { keyboard: 'focused' }, COMPLETE );
  20. splide.mount();
  21. expect( splide.root.getAttribute( 'tabindex' ) ).toBe( '0' );
  22. } );
  23. test( 'should listen to the keydown event of the root element if the keyboard option is "focused".', done => {
  24. const splide = new Splide( '#splide', { keyboard: 'focused' }, COMPLETE );
  25. splide.mount();
  26. splide.on( 'moved', () => {
  27. expect( splide.index ).toBe( 1 );
  28. done();
  29. } );
  30. splide.root.dispatchEvent( new KeyboardEvent( 'keydown', { key: 'ArrowRight' } ) );
  31. splide.Components.Elements.list.dispatchEvent( new Event( 'transitionend' ) );
  32. } );
  33. } );