tab.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { CLASS_PAGINATION, CLASS_PAGINATION_PAGE } from '../../../constants/classes';
  2. import { fire, init } from '../../../test';
  3. import { Options } from '../../../types';
  4. /**
  5. * - `aria-selected` is tested on general.test.
  6. * - `aria-labelledby` is not necessary since each tabpanel has its own `aria-label`.
  7. */
  8. describe( 'Pagination', () => {
  9. test( 'can set the `tablist` role to the pagination root.', () => {
  10. init();
  11. const pagination = document.querySelector( `.${ CLASS_PAGINATION }` );
  12. expect( pagination.getAttribute( 'role' ) ).toBe( 'tablist' );
  13. } );
  14. test( 'can set the `tab` role to each item in pagination.', () => {
  15. init();
  16. const items = Array.from( document.getElementsByClassName( CLASS_PAGINATION_PAGE ) );
  17. items.forEach( item => {
  18. expect( item.getAttribute( 'role' ) ).toBe( 'tab' );
  19. } );
  20. expect( items.length ).toBeGreaterThan( 0 );
  21. } );
  22. test( 'can set the `aria-label` role to each item in pagination.', () => {
  23. const items = Array.from( document.getElementsByClassName( CLASS_PAGINATION_PAGE ) );
  24. items.forEach( ( item, index ) => {
  25. expect( item.getAttribute( 'aria-label' ) ).toBe( `Go to slide ${ index + 1 }` );
  26. } );
  27. } );
  28. test( 'can set `aria-controls="target slide ID"` to each item in pagination.', () => {
  29. const splide = init();
  30. const items = Array.from( document.getElementsByClassName( CLASS_PAGINATION_PAGE ) );
  31. const Slides = splide.Components.Slides;
  32. items.forEach( ( item, index ) => {
  33. const Slide = Slides.getAt( index );
  34. if ( Slide ) {
  35. expect( item.getAttribute( 'aria-controls' ) ).toBe( Slide.slide.id );
  36. } else {
  37. fail();
  38. }
  39. } );
  40. } );
  41. describe.each( [
  42. [ 'ltr', 'ArrowRight', 'ArrowLeft' ],
  43. [ 'rtl', 'ArrowLeft', 'ArrowRight' ],
  44. [ 'ttb', 'ArrowDown', 'ArrowUp' ],
  45. ] )( 'in %s mode.', (
  46. direction,
  47. nextKey,
  48. prevKey
  49. ) => {
  50. test( 'can move focus by arrow keys and activate the corresponded slide', () => {
  51. const splide = init( { speed: 0, direction: direction as Options[ 'direction' ], height: 300 } );
  52. const items = document.getElementsByClassName( CLASS_PAGINATION_PAGE );
  53. fire( items[ 0 ], 'keydown', { key: nextKey } );
  54. expect( items[ 1 ] === document.activeElement ).toBe( true );
  55. expect( splide.index ).toBe( 1 );
  56. fire( items[ 1 ], 'keydown', { key: nextKey } );
  57. expect( items[ 2 ] === document.activeElement ).toBe( true );
  58. expect( splide.index ).toBe( 2 );
  59. fire( items[ 2 ], 'keydown', { key: prevKey } );
  60. expect( items[ 1 ] === document.activeElement ).toBe( true );
  61. expect( splide.index ).toBe( 1 );
  62. } );
  63. test( 'can loop focus by arrow keys.', () => {
  64. const splide = init( { speed: 0, direction: direction as Options[ 'direction' ], height: 300 } );
  65. const items = document.getElementsByClassName( CLASS_PAGINATION_PAGE );
  66. const end = splide.length - 1;
  67. fire( items[ 0 ], 'keydown', { key: prevKey } );
  68. expect( items[ end ] === document.activeElement ).toBe( true );
  69. expect( splide.index ).toBe( end );
  70. fire( items[ end ], 'keydown', { key: nextKey } );
  71. expect( items[ 0 ] === document.activeElement ).toBe( true );
  72. expect( splide.index ).toBe( 0 );
  73. } );
  74. test( 'can focus the first slide by and the last slide by End.', () => {
  75. const splide = init( { speed: 0, direction: direction as Options[ 'direction' ], height: 300 } );
  76. const items = document.getElementsByClassName( CLASS_PAGINATION_PAGE );
  77. const end = splide.length - 1;
  78. fire( items[ 0 ], 'keydown', { key: 'End' } );
  79. expect( items[ end ] === document.activeElement ).toBe( true );
  80. expect( splide.index ).toBe( end );
  81. fire( items[ end ], 'keydown', { key: 'Home' } );
  82. expect( items[ 0 ] === document.activeElement ).toBe( true );
  83. expect( splide.index ).toBe( 0 );
  84. } );
  85. test( 'can set proper orientation according to the direction.', () => {
  86. init( { speed: 0, direction: direction as Options[ 'direction' ], height: 300 } );
  87. const pagination = document.querySelector( `.${ CLASS_PAGINATION }` );
  88. expect( pagination.getAttribute( 'aria-orientation' ) )
  89. .toBe( direction === 'ttb' ? 'vertical' : null );
  90. } );
  91. } );
  92. test( 'should not activate keyboard shortcuts if `paginationKeyboard` option is disabled.', () => {
  93. const splide = init( { paginationKeyboard: false, speed: 0 } );
  94. const items = document.getElementsByClassName( CLASS_PAGINATION_PAGE );
  95. fire( items[ 0 ], 'keydown', { key: 'ArrowRight' } );
  96. expect( items[ 1 ] === document.activeElement ).toBe( false );
  97. expect( splide.index ).toBe( 0 );
  98. } );
  99. } );